Home Nodejs How To Install NodeJS On Linux

How To Install NodeJS On Linux

By sk
Published: Last Updated on 80.7K views

In this tutorial, we will see a brief introduction to NodeJS and then different ways to install Nodejs on Linux distributions including Arch Linux, Debian, Ubuntu, RHEL, CentOS, Fedora etc.

What is NodeJS?

NodeJS is an open source, cross-platform, and lightweight JavaScript run-time environment that can be used to build scalable network applications.

It is fast and efficient server-side software built on Chrome's V8 JavaScript engine.

Initially, JavaScript was primarily used for client-side scripting. But, Nodejs enables JavaScript to be used for server-side scripting, and runs scripts server-side to produce dynamic web pages.

Another notable thing is Nodejs has a command-line utility called npm, a package manager to install, manage nodejs libraries and applications.

Nodejs package ecosystem is the largest ecosystem of open source libraries in the world.

Install NodeJS on Linux

There are a few ways to install Nodejs in Linux. Here I have listed 3 methods.

The first method is installing Nodejs using NVM. It is the officially recommended way to install Nodejs. Especially, the first method is the best way to avoid permission issues while installing packages globally

The second method is to install Nodejs using the distribution's package manager. This method is suitable for those who wants to use the stable NodeJS version.

And, the third and final method describes how to install Nodejs from source. This is suitable for those who wants to get hands on latest nodejs version.

1. Install Nodejs on Linux using NVM (Recommended method)

This is the recommended way to install Nodejs. Furthermore, it is the best way to avoid permissions issues.

NVM (Node Version Manager) is a bash script used to manage multiple Node.js versions. It allows us to install, uninstall node.js, and switch from one version to another.

Good thing is we can install any available Node.js version of our choice using NVM.

To install nvm in Linux, use the latest install script from here.

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

Or,

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

The above command will clone the nvm repository to ~/.nvm and add the source line to your profile (~/.bash_profile, ~/.zshrc, ~/.profile, or ~/.bashrc).

Sample output:

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 15037  100 15037    0     0  31969      0 --:--:-- --:--:-- --:--:-- 31925
=> Downloading nvm from git to '/home/ostechnix/.nvm'
=> Cloning into '/home/ostechnix/.nvm'...
remote: Enumerating objects: 355, done.
remote: Counting objects: 100% (355/355), done.
remote: Compressing objects: 100% (302/302), done.
remote: Total 355 (delta 39), reused 170 (delta 28), pack-reused 0
Receiving objects: 100% (355/355), 228.96 KiB | 1.16 MiB/s, done.
Resolving deltas: 100% (39/39), done.
* (HEAD detached at FETCH_HEAD)
  master
=> Compressing and cleaning up git repository

=> Appending nvm source string to /home/ostechnix/.bashrc
=> Appending bash_completion source string to /home/ostechnix/.bashrc
=> Close and reopen your terminal to start using nvm or run the following to use it now:

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

Restart your Terminal before start using NVM. If you are on SSH, exit from the current session and log in again.

To verify whether NVM is installed or not, run:

$ command -v nvm

Sample output:

nvm

It should output 'nvm' if the installation was successful.

Now, we can install Nodejs and npm.

First, run the following command to view the list of available Nodejs versions:

$ nvm ls-remote

Sample output:

[...]
v17.9.0
v17.9.1
v18.0.0
v18.1.0
v18.2.0
v18.3.0
v18.4.0
v18.5.0
v18.6.0
v18.7.0

To install/update to the most recent Nodejs version in Linux, just run:

$ nvm install node

Sample output:

Downloading and installing node v18.7.0...
Downloading https://nodejs.org/dist/v18.7.0/node-v18.7.0-linux-x64.tar.xz...
######################################################################### 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v18.7.0 (npm v8.15.0)
Creating default alias: default -> node (-> v18.7.0)

As of writing/updating this guide, the latest version was 18.7.0.

1.1. Install specific Node version

You can also install any specific version of your choice, for example v9.3.0, like below.

$ nvm install v9.3.0

Sample output:

Downloading and installing node v9.3.0...
Downloading https://nodejs.org/dist/v9.3.0/node-v9.3.0-linux-x64.tar.xz...
######################################################################## 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v9.3.0 (npm v5.6.0)
Creating default alias: default -> v9.3.0

Similarly, you can install any number of versions you want.

1.2. List installed Node versions

To view the list of installed Nodejs versions, run:

$ nvm list

Sample output:

->      v18.7.0
default -> node (-> v18.7.0)
iojs -> N/A (default)
unstable -> N/A (default)
node -> stable (-> v18.7.0) (default)
stable -> 18.7 (-> v18.7.0) (default)
lts/* -> lts/gallium (-> N/A)
lts/argon -> v4.9.1 (-> N/A)
lts/boron -> v6.17.1 (-> N/A)
lts/carbon -> v8.17.0 (-> N/A)
lts/dubnium -> v10.24.1 (-> N/A)
lts/erbium -> v12.22.12 (-> N/A)
lts/fermium -> v14.20.0 (-> N/A)
lts/gallium -> v16.17.0 (-> N/A)

The arrow mark in the above output shows the default node version. As you see in the above output, I have installed 18.7.0 version.

1.3. Check installed Node version

To view the currently installed and default Nodejs version, run:

$ node --version

Or,

$ node -v

Sample output:

v18.7.0

Check npm version:

$ npm -v

Sample output:

8.15.0

1.4. Switch between different node versions

If you have installed more than one version, you can switch between different Nodejs versions as below.

$ nvm use node

Or you can just run it to be more specific:

$ nvm run node v17.0.0

1.5. Set default Node version

To set a particular Nodejs version as the default, run:

$ nvm alias default v18.7.0

Sample output would be:

default -> v18.7.0

1.6. Update npm

Once in a while, you can check and update npm to latest available version using the following command:

$ npm install -g npm@latest

1.7. Remove Node

Before removing Node, first make sure whether or not the version you are about to remove is the current active version using command:

$ nvm current

If it is not currently-active version, simply remove it using command:

$ nvm uninstall <node_version>

Example:

$ nvm uninstall v17.0.0

If you try to remove the currently active version using command:

$ nvm uninstall node

You will see an error something like below:

nvm: Cannot uninstall currently-active node version, v18.7.0 (inferred from node).

You must deactivate the nvm first using command:

$ nvm deactivate

And then try to uninstall node:

$ nvm uninstall node

Sample output:

Uninstalled node v18.7.0

2. Install Nodejs on Linux using your distribution's package manager (Stable, but outdated versions)

Nodejs is available in the default repositories of most Linux distributions. It might not be latest version, but stable.

If you want to have a stable Node.js on your Linux, you better install it using your distribution's package manager as shown below.

On Arch Linux and its derivatives like Antergos, Manajaro Linux, run the following command to install it:

$ sudo pacman -S nodejs npm

On Debian, Ubuntu, Linux Mint:

$ sudo apt-get install nodejs npm

On RHEL, and its clones like CentOS, AlmaLinux, and Rocky Linux, you need to enable EPEL repository first.

$ sudo dnf install epel-release

And, then install Nodejs using command:

$ sudo dnf install nodejs npm

On Fedora you don't need to enable EPEL repository, because it is already activated by default. So you can install Nodejs and Npm by simply running the following command:

$ sudo dnf install nodejs npm

Heads Up: Since the packages from the default repositories are outdated, you will get the following error when you try to install any NodeJS modules using npm.

/usr/bin/env: ‘node’: No such file or directory

To solve this error, you need to create symlink as shown below.

$ sudo ln -s /usr/bin/nodejs /usr/bin/node

3. Install Nodejs on Linux from NodeSource

Like I already said, nodejs is available in the default repositories, but it might be bit outdated. To install the most recent version, install the latest version from NodeSource.

Add the latest Nodejs repository as shown here depending on the Linux distribution you use.

On Debian, Ubuntu and derivatives:

Add NodeJs 18.x repository:

## As Non-root user
curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -

## As Root user
curl -fsSL https://deb.nodesource.com/setup_18.x | bash -

Nodejs 16.x repository:

## As non-root
curl -fsSL https://deb.nodesource.com/setup_16.x | sudo -E bash -

## As root
curl -fsSL https://deb.nodesource.com/setup_16.x | bash -

Nodejs 14.x:

## As non-root
curl -fsSL https://deb.nodesource.com/setup_14.x | sudo -E bash -

## As root
curl -fsSL https://deb.nodesource.com/setup_14.x | bash -

Nodejs LTS (16.x):

## As non-root
curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo -E bash -

## As root
curl -fsSL https://deb.nodesource.com/setup_lts.x | bash -

For Nodejs current version (18.x):

##As non-root
curl -fsSL https://deb.nodesource.com/setup_current.x | sudo -E bash -

##As root
curl -fsSL https://deb.nodesource.com/setup_current.x | bash -

After adding the repository, install Node.js and Npm using command:

## As non-root user
sudo apt-get install nodejs npm

#As root user
apt-get install nodejs npm

On Fedora, RHEL, CentOS, AlmaLinux, Rocky Linux:

Nodejs 18.x:

# As root
curl -fsSL https://rpm.nodesource.com/setup_18.x | bash -

# No root privileges
curl -fsSL https://rpm.nodesource.com/setup_18.x | sudo bash -

Nodejs 16.x:

# As root
curl -fsSL https://rpm.nodesource.com/setup_16.x | bash -

# No root privileges
curl -fsSL https://rpm.nodesource.com/setup_16.x | sudo bash -

NodeJS 14.x

# As root
curl -fsSL https://rpm.nodesource.com/setup_14.x | bash -

# No root privileges
curl -fsSL https://rpm.nodesource.com/setup_14.x | sudo bash -

NodeJS LTS (16.x)

# As root
curl -fsSL https://rpm.nodesource.com/setup_lts.x | bash -

# No root privileges
curl -fsSL https://rpm.nodesource.com/setup_lts.x | sudo bash -

NodeJS current release (18.x):

# As root
curl -fsSL https://rpm.nodesource.com/setup_current.x | bash -

# No root privileges
curl -fsSL https://rpm.nodesource.com/setup_current.x | sudo bash -

Then install Nodejs using command:

# As Non-root user
sudo dnf install nodejs npm

# As root user
yum dnf nodejs npm

Install build tools (Optional)

To compile and install native addons from npm repository, you may also need to install build tools.

To install build tools on Debian, Ubuntu distributions, run the following command:

$ sudo apt install -y build-essential

On Fedora and RHEL based systems:

$ sudo dnf groupinstall 'Development Tools'

Conclusion

In this guide, we discussed 3 different ways to install Nodejs and Npm in Debian-based and RHEL-based Linux distributions. As you can see, installing Nodejs is fairly easy. You can now build and run Nodejs apps in your Linux machine.

Resources:

You May Also Like

6 comments

Abdur November 30, 2019 - 12:55 pm

I follow your guidance and it works perfectly. Thank you very much.

Reply
Param February 2, 2020 - 8:54 pm

Thanks a lot. That Perfect Instructions That are useful to every user on almost every OS who want to install Node.

Reply
Jalal May 17, 2020 - 4:09 pm

Hi,
Thanks a lot….

Reply
Prince Alarming May 25, 2020 - 9:51 pm

Thanks SK for this article!! The only thing it doesn’t broach is how to create a shortcut to boot Node.js in Linux or have a window with menus’ to work with in Linux(Microshaft’s Winblows(sorry I don’t like)made me lazy). I know you can download a windows version and run it through Wine and PlayOnLinux but I noticed Node.js detects system before download!!

Reply
Oliver May 12, 2021 - 3:25 pm

I followed the steps and setup Nodejs easily. Thanks a bunch.

Reply
Well-Wisher January 27, 2022 - 9:50 pm

Thank-You very much.

Reply

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. By using this site, we will assume that you're OK with it. Accept Read More