📒 

Introduction

NVM (Node Version Manager) is a popular tool for managing multiple versions of Node.js on a single machine. It allows developers to install, switch, and manage different versions of Node.js seamlessly, making it especially useful for those working on multiple projects that require different Node.js versions. This guide will walk you through the steps to install NVM and use it to manage Node.js versions on an Ubuntu system.

Prerequisites

Before you begin, make sure that:

  • You have a user account with sudo privileges.
  • Your system is running Ubuntu (this guide works for various Ubuntu versions including 20.04, 22.04, and later).

Step 1: Update the Package List

It’s always a good practice to update your package list before installing new software. Open a terminal and run:

sudo apt-get update

This command ensures that all your package lists are up to date.

Step 2: Install NVM

To install NVM, you need to download the installation script from the official NVM repository. You can do this using curl or wget.

Option 1: Using curl

If you have curl installed on your system, run:

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

Option 2: Using wget

If you prefer to use wget, run:

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

Replace v0.39.5 with the latest version of NVM if a newer version is available. You can check the latest version on the NVM GitHub repository.

Step 3: Activate NVM

After running the installation script, you need to load NVM into your current terminal session. You can do this by running:

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"

Alternatively, you can close and reopen the terminal, or run the following command to refresh your shell profile:

source ~/.bashrc

If you are using a different shell like zsh, run:

source ~/.zshrc

Step 4: Verify the NVM Installation

To confirm that NVM is installed correctly, run:

nvm --version

You should see the version number of NVM, indicating that it has been installed successfully.

Step 5: Install Node.js Using NVM

With NVM installed, you can easily install Node.js. To install the latest version of Node.js, run:

nvm install node

This command installs the latest stable version of Node.js. If you want to install a specific version, specify it like this:

nvm install 16.20.0

Replace 16.20.0 with the desired version number.

Step 6: Check Installed Node.js Version

To check which version of Node.js is currently active, run:

node -v

This command will display the version number of Node.js that is currently in use.

Step 7: Switch Between Node.js Versions

One of the key benefits of NVM is the ability to switch between different Node.js versions. To list all the installed Node.js versions, run:

nvm ls

You will see a list of all the versions installed using NVM, along with an indication of the currently active version.

To switch to a different version, use:

nvm use 14.17.6

Replace 14.17.6 with the version number you want to use.

Step 8: Set a Default Node.js Version

If you want a specific Node.js version to be the default whenever you open a new terminal session, run:

nvm alias default 16.20.0

Replace 16.20.0 with the version you want to set as default.

Step 9: Uninstall a Node.js Version

If you no longer need a specific Node.js version, you can uninstall it with:

nvm uninstall 14.17.6

Replace 14.17.6 with the version number you wish to remove.

Step 10: Install Global npm Packages with NVM

When using NVM, it’s important to install global npm packages (like yarn or npm) for each Node.js version separately. This is because each version of Node.js has its own isolated environment. To install a global package, use:

npm install -g yarn

Make sure that you have the desired Node.js version active before installing global packages.

Conclusion

NVM is an essential tool for developers who need to manage multiple versions of Node.js on a single machine. By following this guide, you can easily install NVM, set up Node.js, and switch between different versions as needed. This makes managing Node.js dependencies across different projects simple and hassle-free. With NVM, you’ll always have the right version of Node.js for your projects, ensuring compatibility and ease of use. Happy coding!