Install Node.js featured image

Installing Node.js on CentOS 8: a Complete Guide

Introduction

Node.js is a free-to-use, open-source server environment. It is commonly used for browser-based web development purposes. It allows developers to use JavaScript to do backend programming. Since most developers are intimately familiar with JavaScript, it makes things very easy. The program lets you add, modify, and delete files on the server, produce dynamic content, and collect data. In this tutorial, we will discuss how you can set up Node.js on a CentOS 8 server. We will cover all the different ways you can go about performing this installation. If you are an Ubuntu user, you can also read about how to install Node.js on Ubuntu 18.04 over here.

Before Installation

Before you think about installing the program, you have to take care of a few prerequisites. For example, you have to make sure that you are using a server with CentOS 8. You also need to log in as the non-root user that is sudo-enabled. You can follow our tutorial on configuring the Linux sudoers file to help you set that up. Once you ensure this, you can move on to installing the program on your system.

Different Ways to Install Node

You have three different options when it comes to installing Node.js for your server. The first option for you is to use the default AppStream repository from CentOS. You have to use dnf to install the nodejs package from the CentOS repository. The second option is for you to use the Node Version Manager, or the nvm, to install the program. With this installation approach, you can install and manage many different versions of node. The third and last approach is for you to build and install node from the source. Next, we are going to explain in detail each option, so you can choose the one that suits you best.

Let’s start!

How to Install Node Using the CentOS AppStream Repository

Let’s further explore how you can install Node.js from the AppStream repository of CentOS. You will be glad to know that there are multiple versions of Node.js available on the AppStream software repository. The way to install each version is by enabling the relevant module stream. You can view all the streams with the dnf command like this:

Node.js 1

This shows that there are currently three available streams. These are 10, 12, and 14. The [d] symbol next to 10 stands for default. It means that stream version 10 is the default stream. It is possible for you to switch between module streams. To install Node.js 12, you have to use the following command:

After running this command, you have to confirm the decision. This will enable the version 12 stream for Node.js. Now you can finally download your  nodejs package using dnf as follows:

You will confirm your decision once again. To install the program, press y and then the ENTER key. If you want to confirm that the software was installed, you have to ask the node for the version number like this:

If you installed version 10, then the output will show something like this:

installed version

The version will, understandably, be different if you installed nodejs 10 instead of 12. When it comes to choosing between the two, you can confidently select either version. They are considered as long-term support releases. This means that the source will continue to update and maintain the software for a considerable period of time. If you want to know more, you can read about it on the official website over here. Another thing that comes with the nodejs package installation is the Node Package Manager. It is also known as the npm utility. It is a dependency that you will need later. To make sure you installed it, verify it with this command:

How to Install Node Via the Node Version Manager

The second method is to use the Node Version Manager, or the nvm, to install Node.js. You can use this software to install several versions of Node.js at a time. It also houses the related node packages. You can download nvm from its official GitHub page. Locate the README file on the main page. Copy the curl command. This will give you access to the latest version of the program’s installation script. Remove | bash from the end of the command to audit the script. Next, send the command through to bash:

Double-check the script for the changes it is making. Then, run the command once more with the | bash segment placed at the end of the script. You can execute the script as follows:

As a result, the nvm script will install on your account. You can use it after accessing it from the .bash_profile file like this:

To know all the available Node versions, use this command with nvm:

You can install any version of Node from this available list. All you have to do is type the relevant version with the install command like so:

In our example, we are installing version 13.6.0 of Node. You can see the versions you currently have installed with this command:

nvm list

The first line always shows the version of Node that is currently in use. In this case, it is (-> v13.6.0). After that, we see some aliases and the version they signify. It will also show you aliases for a number of long-term support releases of Node. Let’s say we want to install the latest long-term support version- erbium. You should type the following:

You can switch between different installed versions as required. In addition, you can use nvm use for this like so:

Now using node v13.6.0 (npm v6.13.4). Use this command to verify your successful installation:

The output confirms that we installed the correct version of the program.

How to Install Node Through the Source

The third way to install Node.js is to compile it on your own after downloading the source code. You can find the link and copy it from the official download page for Node.js. Open your home directory in the SSH session:

Enter curl with the copied link and add | tar xz at the end:

Now access the new source directory with this:

Next, you have to compile the code. To do so, you need to download some packages from the CentOS repositories. You can install them using dnf:

Confirm your decision to install the packages. Now type y and then ENTER. Let’s start configuring the software:

If you are using a four-core server, the compilation can take around 30 minutes. Adding the -j4 option runs four parallel compilation processes. Once the compilation is complete, install the software:

Like previous sections, verify the installation by querying the version number:

The packages are installed successfully if you see the correct version as output. You will also have an associated npm available with Node.

Conclusion

We covered all the basics when it comes to installing Node.js on your system. Now you are aware of the details of each of the three methods you can use to install the program. The first being through the CentOS AppStream repository, the second being using the Node Version Manager, and lastly by compiling from the source code. Now you can capitalize on the benefits of the Node.js program with JavaScript. For more on programming with JavaScript, take a look at the related blog posts from our blog:

Happy Computing!