Steps to Install NVM on Ubuntu

Install NVM on Ubuntu to Use Multiple Versions of Node.js

Using multiple versions of Node.js and switching between them requires NVM installed on your system. You might need to do a project that specific Node.js versions are its prerequisites. So, let’s learn How to Install NVM on Ubuntu. NVM supports both Linux and macOS and works on all operating systems. On Linux, the NVM, also known as ”Node Version Manager” provides a command line tool to let developers install and manage multiple Node.js versions.

Working on multiple projects is easy when you can access different versions of Node.js. How it works? When your project requires a newer version, it is not possible to install the most recent version of Node.js and utilize it for all of your projects without breaking the older ones. Furthermore, installing the previous version and using it for all of your projects will break the new ones. NVM is here to manage this situation, and this article covers the way it works and the process of installing it in your Ubuntu terminal.

What is NVM, and Why Use it?

NVM is the acronym for Node Version Manager. As the name implies, managing and switching between several Node versions is simple. As a powerful tool, it has a command-line interface that lets you do many things, such as establish a default, switch between versions, and install several versions with a single command.

Knowing about Node Features helps to know that Node evolves frequently, and it can be challenging to test apps using different versions. Version compatibility testing and updates with different libraries are significantly simpler because NVM makes it possible to move between Node versions quickly and easily. Sudo is not required because NVM saves the Node versions and related modules inside your user directory. NVM facilitates installation and compilation procedures by eliminating the need to receive Node versions straight from the distribution channel.

It is possible that even though you have the most recent version of Node installed on your computer, you must downgrade since the project you are going to work on calls for an earlier version. It could also be useful to be able to switch between the two while you’re upgrading an outdated Node project to a more recent one. To install the necessary Node version, would need a lot of time and work being spent manually uninstalling and reinstalling Node versions and their global packages without a decent tool. Thankfully, there is an improved method: Install NVM on your in-use operating system.

Prerequisites to Install NVM on Ubuntu 20.04 & 22.04

Before you jump in, ensure your PC meets the following requirements and get prepared for NVM install Ubuntu 20.04 and Ubuntu 22.04.

  • A Server running Ubuntu VPS.
  • A non-root user with sudo privileges.
  • Log in with a user account, to which you need to install node.js.
  • curl Package Installed:
  • Check if installed: dpkg -s curl
  • If it is not installed, run: sudo apt install curl

Tutorial Install and Use NVM on Ubuntu

Let’s go through the steps of this guide and learn How to Install NVM on Ubuntu and Use Multiple Versions of Node.js. In the end, you will be able to manage and switch between several Mode.js versions using the NVM.

Step 1. Install NVM

Since NVM is not in the default repository of Ubuntu, it must be installed from the script. Launch a terminal on your computer or use SSH to connect to a distant system. After installing curl on your computer using the aforementioned steps, launch the NVM installer script.

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

NVM will be installed by running the previously mentioned command, which also modifies the .bashrc file with all necessary environment variables. Also, an environment entry is created to the current user’s login script by the nvm installer script. To load the environment, you can either log out and back in again or use the command below.

source ~/.bashrc

All you need for NVM download Ubuntu is included in this step. The above command activates all settings to let your Ubuntu system successfully finish installing NVM. So, you can use the following command to verify the NVM version:

nvm –version

Note: NVM uses the NVM_DIR, NVM_BIN, NVM_INC, NVM_CD_FLAGS, and NVM_RC_VERSION environmental variables. We advise leaving them in their default configurations. If you do decide to alter them for any reason, proceed with caution.

Step 2. Install Node.js Using NVM

Install NVM on Ubuntu is successfully done by now. So, you are ready to install different versions of node.js. and make use of the node.js installed version that is needed for your application.

To start using NVM, try the command below to install the latest version of Node.js:

nvm install node

Also, you can run the following command to install your considered version of node:

nvm install 20.9.0

Replace 20.9.0 with the specific version number you need. To install the latest LTS (Long Term Support) version of Node.js, type:

nvm install node --lts

With that previous command, you can select any other version to install. The default version is the one that was installed initially. The default version of the node (for example, nvm alias default) will be used by new shells.

Use the --lts option in conjunction with the desired release line name to install a specific LTS release line (a version other than the most recent version).

nvm install --lts=argon

The “argon” LTS line of Node.js is installed by using the above example command. Use the following command to switch to the most recent LTS version of Node.js that is already installed on your system:

nvm use --lts

Run the following command to switch to a specific LTS version of Node.js:

nvm use lts/argon

In this way, the example command switches to the “argon” LTS line of Node.js. Again, using the nvm –version command, you can see the final version result of the installed NVM on your Ubuntu system.

Step 3. Create NVM Aliases

Using a different or more easily remembered name for a Node version may be more convenient for you. Some pre-made defaults, such default as and node, which refers to the most recent version, are already provided by NVM. However, you can make a new alias or modify the value of an existing one with the alias command.

Altering the Node version by default can be done with the nvm alias command. The default alias and the updated Node version it should reference should come after the alias keyword.

nvm alias default 20

To make a new alias, run the nvm alias command. A new maintenance alias, designated as Node version 18.16.0, is defined in the example below.

nvm alias maintenance 18.16.0

The command below allows you to see every alias, both old and new.

nvm ls

Step 4. Manage Node.js Versions Using NVM

To work with NVM and Switch between Versions of Node.js, first list all installed Node.js versions by running the command below:

nvm ls

To find all the available Node.js versions, type:

nvm ls-remote

You can set your default Node.js version to 20.9.0. The command below allows you to select a specific version for the current session:

nvm use 20.9.0

In this way, the version that is chosen will be the one that is only active for the current shell. Then, run the following command to find the default version for the current user:

nvm run default --version

To launch a Node.js version-specific Node.js application, run:

nvm run 20.9.0 app.js

Use the command below to run a Node script with a considered version of node.js:

nvm exec 20.9.0 server.js

Step 5. Quick Switch Node Version

In this part, after a successful Install NVM on Ubuntu process, you will learn how to use NVM run to switch to a different version of Node and launch a Node console right away. In every other way, using the run command is extremely identical to using NVM.

nvm run node

That’s it! Hopefully, you can now use any version of Node.js to execute your application. NVM can also be manually downloaded and installed, or it can be installed using GIT. For comprehensive procedures, and to view a full list of all advanced topics, refer to the NVM Documentation Guide‘s GIT section.

NVM Advanced Capabilities

You can migrate packages from an earlier Node version using NVM. To install a fresh version of Node with the packages from a previous release, use the nvm install command together with the optional -reinstall-packages-from= argument. In addition to installing the most recent version of Node, the aforementioned command links and reinstall the default Node package set.

nvm install node --reinstall-packages-from=default

You can choose unique colors for the different Node versions and aliases to be shown thanks to NVM. The NVM GitHub page defines these colors. Apply the nvm set-colors <color-key> command. A command that has the --no-colors flag added to it displays data in black and white.

nvm set-colors rgBcm

The .nvmrc file also allows you to set the default Node version number. You can find this file in any parent directory or the root directory. If no version is supplied on the command line, the version found in .nvmrc is utilized.

Quick NVM Uninstall on Ubuntu

So far, you learned how to install and use NVM on Ubuntu. Anytime you prefer to remove NVM, you must follow the instructions in this part:

Use the nvm uninstall command and the version number to remove a particular Node.js version. The command nvm ls can be used to find the installed versions.

So, to remove Node.js version 20.9.0, type:

nvm uninstall 20.9.0

Uninstall NVM in Ubuntu is as simple as you reviewed.

Conclusion

In this article, you learned How to Install NVM on Ubuntu to manage different versions of Node.js. NVM is a useful tool for developers working on different projects because it allows you to move between different Node.js versions with ease.

It is possible to Install Node.js using NVM Ubuntu. So, if you are interested, refer to our guide. However, by following the above steps properly, you can smoothly install NVM on Ubuntu without any errors. But do not hesitate to contact us if you encounter any problems. Our technical support team will try their best to solve your problems.

Leave a Reply

Your email address will not be published. Required fields are marked.