In this guide, I’ll show you how you can run multiple versions of Node.js on Linux using Node Version Manager (NVM). NVM is a simple bash script to manage multiple active node.js versions using your favorite Linux terminal.
How to Install Node Version Manager on Linux
Node Version Manager project provides a script which automates the installation for you. Install it by simply running the commands below:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/master/install.sh|bash
The script will clone the nvm repository to ~/.nvm
directory and adds the following source line to your profile: (~/.bash_profile, ~/.zshrc, ~/.profile, or ~/.bashrc).
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion
If you’re a zsh
user, add the lines to your ~/.zshrc
file.
To verify that nvm has been installed, do:
$ source ~/.bashrc
$ command -v nvm
nvm
This should output ‘nvm‘ if the installation was successful.
How to use nvm to manage Node.js versions
Now that you have installed nvm, let’s look at how you can use it to manage the versions of Node.js installed on your system.
Check available versions of Node.js
Check versions that can be installed:
$ nvm ls-remote
.................................
v12.22.12 (Latest LTS: Erbium)
v14.19.3 (Latest LTS: Fermium)
v16.15.0 (Latest LTS: Gallium)
v18.2.0
Install Latest version of Node.js
To download, compile, and install the latest release of node, run:
$ nvm install node
Downloading and installing node v18.2.0...
Downloading https://nodejs.org/dist/v18.2.0/node-v18.2.0-linux-x64.tar.xz...
############################################################################################################################################################################################### 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v18.2.0 (npm v8.9.0)
Creating default alias: default -> node (-> v18.2.0)
Install Specific version of Node.js
Install a specific version of node, provide version number as an argument to nvm install command.
$ nvm install v16
Downloading and installing node v16.15.0...
Downloading https://nodejs.org/dist/v16.15.0/node-v16.15.0-linux-x64.tar.xz...
############################################################################################################################################################################################### 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v16.15.0 (npm v8.5.5)
$ nvm install v14
Downloading and installing node v14.19.3...
Downloading https://nodejs.org/dist/v14.19.3/node-v14.19.3-linux-x64.tar.xz...
############################################################################################################################################################################################### 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v14.19.3 (npm v6.14.17)
Install Node.js LTS version
This installs the latest LTS release of node.
$ nvm install --lts
Downloading and installing node v16.15.0...
Downloading https://nodejs.org/dist/v16.15.0/node-v16.15.0-linux-x64.tar.xz...
############################################################################################################################################################################################### 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v16.15.0 (npm v8.5.5)
List installed Node.js versions
To check installed versions, use:
$ nvm ls
v14.19.3
-> v16.15.0
v18.2.0
default -> node (-> v18.2.0)
iojs -> N/A (default)
unstable -> N/A (default)
node -> stable (-> v18.2.0) (default)
stable -> 18.2 (-> v18.2.0) (default)
lts/* -> lts/gallium (-> v16.15.0)
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.12 (-> N/A)
lts/fermium -> v14.19.3
lts/gallium -> v16.15.0
Use Latest Node.js
To use the latest version on the shell, run:
$ nvm use node
Now using node v18.2.0 (npm v8.9.0)
Use a specific version of node:
Specify version number.
$ nvm use v16
Now using node v16.15.0 (npm v8.5.5)
Migrating global packages while installing:
If you want to install a new version of Node.js and migrate npm packages from a previous version:
$ nvm install node --reinstall-packages-from=node
v18.2.0 is already installed.
Now using node v18.2.0 (npm v8.9.0)
Can not reinstall packages from the current version of node.
To use the system version of node, add system at the end of use.
$ nvm use system
$ nvm run system --version
To restore your PATH, you can deactivate it:
nvm deactivate
For more usage examples, visit nvm Github page.
Similar articles: