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/creationix/nvm/v0.33.11/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 installednvm
, let’s look at how you can use it to manage the versions of Node.js installed on your system.
To download, compile, and install the latest release of node, run:
$ nvm install node Downloading and installing node v10.9.0... Downloading https://nodejs.org/dist/v10.9.0/node-v10.9.0-linux-x64.tar.xz... ################################################################### 100.0% Computing checksum with sha256sum Checksums matched! Now using node v10.9.0 (npm v6.2.0) Creating default alias: default -> node (-> v10.9.0)
To check installed versions, use:
$ nvm ls -> v10.9.0 default -> node (-> v10.9.0) node -> stable (-> v10.9.0) (default) stable -> 10.9 (-> v10.9.0) (default) iojs -> N/A (default) lts/* -> lts/carbon (-> N/A) lts/argon -> v4.9.1 (-> N/A) lts/boron -> v6.14.4 (-> N/A) lts/carbon -> v8.11.4 (-> N/A)
To use the latest version on the shell, run:
$ nvm use node Now using node v10.9.0 (npm v6.2.0)
Use a specific version of node:
$ nvm use v8.11.4 Now using node v8.11.4 (npm v5.6.0)
Check versions that can be installed:
$ nvm ls-remote
Install specific version of node
$ nvm install v8.11.4
This installs the LTS release of node.
$ nvm install v8.11.4 Downloading and installing node v8.11.4... Downloading https://nodejs.org/dist/v8.11.4/node-v8.11.4-linux-x64.tar.xz... ############################################################################################################################################### 100.0% Computing checksum with sha256sum Checksums matched! Now using node v8.11.4 (npm v5.6.0)
You can also install lts
by using the option --lts
$ nvm install --lts Installing latest LTS version. v8.11.4 is already installed. Now using node v8.11.4 (npm v5.6.0)
Check installed versions again:
$ nvm ls -> v8.11.4 v10.9.0 default -> node (-> v10.9.0) node -> stable (-> v10.9.0) (default) stable -> 10.9 (-> v10.9.0) (default) iojs -> N/A (default) lts/* -> lts/carbon (-> v8.11.4) lts/argon -> v4.9.1 (-> N/A) lts/boron -> v6.14.4 (-> N/A) lts/carbon -> v8.11.4
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
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.