How To Install Node.js 22,20,18 on Rocky Linux 9

Programming is one of the popular skills in the tech world today. It is defined as executing a set of instructions written in a language that a computer can understand. These instructions are used to perform a specific task, for example adding numbers, displaying some information e.t.c. Humans understand natural languages such as English, Fench e.t.c. Computers understand instructions written in a specific syntax known as a programming language.

Programming was started in 1883 by Charles Babbage after developing a device to compute Bernoulli’s number. Later in 1949, a low-level assembly language was developed. This was then followed by Autocode, the first compiled computer programming language developed by Alick Glennie in 1952. In 1957, John Backus and IBM developed Fortran which was then followed by ALGOL in 1958 and COBOL in 1959. Visual Basic previously known as BASIC was developed in 1964. In 1972, the C programming language was developed. This is a general programming language that is assumed to be the mother of almost all higher-level programming languages such as Limbo, C#, D, Go, LPC, Perl, Python, PHP, Java, JavaScript, and the Unix’s C shell.

JavaScript is a lightweight, dynamic scripting language commonly used to develop complex web pages. JavaScript was first developed by Netscape in 1995 with the name LiveScript. It was later renamed probably due to the excitement generated by Java. It offers a lot of amazing features such as animated 2D/3D graphics, cross-platform, integration with Java and HTML e.t.c

NodeJS is a free and open-source asynchronous event-driven JavaScript runtime that is built on Chrome’s V8 JavaScript engine. It finds use in several areas such as:

  • Microservices
  • Server-side proxy
  • Scripting and automation
  • Build highly scalable network applications by offering both backend and full-stack front-end solutions.
  • System Monitoring Dashboard
  • Chatbots
  • Big Data and Analytics

There are several releases of Node.js available for use. The major Node.js versions enter the current release status for six months. This gives the library authors time to add support for them. After those six months, the odd-numbered releases such as 9,11 become unsupported and the even-numbered releases such as 22,20,18 move to the active LTS status.

Step 1 – Install Node.js 22,20,18 on Rocky Linux 9

By following this guide to the end, you should be able to install Node.js 22,20,18 on Rocky Linux 9. In this guide, we will offer two options on how to install Node.js:

  • Using AppStream Repositories
  • Using Nodesource Repositories
  • Using NVM

Option 1 – Install Node.js 22,20,18 on Rocky Linux 9 using AppStream Repositories.

Rocky Linux AppStream repositories provide Node.JS as a module called nodejs. Therefore, there’s no need to add or enable any third-party repositories. The downside is that the versions provided are not up to date, but will nonetheless get the job done.

To check out the available versions provided, run the command:

[root@rocky9 ~]# sudo dnf module list nodejs
Last metadata expiration check: 0:45:49 ago on Sat 04 Jan 2025 08:29:41 PM EAT.
Rocky Linux 9 - AppStream
Name                                    Stream                                  Profiles                                                               Summary
nodejs                                  18                                      common [d], development, minimal, s2i                                  Javascript runtime
nodejs                                  20                                      common [d], development, minimal, s2i                                  Javascript runtime
nodejs                                  22                                      common [d], development, minimal, s2i                                  Javascript runtime

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

A. Install Node.js 18 LTS on Rocky Linux 9

Rocky Linux 9 released recently provides Node.js 20 LTS in the AppStream repositories and can be installed with:

sudo dnf module install nodejs:18 -y

Dependency Tree:

Last metadata expiration check: 0:53:31 ago on Mon 23 Dec 2024 10:42:58 PM EAT.
Dependencies resolved.
==========================================================================================================================================================================================================
 Package                                     Architecture                      Version                                                                         Repository                            Size
==========================================================================================================================================================================================================
Installing group/module packages:
 nodejs                                      x86_64                            1:18.20.4-1.module+el9.4.0+25542+b1e8296f                                       appstream                             13 M
 npm                                         x86_64                            1:10.7.0-1.18.20.4.1.module+el9.4.0+25542+b1e8296f                              appstream                            1.8 M
Installing weak dependencies:
 nodejs-docs                                 noarch                            1:18.20.4-1.module+el9.4.0+25542+b1e8296f                                       appstream                            7.8 M
 nodejs-full-i18n                            x86_64                            1:18.20.4-1.module+el9.4.0+25542+b1e8296f                                       appstream                            8.4 M
Installing module profiles:
 nodejs/common
Enabling module streams:
 nodejs                                                                        18

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

Total download size: 31 M
Installed size: 167 M
Is this ok [y/N]: y

Once complete, verify the installation using the command:

$ node -v
v18.20.4

Also, check the npm version:

$ npm -v
10.7.0

B. Install Node.js 20 LTS on Rocky Linux 9

Rocky Linux 9 released recently provides Node.js 20 LTS in the AppStream repositories and can be installed with:

sudo dnf reset module nodejs -y && sudo dnf module install -y nodejs:20

Dependency Tree:

Last metadata expiration check: 1:28:15 ago on Sat 04 Jan 2025 08:29:41 PM EAT.
Dependencies resolved.
==========================================================================================================================================================================================================
 Package                                     Architecture                      Version                                                                         Repository                            Size
==========================================================================================================================================================================================================
Installing group/module packages:
 nodejs                                      x86_64                            1:20.17.0-1.module+el9.5.0+29018+840bbf8d                                       appstream                             14 M
 npm                                         x86_64                            1:10.8.2-1.20.17.0.1.module+el9.5.0+29018+840bbf8d                              appstream                            1.9 M
Installing weak dependencies:
 nodejs-docs                                 noarch                            1:20.17.0-1.module+el9.5.0+29018+840bbf8d                                       appstream                            8.2 M
 nodejs-full-i18n                            x86_64                            1:20.17.0-1.module+el9.5.0+29018+840bbf8d                                       appstream                            8.4 M
Installing module profiles:
 nodejs/common
Enabling module streams:
 nodejs                                                                        20

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

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

Once complete, verify the installation using the command:

$ node -v
v20.17.0

Also, check the npm version:

$ npm -v
10.8.2

C. Install Node.js 22 LTS on Rocky Linux 9

Node.js 20 is an LTS version and is provided in the Rocky Linux 9 AppStream repositories and can be installed using the command below:

 sudo dnf module reset nodejs -y && sudo dnf module install -y nodejs:22

Dependency Tree:

Last metadata expiration check: 1:32:32 ago on Sat 04 Jan 2025 08:29:41 PM EAT.
Dependencies resolved.
==========================================================================================================================================================================================================
 Package                                     Architecture                      Version                                                                         Repository                            Size
==========================================================================================================================================================================================================
Installing group/module packages:
 nodejs                                      x86_64                            1:22.11.0-1.module+el9.5.0+29662+3d9e69a6                                       appstream                            1.9 M
 npm                                         x86_64                            1:10.9.0-1.22.11.0.1.module+el9.5.0+29662+3d9e69a6                              appstream                            2.1 M
Installing dependencies:
 nodejs-libs                                 x86_64                            1:22.11.0-1.module+el9.5.0+29662+3d9e69a6                                       appstream                             19 M
Installing weak dependencies:
 nodejs-docs                                 noarch                            1:22.11.0-1.module+el9.5.0+29662+3d9e69a6                                       appstream                            8.5 M
 nodejs-full-i18n                            x86_64                            1:22.11.0-1.module+el9.5.0+29662+3d9e69a6                                       appstream                            8.4 M
Installing module profiles:
 nodejs/common
Enabling module streams:
 nodejs                                                                        22

Transaction Summary
==========================================================================================================================================================================================================
Install  5 Packages

Total download size: 40 M
Installed size: 215 M
Is this ok [y/N]: y

Verify the installation:

 $ node -v
v22.11.0

Option 2 – Install Node.js 22,20,18 on Rocky Linux 9 using AppStream Repositories.

The second option is to install Node.JS from the Node.JS binary package which is provided by nodesource. This provides the latest version of Node.JS which, at the time of writing this guide, is Node.JS v23.5.0.

A. Install Node.js 18 LTS on Rocky Linux 9

##Download the latest NodeSource repository script.
curl -fsSL https://rpm.nodesource.com/setup_18.x -o nodesource_setup.sh

##Run the NodeSource repository installation script.
sudo -E bash nodesource_setup.sh

##Install Node.js and NPM on your server.
sudo dnf install nodejs -y

##Check the node and npm versions
node -v
npm -v

B. Install Node.js 20 LTS on Rocky Linux 9

##Download the latest NodeSource repository script.
curl -fsSL https://rpm.nodesource.com/setup_20.x -o nodesource_setup.sh

##Run the NodeSource repository installation script.
sudo -E bash nodesource_setup.sh

##Install Node.js and NPM on your server.
sudo dnf install nodejs -y

##Check the node and npm versions
node -v
npm -v

C. Install Node.js 22 LTS on Rocky Linux 9

##Download the latest NodeSource repository script.
curl -fsSL https://rpm.nodesource.com/setup_22.x -o nodesource_setup.sh

##Run the NodeSource repository installation script.
sudo -E bash nodesource_setup.sh

##Install Node.js and NPM on your server.
sudo dnf install nodejs -y

##Check the node and npm versions
node -v
npm -v

Option 3 – Install Node.js 22,20,18 using NVM

Node Version Manager abbreviated as NVM allows one to install a specific version of Node.JS on their system. This is accomplished using the below steps.

First, install NVM on Rocky Linux 9:

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

Execution output:

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 16631  100 16631    0     0  31921      0 --:--:-- --:--:-- --:--:-- 31921
=> Downloading nvm as script to '/root/.nvm'

=> Appending nvm source string to /root/.bashrc
=> Appending bash_completion source string to /root/.bashrc
=> You currently have modules installed globally with `npm`. These will no
=> longer be linked to the active version of Node when you install a new node
=> with `nvm`; and they may (depending on how you construct your `$PATH`)
=> override the binaries of modules installed with `nvm`:

/usr/lib
├── [email protected]
=> If you wish to uninstall them at a later point (or re-install them under your
=> `nvm` node installs), you can remove them from the system Node as follows:

     $ nvm use system
     $ npm uninstall -g a_module

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

Source your profile:

source ~/.bashrc

Verify the installation:

$ nvm --version
0.40.1

A. Install Latest NodeJS Version using NVM

Now, you can use NVM to install any version of Node.js on your Rocky Linux 9 system. To install the latest version of Node.js, use the command:

nvm install node

Sample Output:

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

Currently, the latest version is Node.js 18. This version has been installed and set as the default Node.js version for use.

Verify the installation:

$ node --version
v23.5.0

B. Install Node.js LTS version

You can also use NVM to install the latest LTS version using the command:

$ nvm install --lts
Installing latest LTS version.
Downloading and installing node v22.12.0...
Downloading https://nodejs.org/dist/v22.12.0/node-v22.12.0-linux-x64.tar.xz...
################################################################################################################################################################################################### 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v22.12.0 (npm v10.9.0)
Creating default alias: default -> lts/* (-> v22.12.0)

The current LTS version is Node.js 16. Verify the installation.

$ node --version
v22.12.0

C. Install a Specific Node.js Version

It is p[possible to install any other preferred Node.js version, for example, Node.js 14 using the command:

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

Verify the installation.

$ node --version
v18.20.5

Step 2 – Manage Multiple Node.js Versions

NVM allows one to manage multiple Node.js versions available on the system. Begin by listing the available versions:

[root@rocky9 ~]# nvm ls
->     v18.20.5
       v22.12.0
        v23.5.0
default -> lts/* (-> v22.12.0)
iojs -> N/A (default)
unstable -> N/A (default)
node -> stable (-> v23.5.0) (default)
stable -> 23.5 (-> v23.5.0) (default)
lts/* -> lts/jod (-> v22.12.0)
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 (-> N/A)
lts/erbium -> v12.22.12 (-> N/A)
lts/fermium -> v14.21.3 (-> N/A)
lts/gallium -> v16.20.2 (-> N/A)
lts/hydrogen -> v18.20.5
lts/iron -> v20.18.1 (-> N/A)
lts/jod -> v22.12.0

From the above output, the default Node.js version is v18.20.5. This is the version used in the current shell. You can switch to another Node.js version, for example, version 18 with the command:

$ nvm use 23
Now using node v23.5.0 (npm v10.9.2)

You can also set this version as the default Node.js version:

$ nvm alias default 23
default -> 23 (-> v23.5.0)

Verify the made changes:

$ node -v
v23.5.0

Step 3 – Getting Started with Node.js

To be able to use the installed Node.js version you have to install the required development modules required by npm to build native modules.

sudo dnf install gcc-c++ make -y 

Once installed, you are set to build the desired web applications with Node.js. In this guide, we will demonstrate this by building a simple Hello World web page.

vim Helloworld.js

Add the below content to the file:

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! Node.JS is amazing!');
});

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

Save the file and allow the set port through the firewall:

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

Now start the Node.js application:

$ node Helloworld.js
Server running at http://0.0.0.0:3000/

Access the web page on the browser using the URL http://IP_Address:3000

Step 4 – Manage the Node.js application

The created Node.js application does not run in the background. It is possible to use the pm2 global NPM module to manage the Node.js application.

Begin by installing pm2

npm install -g pm2

Once the installation is complete, configure pm2 to start on boot automatically.

[root@rocky9 ~]# pm2 startup

                        -------------

__/\\\\\\\\\\\\\____/\\\\____________/\\\\____/\\\\\\\\\_____
 _\/\\\/////////\\\_\/\\\\\\________/\\\\\\__/\\\///////\\\___
  _\/\\\_______\/\\\_\/\\\//\\\____/\\\//\\\_\///______\//\\\__
   _\/\\\\\\\\\\\\\/__\/\\\\///\\\/\\\/_\/\\\___________/\\\/___
    _\/\\\/////////____\/\\\__\///\\\/___\/\\\________/\\\//_____
     _\/\\\_____________\/\\\____\///_____\/\\\_____/\\\//________
      _\/\\\_____________\/\\\_____________\/\\\___/\\\/___________
       _\/\\\_____________\/\\\_____________\/\\\__/\\\\\\\\\\\\\\\_
        _\///______________\///______________\///__\///////////////__


                          Runtime Edition

        PM2 is a Production Process Manager for Node.js applications
                     with a built-in Load Balancer.

                Start and Daemonize any application:
                $ pm2 start app.js

                Load Balance 4 instances of api.js:
                $ pm2 start api.js -i 4

                Monitor in production:
                $ pm2 monitor

                Make pm2 auto-boot at server restart:
                $ pm2 startup

                To go further checkout:
                http://pm2.io/


                        -------------

[PM2] Init System found: systemd
Platform systemd
Template
[Unit]
Description=PM2 process manager
Documentation=https://pm2.keymetrics.io/
After=network.target

[Service]
Type=forking
User=root
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity
Environment=PATH=/root/.nvm/versions/node/v23.5.0/bin:/root/.local/bin:/root/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin
Environment=PM2_HOME=/root/.pm2
PIDFile=/root/.pm2/pm2.pid
Restart=on-failure

ExecStart=/root/.nvm/versions/node/v23.5.0/lib/node_modules/pm2/bin/pm2 resurrect
ExecReload=/root/.nvm/versions/node/v23.5.0/lib/node_modules/pm2/bin/pm2 reload all
ExecStop=/root/.nvm/versions/node/v23.5.0/lib/node_modules/pm2/bin/pm2 kill

[Install]
WantedBy=multi-user.target

Target path
/etc/systemd/system/pm2-root.service
Command list
[ 'systemctl enable pm2-root' ]
[PM2] Writing init configuration in /etc/systemd/system/pm2-root.service
[PM2] Making script booting at startup...
[PM2] [-] Executing: systemctl enable pm2-root...
Created symlink /etc/systemd/system/multi-user.target.wants/pm2-root.service → /etc/systemd/system/pm2-root.service.
[PM2] [v] Command successfully executed.
+---------------------------------------+
[PM2] Freeze a process list on reboot via:
$ pm2 save

[PM2] Remove init script via:
$ pm2 unstartup systemd

Now daemonize the created Node.js application.

pm2 start Helloworld.js

Sample Output:

Save the made configurations:

[root@rocky9 ~]# pm2 save
[PM2] Saving current process list...
[PM2] Successfully saved in /root/.pm2/dump.pm2

Now your application is configured to start automatically on boot. To remove the pm2 init script using the command:

[root@rocky9 ~]# pm2 unstartup systemd
[PM2] Init System found: systemd
Removed "/etc/systemd/system/multi-user.target.wants/pm2-root.service".

Removed "/etc/systemd/system/multi-user.target.wants/pm2-root.service".

[PM2] Init file disabled.

Final Thoughts

That marks the end of this guide on how to install Node.js 22,20,18 on Rocky Linux 9. We have also seen how to set the Node.js version and demonstrated how to use Node.js to create a web page. I hope this was important.

See more:

Your IT Journey Starts Here!

Ready to level up your IT skills? Our new eLearning platform is coming soon to help you master the latest technologies.

Be the first to know when we launch! Join our waitlist now.

Join our Linux and open source community. Subscribe to our newsletter for tips, tricks, and collaboration opportunities!

Recent Post

Leave a Comment

Your email address will not be published. Required fields are marked *

Related Post

Virtualization is the creation of a virtual version of something, this is achieved by allowing hardware components of a single […]

There are many programming languages available to use. They are used by developers as a way to build programs used […]

A database is a collection of structured data that is organized in such a way that can be easily accessed, […]

Let's Connect

Unleash the full potential of your business with CloudSpinx. Our expert solutions specialists are standing by to answer your questions and tailor a plan that perfectly aligns with your unique needs.
You will get a response from our solutions specialist within 12 hours
We understand emergencies can be stressful. For immediate assistance, chat with us now

Contact CloudSpinx today!

Download CloudSpinx Profile

Discover the full spectrum of our expertise and services by downloading our detailed Company Profile. Simply enter your first name, last name, and email address.