NVM (Node Version Manager) is a powerful tool that allows you to easily manage multiple versions of Node.js and npm on your system. This is particularly useful for developers who need to switch between different versions of Node.js for various projects. In this article, we'll walk you through the steps to install NVM on Ubuntu and how to use it to manage Node.js versions.
Prerequisites
Before you begin, make sure you have the following:
- A system running Ubuntu (this guide is tested on Ubuntu 20.04 and 22.04).
- A user account with sudo privileges.
- Access to a terminal.
Step 1: Update Your System
First, it's a good idea to update your system's package list to ensure you have the latest versions of all software.
sudo apt update
sudo apt upgrade -y
Step 2: Install Curl
NVM is installed using a script that is downloaded via curl
. If curl
is not already installed on your system, you can install it using the following command:
sudo apt install curl -y
Step 3: Download and Install NVM
Once curl
is installed, you can download and install NVM by running the following command:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.1/install.sh | bash
Note: Replace v0.40.1
with the latest version of NVM if a newer version is available. You can check the latest version on the NVM GitHub page.
This command downloads the installation script and runs it. The script clones the NVM repository to ~/.nvm
and adds the necessary environment variables to your shell profile file (~/.bashrc
, ~/.zshrc
, ~/.profile
, or ~/.bash_profile
).
Step 4: Load NVM into Your Shell
After the installation is complete, you need to load NVM into your current shell session. You can do this by sourcing your shell profile file:
source ~/.bashrc
Alternatively, you can close and reopen your terminal to apply the changes.
Step 5: Verify NVM Installation
To verify that NVM has been installed correctly, you can run the following command:
nvm --version
This should output the version of NVM that you installed, confirming that the installation was successful.
Step 6: Install Node.js Using NVM
Now that NVM is installed, you can use it to install any version of Node.js. For example, to install the latest stable version of Node.js, you can use the following command:
nvm install node
To install a specific version of Node.js, you can specify the version number:
nvm install 16.14.0
You can also install the latest LTS (Long Term Support) version of Node.js:
nvm install --lts
Step 7: Switch Between Node.js Versions
One of the key features of NVM is the ability to switch between different versions of Node.js. To switch to a specific version, use the following command:
nvm use 16.14.0
To switch to the latest LTS version:
nvm use --lts
Step 8: Set a Default Node.js Version
If you want to set a default Node.js version that will be used in all new shell sessions, you can use the following command:
nvm alias default 16.14.0
Replace 16.14.0
with the version you want to set as the default.
Step 9: List Installed Node.js Versions
To see a list of all the Node.js versions you have installed using NVM, you can use the following command:
nvm ls
Step 10: Uninstall a Node.js Version
If you no longer need a specific version of Node.js, you can uninstall it using the following command:
nvm uninstall 16.14.0
Replace 16.14.0
with the version you want to uninstall.
Step 11: Update NVM
To update NVM to the latest version, you can use the following command:
nvm install-latest-nvm
This will download and install the latest version of NVM.
Conclusion
NVM is an essential tool for developers who need to manage multiple versions of Node.js on their system. With NVM, you can easily install, switch between, and manage different versions of Node.js, making it easier to work on various projects with different requirements.
By following the steps outlined in this guide, you should now have NVM installed on your Ubuntu system and be able to use it to manage Node.js versions effectively. Happy coding!