Step 1: Installing NVM on Ubuntu
To install NVM, you need to run a shell script on your Ubuntu 20.04 system. Open a terminal or connect to a remote system using SSH.
-
First, install curl with the following command:
sudo apt install curl
-
Next, run the NVM installer script using curl:
curl https://raw.githubusercontent.com/creationix/nvm/master/install.sh | bash
The installer script adds NVM to your login script.
-
To activate it, either log out and log back in or run:
source ~/.bashrc
NVM command-line tool is now installed and configured successfully on your Ubuntu system.
Step 2: Installing Node using NVM
As you already have NVM installed, Now you can install multiple Node.js versions on your system. Remember that the Node version installed with NVM is user specific. It keeps all files under $HOME/.nvm
directory.
-
To install the latest Node.js version, type:
nvm install node
-
To install a specific version of Node.js, use:
nvm install 18.16.0
Replace
18.16.0
with the version you need. -
To install the latest Long Term Support (LTS) version, use:
nvm install --lts
The first version you install becomes the default. New terminal sessions will use this default version.
Step 3: Working with NVM
NVM provide large number of command line options to manage node versions. Here is the frequently used commands:
-
To list the Node.js versions installed on your system, use:
nvm ls
-
To see all available Node.js versions for installation, use:
nvm ls-remote
-
To use a different Node.js version for the current terminal session, use:
nvm use 18.16.0
Replace
18.16.0
with the version you want. -
To find the default Node.js version for the current user, type:
nvm run default --version
-
To run a Node.js script with a specific version, use:
nvm exec 18.16.0 server.js
Step 4: Uninstalling a Node Version (Optional)
To uninstall a specific Node.js version, use the nvm uninstall
command followed by the version number. For example, to uninstall version 18.16.0, use:
nvm uninstall 18.16.0
Replace 18.16.0
with the version you want to uninstall.