How to Install and Use NVM on Ubuntu 20.04 & CentOS 8

Install NVM Ubuntu CentOS

NVM also know as “Node Version Manager” is used to manage and control multiple active versions of Node.js in Linux. It is a command-line tool that allows developers to easily switch between different versions of Node.js. NVM is very useful when your application does not support the latest Node.js version.

You should also read Three way to install Node.js and NPM on Ubuntu 20.04.

In this tutorial, you will learn –

Prerequisites

  • A server running Ubuntu 20.04.
  • A root password is configured on your server.

How to Install NVM on Ubuntu 20.04

By default, NVM package is not available in the Ubuntu 20.04 default repository. So you will need to install NVM using the script. First, install the curl with the following command:

apt-get install curl -y

Next, download and run the NVM installation script with the following command:

curl https://raw.githubusercontent.com/creationix/nvm/master/install.sh | bash 

This will install the NVM in your system as shown below:

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 14642  100 14642    0     0  70734      0 --:--:-- --:--:-- --:--:-- 70734
=> Downloading nvm as script to '/root/.nvm'

=> Appending nvm source string to /root/.bashrc
=> Appending bash_completion source string to /root/.bashrc
=> 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

Next, activate the environment variable for NVM with the following command:

source ~/.profile 

Next, verify the installed version of NVM using the following command:

nvm --version

You should see the following output:

0.37.2

How to Install NVM on CentOS 8

First, install the curl command-line tool with the following command:

dnf install curl -y

Next, download and run the NVM installation script with the following command:

curl https://raw.githubusercontent.com/creationix/nvm/master/install.sh | bash 

This will install the NVM in your system as shown below:

Next, activate the environment variable for NVM with the following command:

source ~/.bash_profile

Next, verify the installed version of NVM using the following command:

nvm --version

You should see the following output:

0.37.2

How to Install Node.js using NVM

NVM allows you to install multiple Node.js versions in your system. So you can use your desired Node.js version with your application.

To list all available Node.js versions, run the following command:

nvm list-remote

You should get a list of all Node.js versions in the following output:

      v14.10.0
       v14.10.1
       v14.11.0
       v14.12.0
       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   (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

Next, run the “nvm install node” command to install the latest version of Node.js:

nvm install node

If you want to install Node.js version v14.15.4, run the following command:

nvm install v14.15.4

Switch Between Node.js Versions Using NVM

If you are a developer then you may often need to switch between Node.js versions. In this case, you can use NVM to change the default Node.js version.

First, list all installed Node.js versions in your system using the following command:

nvm ls

You should see the following output:

->     v14.15.4
        v15.8.0
default -> node (-> v15.8.0)
iojs -> N/A (default)
unstable -> N/A (default)
node -> stable (-> v15.8.0) (default)
stable -> 15.8 (-> v15.8.0) (default)
lts/* -> lts/fermium (-> v14.15.4)
lts/argon -> v4.9.1 (-> N/A)
lts/boron -> v6.17.1 (-> N/A)
lts/carbon -> v8.17.0 (-> N/A)
lts/dubnium -> v10.23.2 (-> N/A)
lts/erbium -> v12.20.1 (-> N/A)
lts/fermium -> v14.15.4

Now, check the default Node.js version using the following command:

nvm current

Output:

v14.15.4

Or

node --version

Output:

v14.15.4

Now, change the default Node.js version to v15.8.0, run the command below:

nvm use v15.8.0

Output:

Now using node v15.8.0 (npm v7.5.1)

If you want to uninstall specific Node.js version, run the following command:

nvm uninstall node-version

Conclusion

In the above guide, you learned how to install NVM on Ubuntu 20.04 and CentOS 8. You also learned how to use NVM to manage different Node.js versions.

About Hitesh Jethva

I am Hitesh Jethva Founder and Author at LinuxBuz.com. I felt in love with Linux when i was started to learn Linux. I am a fan of open source technology and have more than 15+ years of experience in Linux and Open Source technologies.

View all posts by Hitesh Jethva