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

mail
Cyrus Kao
Last modified

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 update 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 official website of Node.js

Installation

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

  • cURL

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

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

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

Check if it's installed:

nvm -v
Bash
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 update:

  • Bash

    source ~/.bashrc
    Bash
  • Zsh

    source ~/.zshrc
    Bash

Usage

For full usage guide, use --help:

nvm --help
Bash
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
Bash

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

nvm install --lts
Bash

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

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

Uninstall Versions

Same goes with uninstalling:

nvm uninstall 17.0.0
Bash

Switch Versions

Print out the list of installed Node.js versions:

nvm list
Bash
        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/*
Bash

Or to use certain version of Node.js:

nvm use 17.0.1
Bash

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
Bash

Uninstalling

Remove NVM directory:

rm -rf "$NVM_DIR"
Bash

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

0500