NVM or Node Version Manager is a command-line tool for managing multiple Nodejs versions. It’s a POSIX-compliant bash script that allows you to install and manage multiple nodejs versions on your computer.

In this tutorial, I will show you how to install and use NVM for managing multiple Node.js versions on your computer. This guide can be applied to different Linux distributions, including Ubuntu, CentOS, and Debian.

1. Install NVM (Node Version Manager)

Firstly, we’re going to install package dependencies for the nvm installation.

For Ubuntu, you can run the apt command below.

sudo apt install build-essential libssl-dev -y

And for CentOS, you can use the dnf command as below.

sudo dnf group install "Development Tools" -y

After that, download and run the nvm installer script as below.

wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.5/install.sh | bash

<img alt="Install NVM" data-ezsrc="https://kirelos.com/wp-content/uploads/2023/09/echo/1.png650301c8d333d.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="334" loading="lazy" src="data:image/svg xml,” width=”750″>

Once the installation is complete, you need to reload the ‘~/.bashrc’ script.

source ~/.bashrc

Now the nvm installation is completed, and you’re able to run the nvm command.

Check the nvm version as below.

nvm --version

You will be shown the result as below.

<img alt="Check NVM version" data-ezsrc="https://kirelos.com/wp-content/uploads/2023/09/echo/2.png650301c90b789.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="208" loading="lazy" src="data:image/svg xml,” width=”750″>

Next, we’re going to show you the basic usage of nvm for managing the nodejs installation.

2. Check Available NodeJS

To check available nodejs on your local system, you can use the ‘ls’ option.

nvm ls

Also, you can check to list remote versions of nodejs that ready to install.

nvm ls-remote

And to specify nodejs ‘LTS’ version, you can use the ‘–lts’ option.

nvm ls-remote --lts

3. Install NodeJS Version

To install the latest version of nodejs, you can use the following command.

nvm install node

To install a specific version, you can use the version number or using the alias of the version.

Install nodejs version with alias.

nvm install lts/erbium

<img alt="Install a package" data-ezsrc="https://kirelos.com/wp-content/uploads/2023/09/echo/3.png650301c94a12d.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="217" loading="lazy" src="data:image/svg xml,” width=”750″>

Install node js with version number.

nvm install v10.17.0

<img alt="Install specific node version" data-ezsrc="https://kirelos.com/wp-content/uploads/2023/09/echo/4.png650301c9746c1.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="219" loading="lazy" src="data:image/svg xml,” width=”750″>

4. Switch to Different NodeJS Versions

The main advantage of using nvm is that we can switch different nodejs version with the simple command.

Firstly, you can check available nodejs on your local system.

nvm ls

<img alt="Change Node.js version" data-ezsrc="https://kirelos.com/wp-content/uploads/2023/09/echo/5.png650301c9a053d.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="476" loading="lazy" src="data:image/svg xml,” width=”748″>

To switch to the different versions of your default, you can use the ‘use’ option following by the version number or alias.

nvm use v12.13.0

nvm use lts/erbium

<img alt="switch version" data-ezsrc="https://kirelos.com/wp-content/uploads/2023/09/echo/6.png650301c9c8818.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="215" loading="lazy" src="data:image/svg xml,” width=”750″>

5. Create an Alias

This feature allows you to create your alias for your specific nodejs version. And make easier to manage nodejs version as you need a developer.

To create an alias, you can use the ‘alias’ option following by the alias name and the version of nodejs.

nvm alias node10 v10.17.0

<img alt="create alias" data-ezsrc="https://kirelos.com/wp-content/uploads/2023/09/echo/7.png650301ca0bf86.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="536" loading="lazy" src="data:image/svg xml,” width=”750″>

Now check available ‘alias’ using the following command.

nvm alias

And you will get your alias on the list.

6. Uninstall NodeJS version

To remove the specific nodejs version, you can use the ‘uninstall’ option followed by the version number or alias.

nvm uninstall v6.17.1

nvm uninstall lts/carbon

nvm uninstall node10

And the nodejs version that you want to remove has been uninstalled from the system.

<img alt="Uninstall NodeJs" data-ezsrc="https://kirelos.com/wp-content/uploads/2023/09/echo/8.png650301ca3e91d.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="221" loading="lazy" src="data:image/svg xml,” width=”750″>

7. Run Script with Specific NodeJS version

With the nvm, we can directly run a js application with the specific version of nodejs.

This feature features can be very useful if you’re creating or running JS applications with a different version of nodejs.

Run the ‘app.js’ script using the lts version of nodejs ‘lts/erbium’.

nvm run lts/erbium app.js

nvm run v12.13.0 app.js

That’s all about the nvm installation and its basic usage for managing multiple nodejs version on Linux operating system.

Reference