How To Install Node.JS on CentOS/RHEL 8

This tutorial will show you how to install Node.js using three different options on your CentOS/RHEL 8. You can adopt any of the following methods to install Node.js on your system.


To follow this guide along, you will need one (physical or virtual) machine running CentOS/RHEL 8 with a non-root user sudo privileges.

If you are running RHEL 8, you will need to add to extra packages for enterprise Linux (EPEL) repository to install nodejs using the dnf package manager.
sudo dnf -y install epel-release 

Install Node.JS using DNF

There are multiple nodejs versions available, and you can choose between them by enabling the appropriate module stream on your system.

First, check the available streams for the nodejs module using the below command:
sudo dnf module list nodejs
You should see below output:
Name                     Stream                   Profiles                                                Summary
nodejs                   10 [d]                   common [d], development, minimal, s2i                   Javascript runtime
nodejs                   12                       common, development, minimal, s2i                       Javascript runtime
As you can see, two streams are available, 10 and 12. The [d] indicates that version 10 is the default stream.

For instance, If you’d like to install Node.js 12, switch module streams to nodejs:12 with below command:
sudo dnf -y module enable nodejs:12
Type below to install the nodejs with below command:
sudo dnf -y install nodejs
Check that the install was successful by querying node for its version number:
node --version
You will see output similar to the below:
When you install the nodejs using dnf package manager, the npm Node Package Manager utility will automatically be installed as a dependency.

You can verify whether npm was installed with below command:
npm --version
This will print the npm version like below:
At this stage, you have successfully installed Node.js and npm using the dnf package manager on your CentOS/RHEL 8.

Install NodeJS using nvm

This section will show you how to install the nodejs using Node Version Manager (npm) on your CentOS/RHEL 8. The npm allows you to install and maintain many different independent versions of Node.js, and their associated Node packages, at the same time.

To install NVM on your CentOS/RHEL 8 machine, visit the project’s GitHub page.

Copy the curl command that displays on the main page. This will get you the most recent version of the installation script.
curl -o- | bash
The script clones the nvm repository to ~/.nvm, and attempts to add the source lines to the correct profile file (~/.bash_profile, ~/.zshrc, ~/.profile, or ~/.bashrc).

To use it, you must first source your correct profile file like below:
source ~/.bash_profile
Now, you can check which versions of nodejs are available:
nvm list-remote
You should see output similar to the following:
v12.13.0   (LTS: Erbium)
       v12.13.1   (LTS: Erbium)
       v12.14.0   (LTS: Erbium)
       v12.14.1   (LTS: Erbium)
       v12.15.0   (LTS: Erbium)
       v12.16.0   (LTS: Erbium)
       v12.16.1   (Latest LTS: Erbium)
You can install a version of Nodejs by typing any of the released versions you see. For this guide, we will install version v13.6.0 using the below command:
nvm install v13.6.0
You can see the different versions you have installed by typing:
nvm list
This will return you similar to the following output:
->      v13.6.0
default -> v13.6.0
node -> stable (-> v13.6.0) (default)
stable -> 13.6 (-> v13.6.0) (default)
The output shows the currently active version on the first line, followed by some named aliases and the versions that those aliases point to.

Note: if you also have a version of Nodejs installed through the dnf package manager, you may see a system -> v12.13.1 (or some other version number) line here. You can always activate the system version of Nodejs using the nvm use system.

Additionally, you’ll see aliases for the various long-term support (or LTS) releases of Nodejs:
lts/* -> lts/erbium (-> 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.19.0 (-> N/A)
lts/erbium -> v12.16.1 (-> N/A)
You can install a release based on these aliases as well. For example, install the latest long-term support version, erbium, with below command:
nvm install lts/erbium
You will see output similar to the following:
Downloading and installing node v12.16.1
Now using node v12.16.1 (npm v6.13.4)
You can switch between installed versions using the nvm use followed by version:
nvm use v13.6.0
This will return output similar to the below:
Now using node v13.6.0 (npm v6.13.4)

Install Node.JS using Source Code

This section will show you how to download, compile and install Node.js using source code.

Open up your preferred web browser, navigate to the official Node.js download page, right-click on the Source Code and Copy Link Address.

Return to your terminal session, download source code using curl in current user’s home directory:
cd ~
curl | tar xz
cd node-v*
sudo dnf -y install gcc-c++ make python2
Now, you can configure and compile the nodejs source code:
make -j4
When the compilation is finished, you can install nodejs on your system with below command:
sudo make install
Type below to verify that the installation was successful:
node --version
If the command return the correct version number in the output, then the installation was completed successfully.

Wrapping up

In this guide you learned how to install Node.js using the three different options on CentOS/RHEL 8.

No comments:

Powered by Blogger.