How to Install NVM on Ubuntu 22.04?


NVM (Node Version Manager) is a cross-platform tool to manage the NodeJS versions on your system. In Ubuntu (or any other system), developers have to deal with multiple projects that are compatible with different node versions. NVM allows you to install variants of NodeJS on one machine so that developers can run applications to bypass the node’s version conflict. Moreover, NVM allows you to switch between numerous node versions instantly. Apart from that NVM also us to switch between versions or uninstall the unnecessary/useless version.

So, today’s guide will instruct you on how you can install, and manage NVM on Ubuntu 22.04.

How to Install NVM on Ubuntu 22.04?

The NVM installation script is embedded into the system using the curl or the wget tool. First, install these (or anyone of these) prerequisites using the command:

$ sudo apt update && sudo apt install curl wget

Get the latest installation script of NVM from the following link and download it on your system via the curl/wget tool:

Using Curl Tool:

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

The path where the NVM script is downloaded is “/home/username/.nvm”, as highlighted in the above output.

Note: The manual path configuration script is provided at the end of the output which is to be used only if you do not restart the terminal (or do not source your “~/.bashrc” file). If you restart the terminal (or source your bashrc file), these lines will automatically be placed in the “~/.bashrc” file.

Using the wget Tool:

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

Sourcing/Refreshing the bash environment:

$ source ~/.bashrc

Verify the installation via the command:

$ nvm --version

 

How to Use NVM on Ubuntu 22.04?

The NVM has its own set of commands/options to deal with Node on Ubuntu 22.04. Let’s see the supported options of NVM using the command:

$ nvm --help

Note: You can use the “nvm man” command to get the detailed usage of the NVM.

Before getting into the NVM usage, let’s check the Node version installed on our system, via the command:

$ node --version

The version “12.22.9” is installed on the system.

NVM | How to Check the Node Versions on Ubuntu 22.04?

NVM can be used to check the available, installed, and in-use NodeJS versions on Ubuntu 22.04. All these functionalities are illustrated through the commands demonstrated below:

  • Check the Available Versions of Node
$ nvm list-remote

In between the versions, you will also get the LTS releases. The version numbers that an LTS lasted for are also mentioned:

The latest LTS of the “Iron” release is “20.10.0”:

  • Check the Node Versions Installed on Ubuntu 22.04
$ nvm ls

Only the system-managed Node version is installed (which is in green shade).

  • Check the Current Node Version in Use
$ nvm current

The system-installed node is in use currently.

NVM | How to Install the NodeJS on Ubuntu 22.04?

As NVM’s core purpose is to manage/install different versions of NodeJs. Here is the list of NVM commands that specifically refer to the installation of various Nodes:

  • Install a Specific Node Version
$ nvm install 20.10.0

Replace the version that you want to install. Moreover, NVM creates a “default” alias at the time of installation which sets the newly installed NodeJS version to “default”.

  • Install the Latest Version (Not Stable)
$ nvm install node

  • Install the Node LTS
$ nvm install --lts

  • Install the Specific LTS Release
$ nvm install lts/hydrogen

The latest LTS of the “Hydrogen” release is now installed. The LTS codenames of NodeJS are “ iron, hydrogen, gallium, ferium, erbium, dubnium, carbon, boron, and argon”.

  • Install the Latest Stable Version (It is not the LTS)
$ nvm install stable

  • Install and Migrate Packages From Other/Older Node Version

The “–reinstall-packages-from” flag is used with the “install” flag to install the mentioned version (LTS in this case) by copying the packages from the specific version (LTS/fermium):

 

$ nvm install --lts --reinstall-packages-from=14.21.3

Note: Ensure that the “14.21.3” (or whichever version you are referring to) is installed already on your system.

NVM | How to Switch NodeJS Versions?

NVM has made the switching between multiple versions easy and effective. Let’s see how:

  • Switch the Current Node Version
$ nvm use 14.21.3

  • Switch to Latest Version
$ nvm use node

  • Switch to LTS (Long Term Support)
$ nvm use --lts

  • Switch to the system-installed NodeJS
$ nvm use system

NVM | How to Manage Aliases in Ubuntu 22.04?

Alias is a user-defined name that maps with any NodeJS version. It can be set using the nvm command and whenever that Alias is called, that mapped NodeJS version is loaded into the system:

  • Create an Alias
$ nvm alias mostused 21.4.0

  • Use the Alias
$ nvm use mostused

  • Remove an Alias
$ nvm unalias default

Now, the system won’t recognize the version set against the alias named “default”.

NVM | How to Deactivate the NodeJS on Ubuntu 22.04?

  • Deactivate the Loaded NodeJS
$ nvm deactivate

  • Uninstall the Specific Node Version
$ nvm uninstall 20.10.3

NVM | Run an Application Via the Specific NodeJS

$ nvm exec [Node-Version] [Command]

 

How to Uninstall NVM on Ubuntu 22.04?

The NVM is installed on the system via the installation script. By default, the NVM script is downloaded to the directory “/home/username/.nvm” (check it when installing/downloading the script). Let’s learn the recommended way of removing the NVM:

Switch to the System Defined NodeJS

$ nvm use system

Now, use the below command to remove the directory where the NVM script was downloaded:

$ sudo rm -rf ~/.nvm

Reload the shell and verify the removal via the command:

$ nvm --version

That’s how NVM manages NodeJS on Ubuntu 22.04.

 

Bottom Line

NVM is the wholesome version manager for NodeJS with advanced support on Ubuntu 22.04 (and other Linux distros). A NodeJS developer has to build and configure various projects supported on multiple versions of Node. Thus, NVM must be there in such a scenario.

This post has explained the installation of NVM on Ubuntu 22.04 alongside the advanced usage of how you can install, switch, and delete various Node versions.

Print Friendly, PDF & Email
Categories