NoCache

Table of Contents

Install & Manage Multiple Versions of Node.js with NVM on Linux

Cyrus Kao
Last modified on .

Managing Node.js versions on Linux is no longer a pain in the ass with NVM (Node Version Manager). It can install different versions of Node.js and NPM on a single machine, switch versions anytime you wish. And the best part is NVM is just a Bash script, truly a lightweight helper for both testing and production.

Downloading binaries from Node.js and updating them manually every now and then is not a pleasant thing to do, and installing Node.js via package manager is usually not a good idea since it's not maintained by the core team and often outdated:

Notice
Notice on the official website of Node.js

Installation

Install NVM with install.sh from its latest release, change v0.39.1 if a newer version is released:

  • curl

    $ curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
    
  • wget

    $ wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
    

It's good practice to review the shell scripts before executing them.

Check if it's installed:

$ nvm -v
0.39.1
Output

Troubleshooting

If you got nvm: command not found, you might need to restart the terminal session for the shell configuration to work. Or to force an update:

  • Bash

    $ source ~/.bashrc
    
  • Zsh

    $ source ~/.zshrc
    

Usage

For the full usage guide, use --help:

$ nvm --help
Node Version Manager (v0.39.1)

Note: <version> refers to any version-like string nvm understands. This includes:
  - full or partial version numbers, starting with an optional "v" (0.10, v0.1.2, v1)
  - default (built-in) aliases: node, stable, unstable, iojs, system
  - custom aliases you define with `nvm alias foo`

 Any options that produce colorized output should respect the `--no-colors` option.

Usage:
  nvm --help                                  Show this message
    --no-colors                               Suppress colored output
  nvm --version                               Print out the installed version of nvm

	...

Note:
  to remove, delete, or uninstall nvm - just remove the `$NVM_DIR` folder (usually `~/.nvm`)
Output

Install Versions

To install the latest version of Node.js:

$ nvm install node

To install the LTS (Long Term Support) version of Node.js:

$ nvm install --lts

To install a certain version of Node.js and migrate the NPM global packages from the current version:

$ nvm install 17.0.0 --reinstall-packages-from=current

Uninstall Versions

The same goes with uninstalling:

$ nvm uninstall 17.0.0

Switch Versions

Print out the list of installed Node.js versions:

$ nvm list
        v16.8.0
       v16.13.2
->      v17.0.1
default -> 17.0.1 (-> v17.0.1)
iojs -> N/A (default)
unstable -> N/A (default)
node -> stable (-> v17.0.1) (default)
stable -> 17.0 (-> v17.0.1) (default)
lts/* -> lts/gallium (-> v16.13.2)
lts/argon -> v4.9.1 (-> N/A)
lts/boron -> v6.17.1 (-> N/A)
lts/carbon -> v8.17.0 (-> N/A)
lts/dubnium -> v10.24.1 (-> N/A)
lts/erbium -> v12.22.9 (-> N/A)
lts/fermium -> v14.18.3 (-> N/A)
lts/gallium -> v16.13.
Output

Use lts/* alias to switch to v16.13.2:

$ nvm use lts/*

Or to use a certain version of Node.js:

$ nvm use 17.0.1

Change Default Version

use is only gonna change the version in current session, to set a version as default:

$ nvm alias default 17.0.0

Uninstalling

Remove the NVM directory:

$ rm -rf "$NVM_DIR"

Edit ~/.bashrc or ~/.zshrc for Zsh, delete the following lines:

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
[[ -r $NVM_DIR/bash_completion ]] && \. $NVM_DIR/bash_completion
Bash

See Also

Comments

Sign in to leave a comment.