How to Install Node.js v21 on Linux: A Step-by-Step Guide

Linux TLDR
Last Updated:
Reading time: 5 minutes
πŸš€
Quick Overview

To immediately install the stable version of Node.js and NPM from your package manager, refer to this section. For installing Node.js v21, either check this recommended section or this alternative. To install Node.js v20 via the Snap package, refer to this section.

Node.js is quite popular among developers and Linux users. The capability of providing JavaScript functionalities outside the web browser using Chrome’s V8 JavaScript engine opens the door for many possibilities.

Like, you can create full-stack web applications, real-time applications, RESTful APIs, microservices architecture, command-line tools, desktop applications, Internet of Things (IoT), streaming applications, and many more.

Although this can only be done if you have Node.js installed on your system, newbies working on Linux might face difficulty while installing it. So, in this article, you will learn all the possible and recommended ways to install the latest version of Node.js on your Linux system.

Tutorial Details

DescriptionNode.js
Difficulty LevelModerate
Root or Sudo PrivilegesYes (for installation)
OS CompatibilityUbuntu, Manjaro, Fedora, etc.
Prerequisites–
Internet RequiredYes (for installation)

How to Install Node.Js on Linux

There are multiple ways to install Node.js on your Linux system. However, I’ll provide you with the most recommended and easiest way to install Node.js and the NPM package manager on your Linux system. Starting with

Installing Node.js using a Package Manager

The easiest way to install Node.js on any Linux distribution is by specifying the package name as an argument to your default package manager.

πŸ“
The Node.js version included in your Linux repository might be outdated. Although it’s okay to use for experimentation, it should be avoided in production environments.

So, follow the appropriate command mentioned below, according to your Linux distribution.

Installing Node.js on a Debian or Ubuntu System

Open your terminal and pull the latest version information for the Node.js package, then install it using the APT package manager.

$ sudo apt update
$ sudo apt install nodejs npm -y

The aforementioned command will install Node.js and the NPM package manager on your Linux system.

Installing Node.js on a Red Hat or Fedora System

Red Hat-based systems automatically update the system repository for the latest package information, so all you have to do is fire the following command, and the rest of the work will be handled by the DNF package manager.

$ sudo dnf install nodejs -y

The above command will install Node.js and the NPM package manager along with it.

Installing Node.js in an Arch or Manjaro System

The Node.js package is available in the community repository; all you have to do is execute the following command:

$ sudo pacman -Sy nodejs npm

The above command will install Node.js and the NPM package manager.

Lastly, execute the following command to verify the installation of Node.js and the NPM package manager is done correctly on your Linux system.

$ node -v
$ npm -v

Output:

Verifying the Node.js and NPM installation

Installing Node.js using the NodeSource Repository

The downside of installing Node.js via the package manager is that the information in the repository is outdated, keeping you away from the latest release of it.

Thankfully, you can use the PPA repository that NodeSource offers, which contains the latest release information for Node.js and the NPM package manager.

1. Open your terminal and install the latest version of curl (including other dependencies) on your Linux system.

$ sudo apt install ca-certificates curl gnupg -y                                                #On Debian or Ubuntu
$ sudo dnf install curl -y                                                                                        #On Red Hat or Fedora

2. Download and import the NodeSource GPG key by running (skip for RHEL-based distros):

$ sudo mkdir -p /etc/apt/keyrings && curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | sudo gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg

3. Add the β€œNode.js v21.x” PPA provided by NodeSource to your Linux system (check this GitHub repo for other Node.js versions and supported Linux distributions).

⚠️
Remove the current Node.js and NPM installations from your system prior to continuing.
#On Debian or Ubuntu
$ echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_21.x nodistro main" | sudo tee /etc/apt/sources.list.d/nodesource.list

#On Red Hat or Fedora
$ sudo dnf install https://rpm.nodesource.com/pub_21.x/nodistro/repo/nodesource-release-nodistro-1.noarch.rpm -y

4. Before installing the latest version of Node.js and NPM, refresh your system repository information once again (skip for RHEL-based distros):

$ sudo apt update

5. Now, execute the following command to install the latest version of Node.js (NPM is included).

$ sudo apt install nodejs -y                                                                                              #On Debian or Ubuntu
$ sudo dnf install nodejs -y                                                                                              #On Red Hat or Fedora

6. Lastly, verify the current version of Node.js and the NPM on your Linux system by running.

$ node -v
$ npm -v

Output:

Verifying the Current Version of Node.js and NPM

Installing Node.js using the NVM (Node Version Manager)

NVM (Node Version Manager) allows you to easily install multiple versions of Node.js and switch between them from your terminal.

The following methods will only work for Ubuntu and Red Hat-based distributions with bash as the default login shell (to learn more, check out this GitHub repo).

1. To install it, execute the following curl command that will download the NVM installer script and install it on your Linux system.

πŸ“
If you encounter an error stating β€œCouldn’t connect to server” in the command prompt, please go to this link, copy its content to β€œinstall.shβ€œ, and then execute the β€œbash install.sh” command.
$ curl https://raw.githubusercontent.com/creationix/nvm/master/install.sh | bash

2. Then either restart your terminal or execute the following command to reload your shell configuration file:

$ source ~/.bashrc

3. List the available versions of Node.js by issuing the following command:

$ nvm list-remote

Output:

List of available Node.js versions

4. Now, choose the one that you wish to install and specify the version name (ex: β€œ20.4.0β€œ) in the following command.

$ nvm install 20.4.0

5. Lastly, verify the current installed version of Node.js by issuing the following command:

$ node -v
$ npm -v

Output:

Verifying the current version of Node.js and NPM

6. To switch between multiple Node.js versions, first install Node.js with another version (ex: β€œ18.1β€œ).

$ nvm install 18.1

7. Then list all the Node.js versions installed on your Linux system.

πŸ“
The most recent installed Node.js version will always be set as the default.
$ nvm ls

Output:

List of installed Node.js versions

8. As you can see from the above image, the default selected Node.js version is β€œv18.1.0β€œ. So, to use another version of Node.js, simply execute the following command, specifying the desired Node.js version (ex: β€œ20.4.0β€œ) as an argument.

$ nvm use 20.4.0
$ nvm ls

Output:

Switching to a different Node.js version

9. To prevent reverting back to the previous default version of Node.js in the next terminal session, just execute the following with the desired Node.js version:

$ nvm alias default 20.4.0

Output:

Permanently switching to another version of Node.js

Installing Node.js using the Snap Package

Snap, developed by Canonical (the creator of Ubuntu systems), can be found in certain Linux distributions. It provides an isolated environment for the tool and includes all the necessary packages and libraries in the same box.

Node.js is available as a snap package; the current β€œv20.8.0β€œ is available for installation, so to install it, run the following command:

$ sudo snap install node --classic

To verify that the installation was done correctly, restart your terminal session and execute the following command:

$ node -v
$ npm -v

Output:

Checking the version of Node.js and NPM installed via Snap

How to Remove Node.Js on Linux

The removal part is as simple as the installation; simply execute the respective command based on your Linux distribution the same way you have used to install Node.js on your Linux system.

Removing Node.js using a Package Manager

$ sudo apt remove nodejs npm -y                                                                    #Debian, Ubuntu, or Pop!_OS
$ sudo dnf remove nodejs -y                                                                             #Red Hat, Fedora, or AlmaLinux
$ sudo pacman -Rs nodejs npm                                                                        #Arch, Manjaro, or EndeavourOS 

Removing Node.js using the NodeSource Repository

#Debian, Ubuntu, or Pop!_OS
$ sudo apt purge nodejs && sudo rm -r /etc/apt/sources.list.d/nodesource.list && rm -r /etc/apt/keyrings/nodesource.gpg

#Red Hat, Fedora, or AlmaLinux
$ sudo dnf remove nodejs && sudo rm -r /etc/yum.repos.d/nodesource*.repo

Removing Node.js using the NVM (Node Version Manager)

1. First, execute the following command to remove NVM:

$ rm -rf "$NVM_DIR"

2. And then open β€œ~/.bashrc” and remove the following lines:

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
[[ -r $NVM_DIR/bash_completion ]] && \. $NVM_DIR/bash_completion

Removing Node.js using the Snap Package

$ sudo snap remove node

Final Word

I’ve covered all the possible ways to install Node.js on most Linux distributions except compiling from source code, which is definitely painful and confusing, especially for newbies.

However, if you found this article useful, do subscribe to us, and if you have any questions or queries related to this article or are stuck somewhere, do let us know via the comment section.

Till then, peace!

Join The Conversation

Users are always welcome to leave comments about the articles, whether they are questions, comments, constructive criticism, old information, or notices of typos. Please keep in mind that all comments are moderated according to our comment policy.