If you are managing multiple Node.js projects, they might be running different versions of Node.js.
Node.js does not support running different versions of the runtime concurrently. This means you have to use third-party software to do it.
To do this on Linux, all you have to do is install some extra packages and then you can install different versions of Node.js on your computer. One of the easiest ways to get different versions of Node.js on your computer is to use [nvm](https://github.com/nvm-sh/nvm)
.
nvm
is available for free for anyone to download. To download it, download with curl
and install it:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh | bash
Or, alternatively, you can do the same thing with wget
:
wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh | bash
What the script does is clone the nvm repository to ~/.nvm
and add the source line to your profile (~/.bash_profile
, ~/.zshrc
, ~/.profile
, or ~/.bashrc
).
To load nvm
, put the following in your profile file (~/.bash_profile
, ~/.zshrc
, ~/.profile
, or ~/.bashrc
):
export NVM_DIR="${XDG_CONFIG_HOME/:-$HOME/.}nvm"
[ -s "$NV_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" # This loads nvm
The --no-use
option is available if you add the --no-use
after nvm.sh
, so that it won’t pick the default version of Node.js.
You can check the nvm
version by running nvm -v
.
Alternatively, you can clone the Git repository for nvm
manually and install it. You can do this by running:
$ cd ~/
$ git clone https://github.com/nvm-sh/nvm.git .nvm
Then, add the following to your ~/.bashrc
, ~/.profile
, or ~/.zshrc
file to have it run on login:
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
nvm
is ready to use once you install at least one version of Node.js.
To install the latest one, run nvm install node
, or to install an earlier version, run nvm install 6.14.4
. The first version of Node.js installed will be the default version.
To list the versions of Node.js installed, run:
nvm ls-remote
When you open a new shell, you have to run nvm
again.
You can also run the node
command with nvm run
.
For example, nvm run node -v
. To choose the version you want to run, run nvm exec 4.2 node — v
.
To install the path of one version of Node.js runtime, run:
nvm which 10.16
You can install the latest long term support (LTS) version without specifying the version. To do this, run nvm install --lts
.
To install other LTS versions of Node.js, you can specify those by their codename, for example, nvm install — lts=carbon
.
Other example commands include:
nvm install lts/carbon
: Installs the latest 8.x version of Node.js.nvm uninstall --lts
: Remove the latest LTS version.nvm uninstall --lts=carbon
: Remove the latest 8.x version of Node.js.nvm uninstall 'lts/*'
: Uninstall all LTS versions of Node.js.nvm uninstall lts/carbon
: Uninstall the latest 8.x version of Node.js.nvm use --lts
: Use the latest LTS version of Node.js.nvm use --lts=carbon
: Use the latest 8.x version of Node.js.nvm use 'lts/*'
: Use the latest LTS version of Node.js installed.nvm use lts/carbon
: Use the 8.x version of Node.js.nvm exec --lts
: Run a Node.js script with the latest LTS version of Node.js.nvm exec --lts=carbon
: Run the Node.js script with the latest 8.x version of Node.js.nvm exec 'lts/*'
: Run a Node.js script with the latest LTS version of Node.js.nvm exec lts/carbon
: Run a Node.js script with the latest 8.x version of Node.js.nvm run --lts
: Run a Node.js script with the latest LTS version of Node.js.nvm run --lts=carbon
: Run a Node.js script with the latest 8.x version of Node.js.nvm run 'lts/*'
: Run a Node.js script with the latest LTS version of Node.js.nvm run lts/carbon
: Run a Node.js script with the latest 8.x version of Node.js.nvm ls-remote --lts
: List all the LTS versions of Node.js.nvm ls-remote --lts=carbon
: List all 8.x versions of Node.js.nvm ls-remote 'lts/*'
: List all the LTS versions of Node.js.nvm ls-remote lts/carbon
: List all 8.x versions of Node.js.nvm version-remote --lts
: List the latest LTS version of Node.js.nvm version-remote --lts=carbon
: List the latest 8.x version of Node.js.nvm version-remote 'lts/*'
: List the latest LTS version of Node.js.nvm version-remote lts/carbon
: List the latest 8.x version of Node.js.
To migration global packages while installing nvm
, run:
nvm install node --reinstall-packages-from=node
It will identify the version of Node you are migration from, by running nvm version node
before migrating. Then, it will run nvm reinstall-packages
to reinstall the packages into your new environment.
You can also specify the versions of Node.js migrate to and from:
nvm install 8--reinstall-packages-from=5
nvm install 8--reinstall-packages-from=argon
To use the system version of Node.js, run:
nvm use system
To list the versions of Node.js installed, run:
nvm ls
And, to see the available versions you can install, run:
nvm ls-remote
By default, nvm ls
, nvm ls-remote
, and nvm alias
output in color text. You can disable color text output by running:
nvm ls --no-colors
Run nvm deactivate
to deactivate nvm
and restore your original PATH
environment variable.
Finally, to set the default Node.js version for a new shell, run:
nvm alias default node
nvm
is one of the easiest programs to manage multiple Node.js versions on your Linux computer. It also works with Windows Subsystem for Linux.