Installation of Node.js on Linux for Newbies

Linux TLDR
Last Updated:
Reading time: 5 minutes

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 Node.js on your Linux system.

Tutorial Details

DescriptionNode.js
Difficulty LevelModerate
Root or Sudo PrivilegesYes
Host System and ArchitectureUbuntu 22.10 (x64)
OS Compatibility Ubuntu, Manjaro, Fedora, etc.
Prerequisites
Internet RequiredYes

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 but 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 updated occasionally, 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 on your Linux system.

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

2. Verify that curl is successfully installed on your Linux system.

$ curl --version

3. Add the “Node.js v20.x” PPA provided by NodeSource to your Linux system (Check this GitHub repo for other Node.js versions and supported Linux distributions).

$ curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -   #On Debian or Ubuntu
$ curl -fsSL https://rpm.nodesource.com/setup_20.x | sudo bash -       #On Red Hat or Fedora

4. Now, execute the following command to install Node.js (NPM is included).

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

5. Lastly, verify the current version of Node.js and the NPM package manager on your Linux system.

$ 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.

$ 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 snap packages, but unfortunately it is “v18.16.1“, so you can install Node.js to use it for certain things, but it is not recommended to use it on production environments.

$ 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

$ sudo apt-get purge nodejs && sudo rm -r /etc/apt/sources.list.d/nodesource.list        #Debian, Ubuntu, or Pop!_OS
$ sudo dnf remove nodejs && sudo rm -r /etc/yum.repos.d/nodesource*.repo                #Red Hat, Fedora, or AlmaLinux

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.

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.