How to install Node.js and npm using Node Version Manager (NVM)

Node.js & npm

Vinojan Veerapathirathasan
5 min readMay 28, 2022

Installing Node.js and npm using the NVM

An alternative for installing Node.js is to use a tool called nvm, the Node Version Manager (NVM). Rather than working at the operating system level, nvm works at the level of an independent directory within your home directory. This means that you can install multiple self-contained versions of Node.js without affecting the entire system.

NVM (Node Version Manager) is a bash script used to manage multiple active Node.js versions. With NVM you can install and uninstall any specific Node.js version you want to use or test.

To install Node.js and npm using NVM on your Ubuntu system, perform the following steps:

1. Installing NVM (Node Version Manager) script

To download and install the nvm script run:

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

The command above will clone the NVM repository from Github to the ~/.nvm directory:

Output=> Close and reopen your terminal to start using nvm or run the following to use it now:

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion

Note :
If you are getting “curl: command not found” error in the terminal.

Installation of Curl Utility on Debian

Curl can be installed from the official APT package repository of the Debian 10 Operating system.

First, update the system’s repository cache using the command given below:

✔ sudo apt update

Now, upgrade the installed packages as well, using the command provided below:

✔ sudo apt upgrade

After successfully updating and upgrading the system. Type the command given below to install the Curl on your Debian 10 system.

✔ sudo apt install curl

The installation of Curl library will start to download, and it will be installed in a moment.

After the installation, you can run the NVM installation command…👍
— ⭕

2. Restart your terminal

In order to pick up the changes to your profile either close and reopen the terminal

3. Verify it worked

verify that nvm was properly installed by typing:

nvm --version
Output
0.34.0

4. See what it does

run the nvm ls-remotecommand to get a list of all the available versions.

Output       v14.13.0
v14.13.1
v14.14.0
v14.15.0 (LTS: Fermium)
v14.15.1 (LTS: Fermium)
v14.15.2 (LTS: Fermium)
v14.15.3 (LTS: Fermium)
v14.15.4 (LTS: Fermium)
v14.15.5 (LTS: Fermium)
v14.16.0 (LTS: Fermium)
v14.16.1 (LTS: Fermium)
v14.17.0 (LTS: Fermium)
v14.17.1 (LTS: Fermium)
v14.17.2 (LTS: Fermium)
v14.17.3 (LTS: Fermium)
v14.17.4 (LTS: Fermium)
v14.17.5 (LTS: Fermium)
v14.17.6 (LTS: Fermium)
v14.18.0 (LTS: Fermium)
v14.18.1 (LTS: Fermium)
v14.18.2 (LTS: Fermium)
v14.18.3 (LTS: Fermium)
v14.19.0 (LTS: Fermium)
v14.19.1 (LTS: Fermium)
v14.19.2 (LTS: Fermium)
v14.19.3 (Latest LTS: Fermium)
v15.0.0
v15.0.1
v15.1.0
v15.2.0
v15.2.1
v15.3.0
v15.4.0
v15.5.0
v15.5.1
v15.6.0
v15.7.0
v15.8.0
v15.9.0
v15.10.0
v15.11.0
v15.12.0
v15.13.0
v15.14.0
v16.0.0
v16.1.0
v16.2.0
v16.3.0
v16.4.0
v16.4.1
v16.4.2
v16.5.0
v16.6.0
v16.6.1
v16.6.2
v16.7.0
v16.8.0
v16.9.0
v16.9.1
v16.10.0
v16.11.0
v16.11.1
v16.12.0
v16.13.0 (LTS: Gallium)
v16.13.1 (LTS: Gallium)
v16.13.2 (LTS: Gallium)
v16.14.0 (LTS: Gallium)
v16.14.1 (LTS: Gallium)
v16.14.2 (LTS: Gallium)
v16.15.0 (Latest LTS: Gallium)
v17.0.0
v17.0.1
v17.1.0
v17.2.0
v17.3.0
v17.3.1
v17.4.0
v17.5.0
v17.6.0
v17.7.0
v17.7.1
v17.7.2
v17.8.0
v17.9.0
v18.0.0
v18.1.0
v18.2.0

5. Install the latest LTS release of Node.js and npm

Now that you’ve got nvm installed let’s use it to install, and use, the current LTS version of Node.js.

nvm install --lts
Output
Now using node v16.15.0 (npm v8.5.5)
Creating default alias: default -> lts/* (-> v16.15.0)

Verify it worked, and that the version is correct:

✔ node --versionOutput
v16.15.0
--------------------------------------------------------------------✔ npm --versionOutput
8.5.5

Set the default version of the node

If you have multiple Node versions installed, you can run ls to get a list of them:

✔ nvm lsOutput->     v16.15.0
default -> lts/* (-> v16.15.0)
node -> stable (-> v16.15.0) (default)
stable -> 16.15 (-> v16.15.0) (default)
iojs -> N/A (default)
unstable -> N/A (default)
lts/* -> lts/gallium (-> v16.15.0)
lts/argon -> v4.9.1 (-> N/A)
lts/boron -> v6.17.1 (-> N/A)
lts/carbon -> v8.17.0 (-> N/A)
lts/dubnium -> v10.24.1 (-> N/A)
lts/erbium -> v12.22.12 (-> N/A)
lts/fermium -> v14.19.3 (-> N/A)
lts/gallium -> v16.15.0

You can also default to one of the versions:

✔ nvm alias default 16.15.0

Output
default -> 16.15.0 (-> v16.15.0)

This version will be automatically selected when a new session spawns. You can also reference it by the alias like in the following command:

✔ nvm use default
Output
Now using node v16.15.0 (npm v8.5.5)

Each version of Node will keep track of its own packages and has npm available to manage these.

Removing Node.js

You can uninstall Node.js using apt or nvm, depending on the version you want to target. To remove the default repository version, you will use apt at the system level. This command removes the package and retains the configuration files. This is useful if you plan to install the package again in the future:

✔ sudo apt remove nodejs

If you don’t want to save the configuration files for later use, then run the following command to uninstall the package and remove the configuration files associated with it:

sudo apt purge nodejs

As a final step, you can remove any unused packages that were automatically installed with the removed package:

✔ sudo apt autoremove

To uninstall a version of Node.js that you have enabled using nvm, first determine whether or not the version you would like to remove is the currently active version:

✔ nvm current

If the version you are targeting is not the current active version, you can run:

✔ nvm uninstall node_version
Output
Uninstalled node node_version

This command will uninstall the selected version of Node.js.

If the version you would like to remove is the current active version, you must first deactivate nvm to enable your changes:

✔ nvm deactivate

Now you can uninstall the current version using the uninstall command used previously. This removes all files associated with the targeted version of Node.js except the cached files that can be used for reinstallment.

I hope y’all understand and enjoy my article. Keep in touch by following my page to receive more updates.

Before moving on to check out another article, please take a moment to follow my page if you aren’t already. Your follow helps support my work in creating more helpful content for the Tech community.

✍️
Vinojan Veerapathirathasan
Founder and CEO at DecHorizon
LinkedIn : https://www.linkedin.com/in/imvinojanv/
Github : https://github.com/imvinojanv
vinojan@dechorizon.com | +94 77 573 7782

Thank You …!

--

--

Vinojan Veerapathirathasan

Software Engineer at EL | Designer | Medium Writer | AI Enthusiast | Entrepreneur | Specializing in Modern Web Application Development