How to Install Node.js on CentOS 8

Published on  

9 min read

Looking for other version?

Choose a different version or distribution.

How to Install Node.js on CentOS 7

This post will walk you through the installation of Node.js and NPM on CentOS/RHEL 8.

You've come to the right place if you want to learn how to create web applications with Node.js. This beginner's guide will walk you through the process of installing Node.js and NPM on CentOS/RHEL 7. This article will have you up and running in no time, whether you're a complete rookie or an experienced developer.

What is Node.js?

Node.js is a cross-platform, open-source JavaScript runtime environment that executes JavaScript code outside of a web browser. Ryan Dahl created it in 2009, and it has since become one of the most popular and extensively used web development tools.

Node.js employs an event-driven, non-blocking I/O approach that allows it to efficiently manage massive volumes of data and requests. It makes advantage of Google's V8 JavaScript engine, which compiles JavaScript into machine code, making it extremely fast.

Node.js is frequently used for developing server-side applications and web services, as well as desktop apps utilizing technologies such as Electron. It also features a robust ecosystem of modules and packages accessible via the npm package manager, allowing developers to easily add functionality to their applications.

What is NPM?

NPM is a JavaScript package management developed by npm, Inc. The Node.js JavaScript runtime environment's default package manager is npm. It consists of a command line client, often known as npm, and the npm registry, an online database of public and paid-for private packages.

Prerequisites

Before we begin, make sure you have the following prerequisites:

  • A machine running CentOS 8
  • Access to a terminal

We'll walk you through installing Node.js on CentOS 8 with the EPEL Repository and NodeSource. Finally, you will learn how to use NVM to install a specific Node.js version.

Update Your System

Before we begin, let's make sure that our system is up to date. Open a terminal window and run the following command:

sudo yum clean all && sudo yum -y update

This command will update the package list and upgrade any outdated packages on your system.

Install Development Tools

Before starting the installation procedure you can install some development packages and tools by entering following command:

sudo yum install -y gcc-c++ make curl

Option 1: How to Install Node.js and npm from the EPEL Repository on CentOS 8

EPEL (Extra Packages for Enterprise Linux) repository for CentOS and comparable distributions.

To acquire access to the EPEL repo, you must alter your installation's repo-list. Fortunately, we can adjust access to this repository by installing the epel-release package, which is present in our current repositories.

sudo yum install epel-release

You can now install Node.js using your standard yum commands now that you have access to the EPEL repository:

sudo yum install nodejs

In most cases, you’ll also want to also install npm, the Node.js package manager. You can do this by installing the npm package with yum:

sudo yum install npm

Finally, double-check the Node.js and NPM version numbers to ensure that it was successfully installed. After you enter the following commands, the command-line will return the installed version number:

node -v
npm -v

Assume you want to uninstall Node.js or NPM from your Linux system. Use the following commands if this is the case:

sudo yum remove nodejs
sudo yum remove npm

Option 2: How to Install Specific Version of Node.js using NodeSource on CentOS 8

You can use a PPA (personal package archive) maintained by NodeSource to install a different version of Node.js. These PPAs have more Node.js versions than the official CentOS repositories.

Open up your terminal and enter the following command:

Step 1. Refresh the yum cache to update the repository:

sudo yum clean all && sudo yum -y update

Step 2. Install the Curl (Optional)

Skip this step if you have cURL installed already. Otherwise, enter the following command:

sudo yum install curl

Step 3. Install Node.js from Nodesource

Install the PPA first to gain access to its packages. Curl the installation script for your preferred version from your home directory, making sure to replace 18.x with your preferred version string (if different):

curl –sL https://rpm.nodesource.com/setup_18.x | sudo bash -

You can check Nodesource Official Github repository for more information. This link has the list of all Node.js versions with RPM files for CentOS/RHEL based distributions.

Again, Refresh the yum cache to update the repository (This is an important step):

sudo yum update

Once you have the NodeSource repository, install Node.js:

sudo yum install nodejs

Use these two commands to query Node.js and NPM versions and confirm installation process was a success:

node -v
npm -v

Option 3: How to Install a Specific Version of Node.js Using NVM on CentOS 8

Node Version Manager (NVM) is a script that assists users in managing different versions of Node.js. It can be used in the process of installing and uninstalling a specific version of Node.js, allowing the user to have many versions of Node.js to test or utilize.

Follow these steps to start installing Node.js and npm with NVM on CentOS server.

Start by downloading the nvm script using the command below:

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

This script will act as a replica to the nvm repository derived from Github to ~/.nvm. Add this script to Bash or the ZSH profile.

The output should look like this:

=> 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

To confirm that nvm is installed, run the command below:

nvm --version

Now, you can ask NVM which versions of Node are available:

nvm list-remote
// Output
. . .
        v18.0.0
        v18.1.0
        v18.2.0
        v18.3.0
        v18.4.0
        v18.5.0
        v18.6.0
        v18.7.0
        v18.8.0
        v18.9.0
        v18.9.1
       v18.10.0
       v18.11.0
       v18.12.0   (LTS: Hydrogen)
       v18.12.1   (LTS: Hydrogen)
       v18.13.0   (Latest LTS: Hydrogen)
        v19.0.0
        v19.0.1
        v19.1.0
        v19.2.0
        v19.3.0
        v19.4.0

It’s a very long list. You can install a version of Node by writing in any of the release versions listed. For instance, to get version v14.10.0, you can run:

nvm install 19.1.0

You can view the different versions you have installed by listing them:

nvm list

Removing Node.js on CentOS 8

You can uninstall Node.js using yum or nvm, depending on how it was installed. To remove the version from the system repositories, use yum remove:

sudo yum remove nodejs

By default, yum remove retains any local configuration files that were created since installation. If you don’t want to save the configuration files for later use, you can enter more detailed command to clean Node.js completely from CentOS.

sudo rm -rf /var/cache/yum
sudo yum remove -y nodejs
sudo rm /etc/yum.repos.d/nodesource*
sudo yum clean all

To uninstall a version of Node.js that you installed using nvm, first determine whether it is the current active version:

nvm current

If the version you are targeting is not the current active version, you can run:

nvm uninstall node_version

This command will uninstall the selected version of Node.js.

If the version you would like to remove is the current active version, you first need to deactivate nvm to enable your changes:

nvm deactivate

Now you can uninstall the current version using the uninstall command used previously. This removes all files associated with the targeted version of Node.js.

Conclusion

And that's it! You've now successfully installed Node.js on your CentOS 8 machine. Whether you're building a small personal project or a large-scale web application, Node.js is a powerful and flexible tool that can help you get the job done. Remember to keep your system up-to-date and check your installations to make sure everything is running smoothly.

For more information on Node.js, check out the official website at nodejs.org and the npm website at npmjs.com. Thanks for reading, and happy coding!

About the Author

Rexposed Staff

Was this helpful?