NVM (Node Version Manager) is a tool that allows you to easily install and manage multiple versions of Node.js on a single system. In this article, we will learn how to install and use NVM on Ubuntu 20.04.
Prerequisites
Before we begin, make sure that you have a clean installation of Ubuntu 20.04 and a user with sudo privileges. You will also need to have the curl
package installed.
To check if the curl
package is installed, run the following command:
dpkg -s curl
If the package is installed, you should see a message saying Status: install ok installed
. If the package is not installed, you will need to install it by running the following command:
sudo apt install curl
Installing NVM
To install NVM, we will first download the installation script from the official repository. You can find the latest version of the installation script at the following URL:
<https://raw.githubusercontent.com/nvm-sh/nvm/v0.37.2/install.sh>
Replace v0.37.2
with the latest version of NVM.
To download the script, run the following command:
curl -o- <https://raw.githubusercontent.com/nvm-sh/nvm/v0.37.2/install.sh> | bash
This will download the installation script and run it automatically. The script will install NVM and add the necessary lines to your .bashrc
file to configure the PATH
environment variable.
After the installation is complete, you can verify that NVM is installed correctly by running the following command:
command -v nvm
This command should print the path to the nvm
executable, which indicates that NVM is installed and available on your system.
Using NVM
Now that NVM is installed, we can use it to install and manage multiple versions of Node.js on our system.
To see the available versions of Node.js that can be installed using NVM, run the following command:
nvm ls-remote
This will display a list of all the available versions of Node.js that can be installed using NVM.
To install a specific version of Node.js, run the following command:
nvm install 14.15.1
Replace 14.15.1
with the version of Node.js that you want to install.
After the installation is complete, you can verify that Node.js is installed correctly by running the following command:
node -v
This command should print the version of Node.js that you installed, which indicates that Node.js is installed and working correctly.
Switching Between Versions of Node.js
NVM allows you to easily switch between different versions of Node.js that are installed on your system. This can be useful if you have multiple projects that require different versions of Node.js.
To see the list of installed versions of Node.js, run the following command:
nvm ls
This will display a list of all the versions of Node.js that are installed on your system.
To switch to a different version of Node.js, run the following command:
nvm use 14.15.1
Replace 14.15.1
with the version of Node.js that you want to use.
After you switch to a different version of Node.js, you can verify that the switch was successful by running the node -v
command again. This should print the version of Node.js that you selected, which indicates that you are now using that version of Node.js.
Uninstalling Node.js
To uninstall a version of Node.js that is installed using NVM, run the following command:
nvm uninstall 14.15.1
Replace 14.15.1
with the version of Node.js that you want to uninstall.
After the uninstallation is complete, the specified version of Node.js will be removed from your system.
Conclusion
In this article, we learned how to install and use NVM on Ubuntu 20.04. We installed NVM, used it to install and manage multiple versions of Node.js, and switched between different versions of Node.js. NVM is a useful tool that allows you to easily install and manage multiple versions of Node.js on a single system.