Setting Up NVM on Ubuntu for Managing Multiple Node.js Versions.
Lets swim to decode the insights of title.
Explore More; Exploring the Dynamic World of LVM in Linux
What is NVM, and Why Use it?
Node Version Manager (NVM) simplifies the management and switching of multiple Node versions. With its command-line interface, this powerful tool allows you to effortlessly set defaults, switch between versions, and install multiple 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 diving in, make sure your PC meets the following requirements and prepare for installing NVM on both Ubuntu 20.04 and Ubuntu 22.04.
- A Server running Ubuntu VPS.
- A non-root user with
- 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
Step 1. Install NVM
As NVM isn’t included in Ubuntu’s default repository, you’ll need to install it from the script. Open a terminal on your computer or connect to a remote system via SSH. Once you’ve installed curl using the provided steps, proceed to launch the NVM installer script.
sudo apt install curl curl https://raw.githubusercontent.com/creationix/nvm/master/install.sh | bash
Execute the previously mentioned command to install NVM, which will also modify the .bashrc file with essential environment variables. Additionally, the NVM installer script creates an environment entry in the current user’s login script.
To load the environment, you can either log out and log back in or use the following command.
This step provides everything required for downloading NVM on Ubuntu. The above command enables all settings for a successful NVM installation on your Ubuntu system.
You can then use the following command to verify the NVM version.
Step 2. Install Node.js Using NVM
Congratulations! You have successfully installed NVM on Ubuntu. Now, you’re prepared to install various versions of Node.js and utilize the specific version needed for your application.
To begin using NVM, execute the following command to install the latest version of Node.js:
nvm install node
Replace the specific version number you need with 20.9.0.
To install the latest LTS (Long Term Support) version of Node.js, enter:
nvm install node –lts
Using the earlier command, you have the flexibility to choose and install any other version. The initially installed version becomes the default. New shells will use the default node version (e.g., nvm alias default).
For installing a specific LTS release line (a version other than the most recent one), utilize the –lts option along with the desired release line name.
nvm install –lts=argon
Having installed the “argon” LTS line of Node.js with the previous command, you can now switch to the most recent LTS version using the following command:
nvm use –lts
Run the following command to switch to a specific LTS version of Node.js:
nvm use lts/argon
By employing the example command, you transition to the “argon” LTS line of Node.js. Furthermore, execute the
nvm --version command to observe the conclusive version 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
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
nvm alias default 20
Create a new alias by executing the
nvm alias command. In the provided example, a new maintenance alias is defined for Node version 18.16.0.
nvm alias maintenance 18.16.0
The command below allows you to see every alias, both old and new.
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:
To find all the available Node.js versions, type:
You have the option to set your default Node.js version to 20.9.0. To do this for the current session, use the following command:
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
Following the successful installation process of NVM on Ubuntu in this section, you’ll discover how to employ the
nvm run command to seamlessly switch to a different Node version and instantly launch a Node console. In all other aspects, the usage of the run command closely mirrors the standard NVM usage.
nvm run node
Quick NVM Uninstall on Ubuntu
Up to this point, you’ve gained insights into installing and utilizing NVM on Ubuntu. If you ever decide to uninstall NVM, follow the instructions in this section.
nvm uninstall command along with the version number to remove a specific Node.js version. Additionally, the command
nvm ls proves handy in identifying the installed versions.
To remove Node.js version 20.9.0, simply type:
nvm uninstall 20.9.0
Uninstalling NVM in Ubuntu is as straightforward as you reviewed.
Explore more; Easy Guide: Installing Samba on Ubuntu