• Product Updates

How to Install Node.js on Ubuntu 22.04 Using NVM

How to Install Node.js on Ubuntu 22.04 Using NVM

Table of contents

This guide shows you how to install Node on Ubuntu 22.04 using NVM (Node Version Manager), a powerful tool for managing multiple Node.js versions. With NVM, you can easily switch between versions, keeping your development environment flexible. Once installed, your server will be ready for building and deploying Node.js applications with ease.

What is Node.js?

Node.js is an open-source JavaScript runtime built on Chrome’s V8 engine. It is designed for creating fast and scalable network applications, especially when handling numerous simultaneous connections with high throughput. One of Node.js’s standout features is its non-blocking, event-driven architecture, which makes it highly efficient in handling tasks like real-time data processing. Node.js is cross-platform, meaning it works on Windows, Mac OS X, and Linux, making it versatile for developers working in various environments. With its memory-efficient design, it’s especially suited for large-scale applications that require high performance.

Introduction to Node.js and NVM

Node.js empowers developers to run JavaScript on the server side, replacing traditional backend technologies like PHP or Ruby with JavaScript. This shift simplifies the development process by allowing the same language to be used across both the front-end and back-end. However, managing multiple versions of Node.js can be a challenge. Different projects often require different Node.js versions, and switching between them can become a hassle without the right tools. To make this easier, you can install Node.js via NVM on Ubuntu, allowing you to manage multiple versions of Node.js effortlessly. This ensures you can install Node on Ubuntu and switch between versions without any conflicts, improving your development workflow.

This is where NVM (Node Version Manager) comes in. NVM is a bash script that allows you to manage multiple versions of Node.js on a single system, providing several key benefits:

  1. Install multiple versions of Node.js side by side.
  2. Switch easily between different versions depending on project requirements.
  3. Set a default version of Node.js for your system.
  4. Manage versions per user without affecting other users on the server.

NVM makes it easy to avoid version conflicts and helps you work efficiently on different projects with varying Node.js version requirements.

What Are the Prerequisites for Installing Node.js on Ubuntu?

Before proceeding with the installation of Node.js on Ubuntu 22.04, ensure you meet the following prerequisites:

You should have an Ubuntu 22.04 server ready for use. This guide assumes you’re working with a fresh cloud server installation.

A non-root user with sudo privileges should be set up. Ensure you avoid using root privileges when installing NVM, as it is intended to manage Node.js installations on a per-user basis.

With these prerequisites in place, you are all set to install Node on Ubuntu and begin developing your applications with ease.

If you're looking to install Node on Ubuntu, using NVM (Node Version Manager) offers a flexible way to handle multiple Node.js versions. In this guide, we'll walk you through the detailed steps to install Node.js via NVM on Ubuntu 22.04, ensuring that you can manage different versions of Node.js without conflicts.

Step #1: Install NVM Dependencies

Before installing NVM, you'll need to install a few dependencies. These include essential tools like curl, wget, and gnupg, which are necessary for downloading and managing packages. Use the following commands to install these dependencies:

sudo apt update

sudo apt install curl wget gnupg

While these dependencies are likely already present on your system, updating them is a good practice to avoid any compatibility issues.

Step #2: Install NVM

Now that the required dependencies are in place, we can proceed with the installation of NVM.

2.1. Fetch and Install NVM

To install NVM, we will fetch the official installation script from the NVM GitHub repository. Execute the following command as your regular user (avoid using sudo):

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



This command performs several key actions:

  • It clones the NVM repository to your ~/.nvm directory.
  • It adds the necessary source lines to your shell profile files (~/.bashrc, ~/.zshrc, etc.).
  • It sets the appropriate permissions for NVM’s installation directory.

Once the script finishes, you should see output similar to the following:

=> Appending nvm source string to /home/<user>/.bashrc

=> Appending bash_completion source string to /home/<user>/.bashrc

2.2. Load NVM

To use NVM immediately, load it into your terminal session by running the two export commands provided in the output:

export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")"

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

Alternatively, you can close and reopen your terminal to load NVM automatically.

Verify the installation by checking the NVM version:

nvm --version

You should see something like 0.39.3, confirming that NVM is successfully installed.

Step #3: Install Node.js

Now that NVM is set up, it’s time to install Node.js on Ubuntu. Node.js is essential for many web development projects, and NVM makes managing its versions easy.

3.1. Install Node v18

For this guide, we’ll install Node.js version 18, which is the latest stable release at the time. Run the following command to install it:

nvm install node

This command will:

  • Download the official Node.js binaries.
  • Unpack and install the Node.js version under ~/.nvm.
  • Automatically set up the Node and npm binaries.

You’ll see output like this once the installation is complete:

Downloading and installing node v18.13.0...

Now using node v18.13.0 (npm v8.19.2)

3.2. Confirm the Node.js Version

To confirm the version of Node.js that was installed, run the following command:

node -v

This will display the installed Node.js version, such as v18.13.0, verifying that NVM installed Node.js correctly.

3.3. List All Installed Node.js Versions

To check all the Node.js versions installed by NVM, use the following command:

nvm ls

You should see something like this:

-> v18.13.0

system

default -> node (-> v18.13.0)

This output shows the installed versions, with the -> symbol indicating the active version (in this case, Node.js v18.13.0).

3.4. Install Another Node Version

You can easily install another version of Node.js. For example, to install the latest LTS (Long Term Support) version, use this command:

nvm install --lts

This installs the most recent LTS version of Node.js.

3.5. Verify All Installed Versions

To see all installed versions of Node.js, run:

nvm ls

This will now show both Node.js v18 and v16, or whichever versions you’ve installed.

Step #4: Managing Node.js Versions with NVM

One of the best features of NVM is its ability to easily manage multiple versions of Node.js. Here’s how you can use NVM to switch between different versions.

4.1. List Available Node Versions

To see all Node.js versions available for installation, use:

nvm ls-remote

This will display a list of versions, both the latest and LTS versions, available for installation:

v18.13.0 (Latest LTS: Hydrogen)

v16.19.0 (LTS: Gallium)

v14.21.2 (LTS: Fermium)

Step #5: Install Specific Node.js Versions

You can install a specific Node.js version by specifying the version number. For example, to install Node v12.22.12, use:

nvm install 12.22.12

Similarly, to install an older LTS version, use:

nvm install --lts=erbium

5.2. List Installed Versions

To list all locally installed versions of Node.js, run:

nvm ls

This will show all versions you have installed:

-> v12.22.12

v14.21.2 (LTS: Fermium)

v16.19.0 (LTS: Gallium)

v18.13.0

5.3. Set the Default Node.js Version

You can set a default version for Node.js that will be used in new terminal sessions. To set Node v14.21.2 as the default, use:

nvm alias default 14.21.2

To verify the default version, use:

nvm run default --version

5.4. Use a Specific Node.js Version

If you need to use a specific version of Node.js for a project, simply switch to that version with the nvm use command:

nvm use 16

This command will set your terminal session to use Node.js v16.19.0. To check which version is active, run:

node -v

You should see:

v16.19.0

5.5. Run Commands Under Specific Node.js Versions

You can also run specific Node.js commands under a particular version without switching globally. For instance:

nvm run 16 --version

This will run the node --version command under Node v16.

You can also install packages using a specific Node version:

nvm exec 12.22.12 npm install -g http-server

This installs the http-server package using Node v12.22.12.

With these steps, you can successfully install Node.js on Ubuntu, switch between different versions, and manage your development environment using NVM.

Uninstalling Node.js Versions and Managing NVM Commands

Managing Node.js versions with NVM (Node Version Manager) is a simple yet effective way to handle multiple Node installations on Ubuntu. But sometimes, you may need to uninstall old versions to keep your system clean. This guide will walk you through uninstalling Node.js versions and review some of the most popular NVM commands, followed by additional best practices for using NVM in production.

Removing a Node.js version is easy with NVM. To uninstall a specific version, use the nvm uninstall command. For example, to uninstall Node v12.22.12, run:

nvm uninstall 12.22.12

Once the version is uninstalled, you can verify which versions are still active by running the nvm ls command. This will list the remaining versions:

nvm ls

If you’ve uninstalled Node v12, you should see only Node v16 and v18 remaining.

It's important to note that NVM does not allow you to uninstall a version that's currently active or set as the default. If you wish to uninstall a version that is currently in use, you’ll need to switch to another version first. You can do this using the nvm use command:

nvm use 16

Now you can safely uninstall the version that was in use.

A Review of Popular NVM Commands

NVM simplifies managing multiple versions of Node.js on your system. Here’s a quick overview of the most commonly used NVM commands:

nvm ls — List all installed Node.js versions.

nvm ls-remote — List all available Node.js versions.

nvm install 8.19.2 — Install a specific version of Node.js.

nvm uninstall 8.19.2 — Uninstall a specific Node.js version.

nvm alias default 8.19.2 — Set a global default Node.js version.

nvm use 8.19.2 — Switch the current shell to a specific Node.js version.

nvm run 6 --version — Run a command under a specific Node.js version.

These are just a few of the essential commands available. For more detailed usage, refer to the NVM documentation on GitHub.

Bonus Insights: Best Practices and Key Considerations for Using NVM

While setting up NVM and Node.js is relatively quick, there are several considerations when using NVM in production environments. Here, we’ll address a few common questions to ensure you make informed decisions.

Should I Use NVM in Production?

NVM is widely used in development environments, providing an easy way to test Node.js applications across different versions. However, you might be wondering whether to use it in production environments as well.

NVM allows you to easily install Node.js and switch between versions without causing conflicts in the system. This flexibility can be particularly useful when upgrading to a new LTS (Long-Term Support) version of Node.js. 

But it’s important to consider the potential challenges when using NVM in production:

  1. Resource Overhead

The more Node versions you install, the more resources your system will require. Each version uses disk space and memory, which can impact performance if you’re running many applications on a production server.

  1. Maintenance Complexity

More versions mean more maintenance overhead. You’ll need to track and manage which versions are active and ensure that older versions don’t interfere with the newer ones.

In production, it’s advisable to limit the number of Node.js versions you install:

  • For frontend builds, stick to one or two LTS versions.
  • For legacy backend services, keep the specific older versions required.
  • Set a default LTS version to maintain stability across your environment.

While NVM adds flexibility, using it in production requires careful consideration to avoid unnecessary complexity.

Should I Use System Packages or NVM for Node.js?

Linux distributions like Ubuntu typically offer Node.js through their package managers. You can install Node.js directly using apt:

sudo apt install nodejs

However, system packages come with some limitations:

  1. Single Version

You can only install one version of Node.js at a time. This limitation is problematic if you need to install and test multiple versions simultaneously.

  1. Outdated Releases

Many system packages are not updated as frequently as Node.js releases. For instance, Debian or Ubuntu's package manager may provide an older version of Node.js, requiring you to manually install the latest release.

  1. Global npm Package Conflicts

When you upgrade the system Node.js, any globally installed npm packages may break, creating additional maintenance work.

In contrast, NVM allows you to install Node.js versions side-by-side without conflicts. It isolates each version of Node.js and its associated npm packages, making it easier to manage different dependencies for each version.

Therefore, while you can use the system's package manager for installing Node.js, NVM offers greater flexibility and reduces the risk of package conflicts. If necessary, you can use both NVM and system packages simultaneously, especially for legacy applications that rely on the system version of Node.js.

Does NVM Affect Globally Installed npm Packages?

When using NVM, it installs separate npm packages for each version of Node.js. This ensures that the npm packages for one version of Node.js do not interfere with another version. For example, Node.js v14 includes npm v6, while Node v16 includes npm v8. This compartmentalization ensures that each version of Node.js has its own set of dependencies.

If you install npm packages globally via NVM, they are scoped to that particular Node.js version. For example, if you install a global package while using Node v14, it will not be available if you switch to Node v16. This prevents version conflicts between global npm packages.

Here are a few important points to remember:

  1. Global npm packages need to be reinstalled after upgrading Node.js versions.
  2. Global installs apply only to the current NVM Node.js version.
  3. Local installs in node_modules are unaffected by version changes.

NVM’s ability to isolate Node versions helps avoid system conflicts and ensures that each version of Node.js can have its own independent npm environment.

How to Uninstall Node.js and NVM on Ubuntu: A Complete Guide

If you're looking to clean up your system by removing Node.js or NVM, this guide will walk you through the necessary steps. Whether you installed Node.js via NVM or using the package manager, you can easily remove it following these steps.

Uninstalling Specific Node.js Versions

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

nvm uninstall 12.22.12

This will remove the entire Node.js version from your system. Afterward, you can check the remaining installed versions by running:

nvm ls

This command will show the versions that are still active on your system, ensuring you've successfully removed the desired one.

How to Completely Uninstall NVM

To remove NVM and all of its associated files, follow these steps:

Uninstall Node.js versions: Begin by uninstalling any Node.js versions installed via NVM. Run the following commands to remove the latest LTS and the default Node version:

nvm uninstall --lts

nvm uninstall node

Delete the NVM directory: Next, remove the entire NVM directory by running this command:

rm -rf ~/.nvm

Edit your shell profile: NVM adds some lines to your shell profile (such as ~/.bash_profile or ~/.zshrc). You need to manually remove the lines that were added for NVM to work, including lines like:

source ~/.nvm/nvm.sh

Restart your terminal: Finally, either restart your terminal or run source ~/.bashrc (or the appropriate profile for your shell) to apply the changes.

After following these steps, NVM will no longer be present on your system. If you try to run an NVM command, you'll see a "command not found" error, confirming that the system is clean.

Uninstalling System Node.js Packages

If you installed Node.js through your system's package manager, you can remove it using the following commands for your Linux distribution.

Debian/Ubuntu

For Debian-based systems like Ubuntu, run:

sudo apt purge nodejs npm

RHEL/CentOS

For RHEL or CentOS, use:

sudo dnf erase nodejs npm

These commands will remove the Node.js and npm packages provided by your operating system’s package manager. However, note that this will not affect the Node.js versions installed by NVM, as they are completely separate.

Installing and Uninstalling Node.js on Ubuntu 22.04

And there you have it! We’ve walked through the installation and removal of Node.js on Ubuntu 22.04 using NVM. Here are the key takeaways from this guide:

  1. NVM lets you manage multiple Node.js versions with ease.
  2. Install NVM first, then install any versions of Node.js as needed.
  3. Switching Node versions is simple and can be done with just a command.
  4. Global npm packages are specific to each Node.js version.
  5. You can easily upgrade or downgrade Node.js using NVM.

With NVM, you can easily manage Node.js versions, simplifying the development of JavaScript backends and APIs. Whether you're building a personal app or managing a production environment, NVM offers the flexibility to ensure the right versions are always in use.

Share

About the authors


scale 1
Ready to scale?

Start for free and unlock high-performance infrastructure with instant setup.

Get started arrow button

Help us improve — share your feedback

Your opinion helps us build a better service.