How to Install NVM on Ubuntu? Tips, and Explanations

how to install nvm on ubuntu

If you’re working with Node.js on your Ubuntu system, managing different versions of Node.js can become a hassle—especially when switching between projects that require specific versions of Node. That’s where NVM (Node Version Manager) comes in. In this guide, we will walk you through the process of install NVM on Ubuntu, explain how it works, and show you how to switch between Node.js versions.

It allows you to easily install and manage multiple versions of Node.js, helping you work efficiently on multiple projects without version conflicts. By the end of this guide, you’ll be able to set up and manage your Node.js versions with ease!

What is NVM and Why Do You Need It?

NVM stands for Node Version Manager, and it is a tool that allows you to install and manage multiple versions of Node.js on a single machine. Whether you’re working on multiple Node.js projects with different version requirements or simply need to test your code against various Node.js versions, NVM makes this process seamless.

Key benefits of using NVM:

  • Install and manage multiple versions of Node.js on the same system.
  • Switch between versions easily with a single command.
  • No need for root privileges when switching Node versions (NVM installs Node.js in your home directory).
  • Manage npm versions as well, since npm comes bundled with Node.js.

For developers working in JavaScript, having NVM installed is essential for smooth development workflows, especially when working with various Node.js applications.

Prerequisites for Install NVM on Ubuntu

Before you begin the installation of NVM, make sure that you have the following prerequisites:

  1. Ubuntu system: This guide is specifically for Ubuntu. It’s recommended that your system is up-to-date, so it’s good practice to run the following commands first:bashCopy codesudo apt update sudo apt upgrade
  2. Curl or Wget: You will need either curl or wget to download the NVM installation script. Most Ubuntu systems have these tools installed by default. If not, you can install them using:bashCopy codesudo apt install curl orbashCopy codesudo apt install wget
How to install NVM on Ubuntu

How to Install NVM on Ubuntu?

The most straightforward way to install NVM is by using the installation script provided by the NVM team. Here’s a step-by-step breakdown:

Step 1: Download and Run the NVM Installation Script

You can install NVM via curl or wget. Choose one of the following commands based on the tool available on your system.

  • Using curl:bashCopy codecurl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
  • Using wget:bashCopy codewget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash

This script will download and install NVM into your home directory (~/.nvm) and modify your shell configuration file (e.g., ~/.bashrc or ~/.zshrc) to make NVM available every time you open a terminal session.

Step 2: Close and Reopen the Terminal

After running the installation script, you need to restart your terminal or run the following command to source your updated shell configuration file:

bashCopy codesource ~/.bashrc

If you’re using Zsh, use:

bashCopy codesource ~/.zshrc

This step loads the NVM command into your terminal session, making it available for use.

Step 3: Verify NVM Installation

To check if NVM was installed successfully, run the following command:

bashCopy codenvm --version

If you see the version number (e.g., 0.39.1), NVM has been installed correctly.

How to Install Node.js Using NVM?

Now that NVM is installed, you can easily install Node.js versions. Follow these steps to install Node.js using NVM:

Step 1: List Available Node.js Versions

To view the available versions of Node.js, run:

bashCopy codenvm ls-remote

This will display a list of all available versions of Node.js. The list includes the LTS (Long Term Support) versions, which are recommended for most production environments, and the Current versions, which feature the latest updates.

Step 2: Install a Specific Version of Node.js

To install a specific version of Node.js, use the following command:

bashCopy codenvm install <version>

For example, to install Node.js version 16.14.0:

bashCopy codenvm install 16.14.0

Alternatively, to install the latest LTS version, run:

bashCopy codenvm install --lts

Step 3: Set a Default Node.js Version

Once you have installed a version of Node.js, you can set it as the default version for your system. Use the following command:

bashCopy codenvm use <version>

For example, to use Node.js version 16.14.0:

bashCopy codenvm use 16.14.0

If you want to set this version as the default, run:

bashCopy codenvm alias default 16.14.0

This will make version 16.14.0 the default for all new terminal sessions.

How to Switch Between Different Node.js Versions?

One of the biggest advantages of NVM is the ability to switch between different Node.js versions effortlessly. To switch between installed versions of Node.js, follow these steps:

Step 1: List Installed Node.js Versions

To list the versions of Node.js installed on your system, run:

bashCopy codenvm ls

This will display all the versions of Node.js installed via NVM.

Step 2: Switch to a Different Version

To switch to a different version, use the following command:

bashCopy codenvm use <version>

For example, to switch to Node.js version 14.17.0:

bashCopy codenvm use 14.17.0

Step 3: Set the Default Version (Optional)

If you want to set a specific version as your default for every new terminal session, you can run:

bashCopy codenvm alias default <version>

For example, to set version 14.17.0 as the default:

bashCopy codenvm alias default 14.17.0

How to Uninstall Node.js Versions Using NVM?

If you no longer need a specific version of Node.js, you can easily uninstall it using NVM. To uninstall a version, use the following command:

bashCopy codenvm uninstall <version>

For example, to uninstall Node.js version 16.14.0:

bashCopy codenvm uninstall 16.14.0

This will remove the specified version from your system.

Troubleshooting Common NVM Installation Issues on Ubuntu

While the installation of NVM is usually smooth, here are a few common issues and how to resolve them:

1. “nvm command not found” Error

If you see the error "nvm command not found", it’s likely that the NVM installation script didn’t update your shell configuration file correctly.

Solution:

  • Ensure that the installation script was successfully run.
  • Open your ~/.bashrc (or ~/.zshrc for Zsh users) and verify that the following lines are added at the end of the file:bashCopy codeexport NVM_DIR="$HOME/.nvm" [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
  • If these lines aren’t present, add them manually, then run source ~/.bashrc (or source ~/.zshrc for Zsh users) to load the changes.

2. Permission Issues

Sometimes, NVM may encounter permission issues, especially if you’re trying to install Node.js globally.

Solution:

  • Always use NVM to install and manage Node.js versions without needing root privileges.
  • If you installed Node.js globally using sudo previously, it could cause conflicts with NVM. It’s recommended to remove any globally installed versions and manage everything with NVM.

3. NVM is Not Persisting After a Reboot

If NVM commands aren’t working after rebooting your system, it might be because the NVM environment isn’t loaded properly.

Solution:

  • Recheck your shell’s configuration file (~/.bashrc or ~/.zshrc) to ensure the NVM initialization code is present and correct.

Conclusion

Install NVM on Ubuntu is a quick and efficient way to manage multiple versions of Node.js for your development projects. NVM not only helps avoid version conflicts but also makes it easy to switch between different Node versions depending on your project’s requirements.

With NVM installed, you can now install, switch between, and uninstall Node.js versions with just a few commands. This gives you greater flexibility and control over your development environment, ensuring that you’re always working with the right version of Node.js.

Leave a Comment

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

Scroll to Top