How to Install Node.js on Ubuntu 24.04, 22.04 or 20.04

This guide will cover how to install Node.js on Ubuntu versions 24.04, 22.04, or 20.04, offering a thorough walk-through for users, sysadmins, or developers. It will explore three distinct methods: using Ubuntu’s APT repository, the NodeSource APT repository, and the Node Version Manager (NVM), each tailored to different use cases.

Node.js, a powerful JavaScript runtime built on Chrome’s V8 JavaScript engine, has revolutionized the way we think about and build server-side applications. Its non-blocking, event-driven architecture makes it a top choice for scalable and efficient web applications. Here’s a quick look at what makes Node.js stand out:

  • Asynchronous and Event-Driven: Node.js uses non-blocking I/O operations, making it lightweight and efficient for data-intensive real-time applications.
  • Single Programming Language: Write both client and server-side code in JavaScript, providing a unified development experience.
  • Vast NPM Registry: Access a massive repository of open-source libraries, enhancing functionality and speeding up development.
  • Cross-Platform: Develop applications that run seamlessly on various operating systems.
  • Community and Corporate Support: Benefit from a vibrant community and backing from major corporations, ensuring constant updates and innovations.

With Node.js, developers gain a versatile tool for creating a variety of web applications, from simple web servers to complex, real-time communication platforms. As we transition into the technical aspects, this guide aims to simplify the installation process on Ubuntu, providing clear instructions for different versions and methods. Let’s dive into the installation process to harness the power of Node.js on your Ubuntu system.

Update Ubuntu Before Node.js Installation

Before installing Node.js, ensuring your Ubuntu system is up-to-date is crucial. This helps prevent potential conflicts and ensures you install the latest version of Node.js. Run the following command to update your system:

sudo apt update && sudo apt upgrade

Install Node.js on Ubuntu via Ubuntu APT Repository

The first method is to install Node.js on Ubuntu 24.04, 22.04, or 20.04 directly from your Ubuntu APT repository. This will be satisfactory for most cases, depending on what you require Node.js for.

To install the Ubuntu default version, run the following command:

sudo apt install nodejs

Ensure Node.js is installed correctly after installation by verifying its version on your system. Run the following command to check the installed Node.js version:

node --version

If the installation was successful, you’ll see the installed Node.js version displayed in your terminal. Now, you’re ready to start using Node.js on your Ubuntu system.

Install Node.js on Ubuntu via NodeSource

Install Required Initial Packages

First, ensure the following packages are installed below:

sudo apt install curl apt-transport-http ca-certificates

Import NodeSource APT Repository

Next, import the GPG key of the NodeSource repository. This action verifies the authenticity of the packages you’ll be installing. Use the command:

curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | sudo gpg --dearmor -o /usr/share/keyrings/nodesource.gpg

Then, select the Node.js version appropriate for your Ubuntu system. For example, replace NODE_MAJOR=20 it with your desired version, like NODE_MAJOR=18. Run this command:

NODE_MAJOR=20
echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_$NODE_MAJOR.x nodistro main" | sudo tee /usr/share/keyrings/nodesource.gpg

Choose from various Node.js versions such as:

  • NODE_MAJOR=16
  • NODE_MAJOR=18
  • NODE_MAJOR=20
  • NODE_MAJOR=21

Install Node.js via NodeSource APT Command

After adding the NodeSource repository, install Node.js with the command below. This method ensures you get the latest or a specific version of Node.js, offering an upgrade from the default repository:

sudo apt install nodejs

This command installs Node.js, and all its dependencies align with the version you chose earlier.

Conclude by verifying the Node.js installation:

node --version

The output version number confirms that Node.js has been successfully installed on your Ubuntu system from the NodeSource APT Repository.

Install Node.js on Ubuntu via Node Version Manager (NVM)

Another way to install Node.js is by using the Node Version Manager (NVM). This method enables you to manage multiple Node.js versions on your system, making switching between versions for different projects easier.

Install NVM on Ubuntu

To install NVM, run one of the following commands:

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

or

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

These commands download and execute the NVM installation script. Once the installation is complete, you need to restart your terminal or run the following command to load NVM:

source ~/.bashrc

Install Node.js on Ubuntu via NVM Command

With NVM installed, you can manage multiple Node.js versions on your system, making switching between versions for different projects convenient.

List Available Node.js Versions on Ubuntu

Before installing a specific version of Node.js, check the available versions by running the following:

nvm ls-remote
Screenshot of 'nvm ls-remote' command listing available Node.js versions for Ubuntu 22.04 or 20.04.
‘nvm ls-remote’ command example showcasing available Node.js versions for installation on Ubuntu 22.04 or 20.04.

This command displays a list of all available Node.js versions, helping you identify the version you want to install, such as the latest LTS release or a specific version number.

Install a Node.js Version on Ubuntu

To install the desired version of Node.js, run the following command and replacing <version> with the specific version you want to install:

nvm install <version>

For example, to install Node.js version v20.0.0, run:

nvm install 20.0.0
Installing Node.js version 20.0.0 using NVM on Ubuntu Linux 24.04, 22.04, or 20.04
Step-by-step installation of Node.js 20.0.0 with NVM on Ubuntu Linux 24.04, 22.04, or 20.04

This command downloads and installs the specified version of Node.js.

Verify Node.js Installation

To check the installed version of Node.js, run the following:

node --version

This command displays your system’s installed version of Node.js, confirming the successful installation.

Switch Between Installed Node.js Versions via NVM

NVM allows you to switch between different Node.js versions easily. To switch between installed Node.js versions, use the following command, replacing <version> with the version you want to switch to:

nvm use <version>

For example, to switch to Node.js version 18.16.0, run:

nvm use 18.16.0

This command sets the specified version as the active Node.js version for your current session. To make a specific Node.js version the default for new terminal sessions, use the command:

nvm alias default <version>

Replace <version> with the desired version number. For example, to set Node.js version 18.16.0 as the default, run:

nvm alias default 18.16.0

Additional Learning: Remove Node.js from Ubuntu

In this section, we will cover removing Node.js from an Ubuntu system. The removal process will depend on the method you used to install Node.js, either through the Ubuntu repository, a PPA, or NVM.

Remove Node.js Installed via Ubuntu Repository or NodeSource

If you installed Node.js from the Ubuntu repository or a PPA, you can uninstall it using the apt program. Here’s a breakdown of the command you need to run:

sudo apt remove nodejs

This command will remove Node.js along with its associated configuration files. It will prompt you to confirm the removal, and after confirmation, it will proceed with the uninstallation.

NVM Command to Remove Node.js on Ubuntu

If you installed Node.js using the Node Version Manager (NVM), follow these steps to uninstall it:

Check the Current Node.js Version on Ubuntu via NVM Command

First, determine the currently installed version of Node.js by running the following command:

nvm current

This command displays the active Node.js version on your system.

Deactivate NVM on Ubuntu via NVM Command

Before uninstalling the current version of Node.js, you need to deactivate NVM by running the following:

nvm deactivate

This command unloads the active Node.js version from your current session.

Remove Node.js Version via NVM Command on Ubuntu

Now, run the following command to uninstall a specific version of Node.js installed using NVM, replacing <version> with the version number you want to uninstall:

nvm uninstall <version>

For example, to uninstall Node.js version v20.0.0, run:

nvm uninstall 20.0.0

This command removes the specified Node.js version from your system, completing the uninstallation process.

Closing Thoughts

There you have it! We’ve journeyed through the different ways to install Node.js on Ubuntu Linux – be it 24.04, 22.04, or 20.04. Whether you chose the straightforward Ubuntu repository, the more up-to-date NodeSource APT, or the versatile NVM, you’re now equipped to handle your Node.js needs like a pro. Remember, the choice of method hinges on what works best for your project.

Leave a Comment