How to Install and Manage Node.js via NVM

Channel: Linux
Abstract: You can see that Node.js version v16.14.0 is set as the default version. You can change the default Node.js version. The below command will set 17.5.0

NVM is known as Node Version Manager, similarly to RVM (Ruby Version Manager) for Ruby language. NVM provides an option for the easy installation of Node.js. You can also install a specific Node.js version or multiple Node.js versions on the same system using nvm and use the required version for the application. This tutorial will help you to Install and Manage Node.js using NVM.

The default NVM is installed under current users home directory, So nvm installation with one user will not be accessible to another user. Windows users can visit our other tutorial to install Nodejs on Windows system.

Step 1 – Install NVM

NVM is a command-line utility to install and manage Node.js versions for specific users. You can install nvm using a shell script provided by the nvm team.

  1. First, make sure you have curl installed on your system:
    sudo apt update && sudo apt install curl -y
    
  2. Next, run the following command to configure nvm on your system for current logged user.
    curl https://raw.githubusercontent.com/creationix/nvm/master/install.sh | bash 
    
  3. Reload system environment using this command. It will set the required environment variables to use nvm on the system.
    • Debian based systems:
      source ~/.profile 
      
    • CentOS, Fedora and Redhat systems:
      source ~/.bashrc  
      
Step 2 – Find Available Node.js Version

At this point, you have installed nvm on your system for the current user. Now find out the available version of Node.js to install. Use ls-remote option to list versions.

nvm ls-remote 

You will see a long list of available versions.

Output:
       ...
       ...
       v16.12.0
       v16.13.0   (LTS: Gallium)
       v16.13.1   (LTS: Gallium)
       v16.13.2   (LTS: Gallium)
       v16.14.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
Step 3 – Installing Node.js with NVM

Now install the node.js version you need to use for running the node.js application. Below command will install node.js 16.14.0 the LTS release on your system.

nvm install v16.14.0 

You can have also installed the latest version of Node.js.

nvm install 17 

Repeat the above command with the different-2 node.js versions to install multiple versions of node.js on your system.

Step 4 – Set A Default Version

As you have installed multiple node.js versions, You can select the specific version of node.js as the default version used by the system and load it into the environment. Use the below command to list the currently installed version and default set version.

nvm list 
Output:
       v16.13.2
->     v16.14.0
        v17.5.0
         system
default -> 16 (-> v16.14.0)
iojs -> N/A (default)
unstable -> N/A (default)
node -> stable (-> v17.5.0) (default)
stable -> 17.5 (-> v17.5.0) (default)
lts/* -> lts/gallium (-> v16.14.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.10 (-> N/A)
lts/fermium -> v14.19.0 (-> N/A)
lts/gallium -> v16.14.0

You can see that Node.js version v16.14.0 is set as the default version. You can change the default Node.js version. The below command will set 17.5.0 as the default Node.js version.

nvm use 17.5.0 

Now verify current active version of node.js

node --version 

v17.5.0
Step 5 – Run Application with Specific Version

If you have multiple node.js applications on your system and want to run each with a specific version of node.js. NVM provides you an option to use a node.js version for running any application. For example

nvm run v16.14.0 app.js 
Step 6 – Remove Unused Node.js Version

This command will provide a list of installed versions of node.js on your system.

nvm list 

You can remove any unused version from your system. For example, to remove Node.js version 9.5.9 execute the following command:

nvm uninstall v9.5.0 
Conclusion

This tutorial helped you to install and configure multiple Node.js versions using NVM. This guide also describes you to switch the default version with the nvm utility and instructions to use different versions during the run time.

Ref From: tecadmin

Related articles