Node.js is a JavaScript runtime for server-side programming. It allows developers to create scalable backend functionality using JavaScript, a language many are already familiar with from browser-based web development.
To make your study more useful, contact the Eldernode team as a VPS provider to have your own Linux Virtual Server.
In the following of this tutorial you will see three different ways of getting Node.js installed on a CentOS 8 server:
using dnf to install the nodejs package from CentOS’s default AppStream repository.
installing nvm, the Node Version Manager, and using it to install and manage multiple versions of node.
building and installing node from the source.
If you’re a developer or otherwise need to manage multiple installed versions of Node, use the nvm method. Building from source is rarely necessary for most users. Of course, they should use dnf to install the built-in pre-packaged versions of Node.
To let this tutorial work better, please consider the below Prerequisites:
- a non-root user with sudo privileges.
- To set up, follow our initial set up centos 8 guide.
Table of Contents
How to install Node.js on CentOS 8
Let’s walk through this guide to learn How to install Node.js on CentOS 8. In case you are working with windows, you can find your target guide on How to Install node.js on Windows.
How To Install Node From The CentOS AppStream Repository
Node.js is available from CentOS 8’s default AppStream software repository. But since there are multiple versions available, you can choose between them by enabling the appropriate module stream.
Firstly, use the following command to list out the available streams for the nodejs module.
sudo dnf module list nodejs
Name Stream Profiles Summary nodejs 10 [d] common [d], development, minimal, s2i Javascript runtime nodejs 12 common, development, minimal, s2i Javascript runtime
As you see, two streams are available, 10 and 12. The [d] indicates that version 10 is the default stream. If you’d prefer to install Node.js 12, switch module streams now:
sudo dnf module enable nodejs:12
Afterward, you must confirm your decision. So the version 12 stream will be enabled and we can continue with the installation.
Now, install the nodejs package with dnf:
sudo dnf install nodejs
Again, dnf will ask you to confirm the actions it will take. Press y then ENTER to do so, and the software will install.
Check that the install was successful by querying node for its version number:
node --version
v12.13.1
Note: Your –version output will be different if you installed Node.js 10 instead. And both available versions of Node.js are long-term support releases, meaning they have a longer guaranteed window of maintenance
Please consider that installing the nodejs package should also install the npm Node Package Manager utility as a dependency. Verify that it was installed properly as well:
npm --version
6.12.1
Point: you have successfully installed Node.js and npm using the CentOS software repositories.
How To Install Node Using the Node Version Manager
Let’s verify the other way of installing Node.js that is particularly flexible is to use nvm, the Node Version Manager. This piece of software 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 8 machine, visit the project’s GitHub page. Copy the curl command from the README file that displays on the main page. This will get you the most recent version of the installation script.
And also it is always a good idea to audit the script to make sure it isn’t doing anything you don’t agree with, before piping the command through to bash. You can do that by removing the | bash segment at the end of the curl command:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.3/install.sh
Then, you need to make sure you are comfortable with the changes it is making. When you are satisfied, run the command again with | bash appended at the end. The URL you use will change depending on the latest version of NVM, but as of right now, the script can be downloaded and executed by typing:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.3/install.sh | bash
By this, you can install the nvm script to your user account. To use it, you must first source your .bash_profile file.
source ~/.bash_profile
Next, ask NVM which versions of Node are available.
nvm list-remote
. . . 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) v13.0.0 v13.0.1 v13.1.0 v13.2.0 v13.3.0 v13.4.0 v13.5.0 v13.6.0 v13.7.0 v13.8.0 v13.9.0 v13.10.0 v13.10.1 v13.11.0 v13.12.0
As you see, it’s a very long list! You can install a version of Node by typing any of the released versions you see. For instance, to get version v13.6.0, you can type:
nvm install v13.6.0
Type the below command to see the different versions you have installed.
nvm list
-> v13.6.0 default -> v13.6.0 node -> stable (-> v13.6.0) (default) stable -> 13.6 (-> v13.6.0) (default)
This shows the currently active version on the first line (-> v13.6.0), followed by some named aliases and the versions that those aliases point to.
Note: if you also have a version of Node installed through the CentOS software repositories, you may see a system -> v12.13.1 (or some other version number) line here. You can always activate the system version of Node using nvm use system.
Additionally, you’ll see aliases for the various long-term support (or LTS) releases of Node:
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)
We can install a release based on these aliases as well. For instance, to install the latest long-term support version, erbium, run the following:
nvm install lts/erbium
Downloading and installing node v12.16.1... . . . Now using node v12.16.1 (npm v6.13.4)
Also, you can switch between installed versions with nvm use:
nvm use v13.6.0
Now using node v13.6.0 (npm v6.13.4)
Then, verify that the install was successful using the same technique from the other sections
node --version
v13.6.0
At this point, the correct version of Node is installed on our machine as we expected. A compatible version of npm is also available.
How To Install Node from Source
In this step, we will verify another way to install Node.js is to download the source code and compile it yourself.
To do so, use your web browser to navigate to the official Node.js download page, right-click on the Source Code link and click Copy Link Address or whichever similar option your browser gives you.
Back in your SSH session, first, make sure you’re in a directory you can write to. You will use the current user’s home directory:
cd ~
Then type curl, paste the link that you copied from the website, and follow it with | tar xz:
curl https://nodejs.org/dist/v12.16.1/node-v12.16.1.tar.gz | tar xz
This will use the curl utility to download the source, then pipe it directly to the tar utility, which will extract it into the current directory.
Now, move into the newly created source directory:
cd node-v*
There are a few packages that you need to download from the CentOS repositories in order to compile the code. To install these, use the following command:
sudo dnf install gcc-c++ make python2
Now, you must confirm the installation. Type y then ENTER to do so. Also, you can configure and compile the software:
./configure make -j4
The compilation will take around 30 minutes on a four-core server. You have used the -j4 option to run four parallel compilation processes. You can omit this option or update the number based on the number of processor cores you have available.
Use the following command to install the software onto your system, when you finished the compilation.
sudo make install
You can also ask Node to display its version number to make sure the installation was successful.
node --version
v12.16.1
conclusion
In this article, you reviewed 3 options for installing Node.js on CentOS 8. Notice that if you see the correct version number, then the installation was completed successfully. By default Node also installs a compatible version of npm, so that should be available as well. If you need to read more about this subject, find our related articles on How to install Node.js on Debian 10 AND How to Install and config Node.js on Ubuntu 20.04.
Is it possible to use package manager to install node.js?
Yes, it is. Since Node.js is available in the official package repository of CentOS 8, you can easily install it on CentOS 8 using DNF or YUM package manager. Use the following command after updating the CentOS 8 package repository and press Y and then press
sudo dnf install nodejs npm
What versions are available now?
You can find two available versions of Node.js:
• v12.x (Long Term Supported)
• v13.x (Current Latest Version)
To check online, you can visit the Official page to find the latest version of Node.js
What about using Firewall?
You need to allow the port 9000 in the firewall to access the web application from external machines:
firewall-cmd --permanent --add-port=9000/tcp
firewall-cmd --reload
I appreciate for this tutorial. I have installed a specific version of Node.js and i want to change to another one please.
You need to specify the new version to install and the system will take care of the rest. Type the command below with assuming if you are switching back to version 10.
sudo dnf module install nodejs:10/default
I have a problem in installing .js via binary archive on Linux and I need the correct output to check.
First, let see what should be done to install it:
Unzip the binary archive to any directory you want to install Node, I use /usr/local/lib/nodejs
The output should display like the below:
VERSION=v10.15.0
DISTRO=linux-x64
sudo mkdir -p /usr/local/lib/nodejs
sudo tar -xJvf node-$VERSION-$DISTRO.tar.xz -C /usr/local/lib/nodejs