Node Version Manager (NVM) is a tool for managing Node versions on servers. NVM allows to install different versions of Node. It allows you to switch between different Node versions depending on the project being worked on.
In this article we will install NVM on Ubuntu. Next, we will show examples of migrations between different Node versions.
Download and Install NVM From Official Repo
First, update the package list:
foc@ubuntu22:~$ sudo apt update -y
Then you should install the wget or curl package to download the installation script. For example, let's install wget:
foc@ubuntu22:~$ sudo apt install wget -y
The install.sh script from the official github page is pulled with wget:
foc@ubuntu22:~$ wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.2/install.sh | bash
=> Downloading nvm from git to '/home/foc/.nvm'
=> Cloning into '/home/foc/.nvm'...
remote: Enumerating objects: 356, done.
remote: Counting objects: 100% (356/356), done.
remote: Compressing objects: 100% (303/303), done.
remote: Total 356 (delta 39), reused 164 (delta 27), pack-reused 0
Receiving objects: 100% (356/356), 222.15 KiB | 271.00 KiB/s, done.
Resolving deltas: 100% (39/39), done.
* (HEAD detached at FETCH_HEAD)
master
=> Compressing and cleaning up git repository
=> Appending nvm source string to /home/foc/.bashrc
=> Appending bash_completion source string to /home/foc/.bashrc
=> Close and reopen your terminal to start using nvm or run the following to use it now:
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
Reload the .bashrc:
foc@ubuntu22:~$ source ~/.bashrc
If you are using different shell the command should be different, For example:
- zsh:
source ~/.zshrc
- ksh:
. ~/.profile
Installed NVM version:
foc@ubuntu22:~$ nvm --version
0.39.2
Installing Node With NVM
Now list the NVM installable Node versions:
foc@ubuntu22:~$ nvm ls-remote
v18.10.0
v18.11.0
v18.12.0 (LTS: Hydrogen)
v18.12.1 (Latest LTS: Hydrogen)
v19.0.0
v19.0.1
v19.1.0
v19.2.0
v19.3.0
Let's install the latest LTS version:
foc@ubuntu22:~$ nvm install v18.12.1
Downloading and installing node v18.12.1...
Downloading https://nodejs.org/dist/v18.12.1/node-v18.12.1-linux-x64.tar.xz...
################################################################# 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v18.12.1 (npm v8.19.2)
Creating default alias: default -> v18.12.1
You can see the different versions you have installed by typing "list":
foc@ubuntu22:~$ nvm list
-> v18.12.1
default -> v18.12.1
iojs -> N/A (default)
unstable -> N/A (default)
node -> stable (-> v18.12.1) (default)
stable -> 18.12 (-> v18.12.1) (default)
lts/* -> lts/hydrogen (-> v18.12.1)
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.21.2 (-> N/A)
lts/gallium -> v16.19.0 (-> N/A)
lts/hydrogen -> v18.12.1
Let's also install the v19.3.0 version:
foc@ubuntu22:~$ nvm install v19.3.0
Downloading and installing node v19.3.0...
Downloading https://nodejs.org/dist/v19.3.0/node-v19.3.0-linux-x64.tar.xz...
################################################################# 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v19.3.0 (npm v9.2.0)
List the installed Node versions again:
foc@ubuntu22:~$ nvm list
v18.12.1
-> v19.3.0
default -> v18.12.1
iojs -> N/A (default)
unstable -> N/A (default)
node -> stable (-> v19.3.0) (default)
stable -> 19.3 (-> v19.3.0) (default)
lts/* -> lts/hydrogen (-> v18.12.1)
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.21.2 (-> N/A)
lts/gallium -> v16.19.0 (-> N/A)
lts/hydrogen -> v18.12.1
With ls-remote, you can list the installable versions, select the version you want to use and install it.
Uninstalling Node With NVM
To uninstall a Node version, you must type uninstall :
foc@ubuntu22:~$ nvm uninstall v19.3.0
nvm: Cannot uninstall currently-active node version, v19.3.0 (inferred from v19.3.0).
It warns that there is an active Node version, it must be deactivated first:
foc@ubuntu22:~$ nvm deactivate
/home/foc/.nvm/*/bin removed from ${PATH}
Then the version can be removed:
foc@ubuntu22:~$ nvm uninstall v19.3.0
Uninstalled node v19.3.0
Summary
We explained the installation of NVM on Ubuntu. Installed with NVM v0.39.2 version shell script. The installation steps will not be different with a different shell script version.
NVM does not have a manual page on Ubuntu:
foc@ubuntu22:~$ man nvm No manual entry for nvm
You can get help with the "--help
" parameter:
foc@ubuntu22:~$ nvm --help ... Example: nvm install 8.0.0 Install a specific version number nvm use 8.0 Use the latest available 8.0.x release nvm run 6.10.3 app.js Run app.js using node 6.10.3 nvm exec 4.8.3 node app.js Run `node app.js` with the PATH pointing to node 4.8.3 nvm alias default 8.1.0 Set default node version on a shell nvm alias default node Always default to the latest available node version on a shell nvm install node Install the latest available version nvm use node Use the latest version nvm install --lts Install the latest LTS version nvm use --lts Use the latest LTS version nvm set-colors cgYmW Set text colors to cyan, green, bold yellow, magenta, and white
References
github.com/nvm-sh/nvm - Installing and Updating