Install Node.js With Nvm (Node.js Version Manager) on Ubuntu

Introduction
We will be using the Node Version Manager (Nvm) to install Node.js on an Ubuntu operating system. And compared to other Node.js installation methods, it has the easiest ways to have multiple versions of Node.js without adding much extra complexity. There will be times when an application needs different versions of Node.js to work, so having the flexibility to change that is important and will save you a lot of time.
Once completed, you will be ready to start building Node.js applications!
Install/Verify System Packages
Before going through these steps, open a new terminal (CTRL
+ALT
+T
) and navigate to the home directory of your machine:
cd ~
First, let's update the system packages on your machine:
apt-get update
When that finishes, run the following command to install the build-essential
package if you don't already have it:
apt-get install build-essential
Install Nvm (Node Version Manager)
Now we are ready to install the nvm
package. Use one of the two (curl
or wget
) commands below to run the nvm
bash
script.
At the time of publication, NVM v0.34.0
was the most recent version available. Make sure you check the NVM Github project page to get the latest version and replace the yellow text in the commands below.
curl
command:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh | bash
Or use the wget
command:
wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh | bash
The bash
script clones the nvm
repository to ~/.nvm
and adds the source line to your profile (~/.bash_profile
, ~/.zshrc
, ~/.profile
, or ~/.bashrc
).
Reload the Shell
After running the above command, you will receive an output message similar to the following in your terminal:
...
=> Close and reopen your terminal to start using nvm or run the following to use it now:
...
You can reload the shell by running:
source ~/.bashrc
Or simply open a new Terminal window/tab.
Verify the Installation
To verify that nvm
was installed correctly, use this command:
nvm --version
You should see an output with the version of nvm
you downloaded.
Now let's install the latest Node.js stable release (replace v10.16.0
with the version you wish to install):
You now have Node.js installed!
Useful Nvm Commands
For the last part of this, we will quickly go over a few useful commands you can use with nvm
.
See What Versions Are Installed
To see what versions of Node.js have been installed in the past, use this command:
nvm ls
Install Specific Version of Node.js
To install a specific version of Node.js, use this command (replace the yellow text with the version you want to install):
nvm install v10.16.0
Change The Default Node.js Version To Use
If you want to change the default Node.js version later, you can run this command to adjust it:
nvm alias default v11.16.0
You can also select what Node.js version is used on a per-project basis. This can be done by running nvm use v10.16.0
in the directory where your project resides.
Delete Node.js Versions
To delete any version of Node.js you have downloaded with nvm
, use this command (replace v10.15.3
with the version you wish to delete):
nvm uninstall v10.15.3
Conclusion
You now have Node.js installed and it's ready for you to use on your Ubuntu machine!
Thanks for reading and happy coding!