In this article, you will learn how to install Nodejs 16 / Node.js 14 on Rocky Linux 8 with NPM. Nodejs is a cross-platform with JavaScript runtime built on Chrome’s V8 JavaScript engine and is used to build scalable network applications. Node.js is not only used on the back-end, but also as a full-stack and front-end solution.

npm, an abbreviation of Node Package Manager is the default package manager for Node.js and also the largest repository for open-source Node.js packages.

Before we commence on Node.js installation, ensure that;

  1. You have a user with sudo privileges
  2. Your system is updated

Install Node.js 14 on Rocky Linux 8

Nodejs is available in the default Rocky Linux repositories. But the available versions are not up to date. To view the available list, run:

dnf module list nodejs

Output:

$ dnf module list nodejs
Rocky Linux 8 - AppS     [===                 ] ---  B/s |   0  B     --:-- ETA
Rocky Linux 8 - AppStream                        60 kB/s | 7.8 MB     02:13    
Rocky Linux 8 - BaseOS                          528 kB/s | 3.5 MB     00:06    
Rocky Linux 8 - Extras                          1.3 kB/s | 3.8 kB     00:02    
Docker CE Stable - x86_64                        18 kB/s |  14 kB     00:00    
Rocky Linux 8 - AppStream
Name      Stream    Profiles                                Summary             
nodejs    10 [d]    common [d], development, minimal, s2i   Javascript runtime  
nodejs    12        common [d], development, minimal, s2i   Javascript runtime  
nodejs    14        common [d], development, minimal, s2i   Javascript runtime  

Hint: [d]efault, [e]nabled, [x]disabled, [i]nstalled

From the above output, we have seen various versions listed and among them is Node.js 14 which is the latest version on AppStream. Installing Node.js 14 on Rocky Linux 8, is easier. One does not need to add repositories. Just download it from apptream repositories with:

sudo yum module install nodejs:14

Dependency tree

Dependencies resolved.
=========================================================================
 Package Arch   Version                                  Repo       Size
=========================================================================
Installing group/module packages:
 nodejs  x86_64 1:14.16.0-2.module+el8.3.0+100+234774f7  appstream  11 M
 npm     x86_64 1:6.14.11-1.14.16.0.2.module+el8.3.0+100+234774f7
                                                         appstream 3.7 M
Installing weak dependencies:
 nodejs-docs
         noarch 1:14.16.0-2.module+el8.3.0+100+234774f7  appstream 7.9 M
 nodejs-full-i18n
         x86_64 1:14.16.0-2.module+el8.3.0+100+234774f7  appstream 7.5 M
Installing module profiles:
 nodejs/common
                                                                        
Enabling module streams:
 nodejs         14                                                      

Transaction Summary
=========================================================================
Install  4 Packages

Total download size: 30 M
Installed size: 138 M
Is this ok [y/N]: y

After a successful installation, check the installed version of Node.js installed.

$ node -v
v14.17.3

Install Node.js 16 on Rocky Linux 8

Installing Node.js 16 requires adding repositories since it is not available in the default AppStream repositories.

1. Install Nodejs and npm using Yum repositories:

curl -sL https://rpm.nodesource.com/setup_16.x | sudo -E bash -

With these repositories added, one can now install Node.js 16 using:

sudo dnf install nodejs

Dependency tree:

Dependencies resolved.
================================================================================
 Package            Arch   Version                             Repository  Size
================================================================================
Installing:
 nodejs             x86_64 2:16.4.1-1nodesource                nodesource  31 M
Upgrading:
 platform-python-pip
                    noarch 9.0.3-19.el8.rocky                  baseos     1.7 M
Installing dependencies:
 python3-pip        noarch 9.0.3-19.el8.rocky                  appstream   19 k
 python3-setuptools noarch 39.2.0-6.el8                        baseos     162 k
 python36           x86_64 3.6.8-2.module+el8.4.0+597+ddf0ddea appstream   18 k
Enabling module streams:
 python36                  3.6                                                 

Transaction Summary
================================================================================
Install  4 Packages
Upgrade  1 Package

Total download size: 33 M
Is this ok [y/N]: y

Add the GPG key:

Importing GPG key 0x34FA74DD:
 Userid     : "NodeSource <[email protected]>"
 Fingerprint: 2E55 207A 95D9 944B 0CC9 3261 5DDB E8D4 34FA 74DD
 From       : /etc/pki/rpm-gpg/NODESOURCE-GPG-SIGNING-KEY-EL
Is this ok [y/N]: y

The command above installs both Nodejs 16 and npm. Check the installed version of Nodejs on Rocky Linux 8

$ node -v
v16.4.1

Also check the version on npm installed on Rocky Linux 8 using this command:

$ npm -v
7.18.1

2. Install Nodejs and npm using NVM.

From NVM (Node Version Manager), one can install and uninstall a specific version of Node.js. This is achieved in a series of steps as demonstrated below:

Step 1: Install NVM

Download and install NVM with the following command:

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

Sample output:

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

As per the output running the above commands adds the path to nvm to the current session. After running the above commands from the output. Check the installed version of nvm.

$ nvm --version
0.35.3

Step 2: Install Nodejs and npm

Now with nvm installed, we can proceed and install Node.js on Rocky Linux 8.

nvm install node

Sample output:

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

As seen, the above command installs the latest version of Node.js

Check the installed version

$ node --version
v16.4.2

WIth NVM, one can still install multiple Node.js versions. Let us say we want to install the latest (LTS) version of Node.js and also install Node.js 10. Beginning with the latest version available:

$ nvm install --lts
Installing latest LTS version.
Downloading and installing node v14.17.3...
Downloading https://nodejs.org/dist/v14.17.3/node-v14.17.3-linux-x64.tar.xz...
######################################################################### 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v14.17.3 (npm v6.14.13)

Now let us install a multiple Node.js (Node.js 10)

$ nvm install 10
Downloading and installing node v10.24.1...
Downloading https://nodejs.org/dist/v10.24.1/node-v10.24.1-linux-x64.tar.xz...
######################################################################### 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v10.24.1 (npm v6.14.12)

With the multiple versions installed, list the installed Node.js versions using:

$ nvm ls
->     v10.24.1
       v14.17.3
        v16.4.2
         system
default -> node (-> v16.4.2)
node -> stable (-> v16.4.2) (default)
stable -> 16.4 (-> v16.4.2) (default)
iojs -> N/A (default)
unstable -> N/A (default)
lts/* -> lts/fermium (-> v14.17.3)
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
lts/erbium -> v12.22.3 (-> N/A)
lts/fermium -> v14.17.3

From the output, v10.24.1 is the default version of Node.js used in the current shell. To switch to another version say we want use version 16.4.2. Issue this command:

$ nvm use 16.4.2
Now using node v16.4.2 (npm v7.18.1)

To make a version of Node.js the default version let us say we want to make version 16.4.2 default:

$ nvm alias default 16.4.2
default -> 16.4.2 (-> v16.4.2)

Install Node.js development Tools

To use npm to build native modules, one requires development tools and libraries.Install them using this command:

sudo yum install gcc-c++ make

Dependency Tree:

Dependencies resolved.
================================================================================
 Package                Arch       Version                  Repository     Size
================================================================================
Installing:
 gcc-c++                x86_64     8.4.1-1.el8              appstream      12 M
 make                   x86_64     1:4.2.1-10.el8           baseos        497 k
Upgrading:
 glibc                  x86_64     2.28-151.el8             baseos        3.6 M
 glibc-common           x86_64     2.28-151.el8             baseos        1.3 M
 glibc-langpack-en      x86_64     2.28-151.el8             baseos        825 k
 libgcc                 x86_64     8.4.1-1.el8              baseos         77 k
 libgomp                x86_64     8.4.1-1.el8              baseos        203 k
 libstdc++              x86_64     8.4.1-1.el8              baseos        450 k
Installing dependencies:
 binutils               x86_64     2.30-93.el8              baseos        5.8 M
 cpp                    x86_64     8.4.1-1.el8              appstream      10 M
 gcc                    x86_64     8.4.1-1.el8              appstream      23 M
 glibc-devel            x86_64     2.28-151.el8             baseos        1.0 M
 glibc-headers          x86_64     2.28-151.el8             baseos        477 k
 isl                    x86_64     0.16.1-6.el8             appstream     834 k
 kernel-headers         x86_64     4.18.0-305.7.1.el8_4     baseos        7.1 M
 libmpc                 x86_64     1.1.0-9.1.el8            appstream      60 k
 libpkgconf             x86_64     1.4.2-1.el8              baseos         34 k
 libstdc++-devel        x86_64     8.4.1-1.el8              appstream     2.0 M
 libxcrypt-devel        x86_64     4.1.1-4.el8              baseos         24 k
 pkgconf                x86_64     1.4.2-1.el8              baseos         37 k
 pkgconf-m4             noarch     1.4.2-1.el8              baseos         16 k
 pkgconf-pkg-config     x86_64     1.4.2-1.el8              baseos         14 k

Transaction Summary
================================================================================
Install  16 Packages
Upgrade   6 Packages

Total download size: 71 M
Is this ok [y/N]: y

Getting started with Node.js

Once we have installed Node.js, let us build a simple web server. First let us create a file example.js

vi example.js

In the file add the content below.

const http = require('http');

const hostname = '0.0.0.0';
const port = 3000;

const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello World');
});

server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});

Here we have created a simple web server to print hello word. Let’s run the web server:

node example.js

The server is running on port 3000

Now allow the port through the firewall

sudo firewall-cmd --zone=public --add-port=3000/tcp --permanent
sudo firewall-cmd --reload

Access the web server using http://IP_Address:3000 on your browser. The output is similar to this.

Conclusion

In this article, we have installed various versions of Node.js on Rocky Linux 8. I have demonstrated different ways of installing Node.js and npm on Rocky Linux 8. I have also demonstrated how to switch between Node.js versions and how to make a version default. I hope this was helpful.

Udemy Node.JS Programming Video Courses

View more articles on our website:

How To Install Node.js on CentOS 8 | CentOS 7

How To Install Node.js on Amazon Linux 2

LEAVE A REPLY

Please enter your comment!
Please enter your name here