How to Install NVM on Ubuntu: Step-by-Step Tutorial

 

NVM (Node Version Manager) is a powerful tool for developers seeking flexibility in managing multiple Node.js versions on their Linux machines. This command-line utility simplifies the Node.js installation process and control of various Node.js versions, allowing developers to switch between them easily based on specific project requirements. 

Also, NVM capability proves invaluable when working on different projects that demand different Node.js versions. It allows you to manage different Node.js versions with ease. For those using the Linux environment, the installation process of NVM on Ubuntu is straightforward and can be completed in a few steps. 

In this guide, we will demonstrate how to install NVM on Ubuntu 22.04 (Jammy JellyFish) distribution. In addition, you will learn how to install and manage various Node.js versions effectively using NVM.

Prerequisites

To install and run NVM (Node Version Manager), you need to have the following prerequisites:

  1. The Ubuntu Linux distribution should be installed and running on your system or inside a Virtual machine.
  2. You should have the root or “sudo” command privileges to install NVM on a Linux system.

Installing NVM on Ubuntu 

NVM is a bash script that helps you install and manage multiple node versions of Node.js on your Linux system. So, if you want to use a specific node version of Node.js and npm, using the NVM bash script is a good choice for you.

Steps to Install NVM on Ubuntu 22.04

To install NVM (Node Version Manager) on the Ubuntu distribution, you need to follow the steps given below:

Step 1: Install the Curl Package

First, open the terminal from the menu bar or quickly launch it using “Ctrl + Alt + t.” Download the bash or auto-install script for NVM using the “wget” or “curl” command line tools. 

If you are using curl, it may not install on the Ubuntu distribution by default. However, you can install curl using the apt package manager by running the following command:

$ sudo apt-get install curl

Step 2: Install NVM on Ubuntu

The NVM (Node Version Manager) package is not included in Ubuntu's default apt repository. However, you can install it using the NVM script. After installing curl, download the NVM installation script using the “wget” or “curl” commands as listed below:

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

OR

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

The above command installs the script and clones the NVM repository to ~/.nvm in your system’s home directory. It will also add required source commands to your shell startup scripts in the ~/.bashrc file. The following output will display inside your terminal.

Step 3: Source the ~./bashrc File

Once the NVM script installation is complete, close the terminal window in which you were working. Launch the terminal window again and source the ~./bashrc file to load the NVM or activate all settings on your Ubuntu system using the following command:

$ source ~/.bashrc

OR

$ source ~/.profile

Step 4: Check NVM Version

In this step, you will verify whether the NVM has been installed on your Ubuntu system or not. To verify it, display the installed NVM version on the terminal. You can check the NVM version by running the following command:

$ nvm –version

That’s all on how to install NVM on Ubuntu. NVM installation offers several advantages, such as enhanced compatibility with various Node.js versions and increased flexibility in workflow for developers. If you are using Host-World VPS service, you can install NVM and test the installation using these instructions easily.

In the rest of the article section, we will learn how to install Node.js and manage different Node versions on the Ubuntu system using NVM(Node Version Manager).

Install Node.js Using NVM (NVM Install Node)

The installation of NVM on your system is now complete. Thus, you can install any desired Node.js on your Linux system using NVM. To check all available Node.js versions that you can install on your system using NVM, use the following command:

$ nvm ls-remote

The NVM node versions list will be displayed on your terminal. You can choose and install any Node.js version from the list shown above.

Install the Latest Node.js Version Using NVM 

You can install the latest Node.js version on Ubuntu using NVM. To install the latest Node version, execute the below-given command:

$ nvm install node

After running the above command, the following output should display on the terminal:

Check the installed Node.js version by executing the following command:

$ node -v

$ npm -v

Install the Latest Stable Node.js Version Using NVM

To install the most recent stable version of Node.js, execute the following command:

$ nvm install node --lts

You will see the following output:

Install a Specific Node.js Version with NVM (NVM Install Node Version)

You can also install any specific Node.js version based on your project requirements. To install a specific Node.js version, use the NMV command and specify the version that you want to install on your system.

$ nvm install v20.9.0

After successfully installing a Node.js version with NVM, the system should now reference the Node.js and npm versions managed by NVM. To check the location of the Node and npm binary or display the path of executables, you can use the “which” command. Run the following command to display this information:

$ which node

$ which npm

Manage Node Versions Using NVM

To list all installed Node.js versions on your system, use the following command:

$ nvm ls

If you want to set a default version for Node.js from all the installed versions you have, you can do so by using this command:

$ nvm use v20.9.0

or

$ nvm alias default 20.9.0

It's important to note that when switching between various Node.js versions, any globally installed npm packages may not function correctly. This is due to the dependency issues of each Node.js version installation. 

For instance, if you've installed a global package while using version 20.9.0, switch to version 21.2.0. You need to reinstall the global package specifically for the new version.

To execute a Node application with a specific Node.js version, use the following command:

$ nvm run v20.9.0 testapp.js

Replace "v20.9.0" with the desired Node.js version and "testapp.js" with the entry point file of your Node application. This command ensures that the specified Node.js version is used for running the application.

Uninstall the Node.js Version Using NVM (NVM Uninstall)

To uninstall or remove a specific Node.js version, use the following command (ensure to replace 20.9.0 with the desired version number):

$ nvm uninstall 20.9.0

To manually uninstall NVM from your system, execute the following commands:

$ rm -rf "$NVM_DIR" 

The above command will remove the NVM directory from your system.

Alternatively, you can also remove the directory ~/.nvm using the following commands:

$ rm -rf ~/.nvm

Next, edit the configuration file for your shell profile (e.g., ~/.bashrc) and delete the following lines:

export NVM_DIR="$HOME/.nvm"

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

&& \. $NVM_DIR/bash_completion

Make sure to replace ~/.bashrc with the appropriate file if you are using a different shell profile.

What Are the Key Benefits of Using NVM on Linux?

Node Version Manager (NVM) comes with a set of features that make it a valuable tool for developers. It allows you to install and switch between different Node.js versions on your computer easily. 

One standout NVM feature is the ability to associate a particular Node.js version with each project. This ensures that every project can use the Node.js version it requires, preventing conflicts between different projects.

NVM also offers the flexibility to set both global and local Node.js versions. You can set a global version for system-wide use and a local version for individual projects, tailoring your environment to each project's needs.

Moreover, switching between installed Node.js versions is a good option with NVM, making it convenient for developers juggling projects with different version requirements. NVM seamlessly integrates with the Node Package Manager (NPM), ensuring consistent management of packages and dependencies across different Node.js versions.

In addition, the installation and removal of specific Node.js versions are straightforward with NVM. It provides shell integration, offering commands and scripts that simplify Node.js version management directly from the command line. 

Furthermore, NVM is not limited to Linux distributions. It also supports macOS, providing a cross-platform solution for managing Node.js versions.

Conclusion

In this guide, we learned how to install NVM on Ubuntu 22.04 using a bash script. NVM enables developers to install and manage multiple Node.js versions effortlessly on a single machine. However, even though NVM seems a bit tricky at first, it's actually pretty easy to use, and it can be super helpful for developers, no matter their skill level. 

If you want to get more information related to NVM, use the “nvm --help” command or visit the GitHub repository forNode Version Manager by following this link.

Items from an article
  • KVM NVMe VPS 2 GB
    • CPU 2 x Xeon Core
    • RAM 2 GB
    • HDD 30GB
    • BANDWIDTH 1 Gbps
    • IPv4 / IPv6
    € 9/month
    9/month
  • KVM NVMe VPS 4 GB
    • CPU 2 x Xeon Core
    • RAM 4 GB
    • HDD 50GB
    • BANDWIDTH 1 Gbps
    • IPv4 / IPv6
    € 14/month
    14/month
  • KVM NVMe VPS 8 GB
    • CPU 4 x Xeon Core
    • RAM 8 GB
    • HDD 80GB
    • BANDWIDTH 1 Gbps
    • IPv4 / IPv6
    € 30/month
    30/month
  • KVM NVMe VPS 12 GB
    • CPU 6 x Xeon Core
    • RAM 12 GB
    • HDD 140GB
    • BANDWIDTH 1 Gbps
    • IPv4 / IPv6
    € 60/month
    60/month
Blog company: