Install Node.js using NVM on Ubuntu

In this tutorial we’ll learn to install Node.js using NVM (Node Version Manager) on Ubuntu (or any Debian based Linux). Here, we’ve used Ubuntu 18.04 but these instructions should work in other versions too.

Usually default versions in Linux distro repositories are old ones. Using NVM we can install latest versions of Node.js and can even manage multiple versions of Node.js easily. So, let’s install Node.js on Ubuntu 18.04 using NVM.

First remove the default Node.js, if installed from distro repositories, by entering following commands;

sudo apt remove nodejs
sudo apt purge nodejs
sudo apt autoremove

Now, download and install the NVM script by entering following command;

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash

NVM is installed!

Next, logout and re-login or enter following command to avoid this logout-login step;

source ~/.profile

Now, you can go ahead and install any Node.js version as you like using ‘nvm install’ command. But first, let’s go over the basics of using NVM.

To get the list of commands of NVM, enter following command;

nvm help

To check locally installed versions, enter;

nvm ls

To check available Node.js versions which can be installed, enter;

nvm ls-remote

To install the latest Node.js version, enter;

nvm install node

To install the latest LTS (Long Term Support) version of Node.js, enter;

nvm install --lts

To install any specific version of Node.js, enter;

nvm install 14

Where ’14’ is the Node.js version number which is to be installed.

To print the installed and active version of Node.js, enter;

node -v

To switch between installed versions of Node.js, enter;

nvm use 16

Where ’16’ is the version number of Node.js to be activated.

Remember to use ‘nvm help’ in order to learn further about NVM commands.

Leave a Comment