We are committed to provide service 24 X 7

Deals, Shopping, Training, Tools

Learn how to – Install Node.js 18 on CentOS 8|RHEL 8|Rocky Linux 8

Learn how to – Install Node.js 18 on CentOS 8|RHEL 8|Rocky Linux 8.

We all know JavaScript as a language to write once and run anywhere. Node.js is a popular programming language mainly because it uses the JavaScript as the main application to create new application. JavaScript being the developers first choice to develop new web applications make NodeJS robust framework in creation of server side web applications. NodeJS is built on Chrome’s V8 JavaScript engine and it provides a cross-platform, non-blocking (asynchronous), event driven runtime environment for Developers to build highly scalable server-side applications using JavaScript. In this article we shall cover installation of Node.js 18 LTS CentOS 8|RHEL 8|Rocky Linux 8.

Features and Applications of NodeJS

Some cool features of NodeJS are:

  • NodeJS is a single threaded language – It uses multiple threads to execute asynchronous code
  • NodeJS is Non blocking in nature – It executes the next tasks in the stack while waiting for a response on previous
  • It is asynchronous and event-driven – All NodeJS APIs are asynchronous. NodeJS server never waits for an API to return data and uses Events mechanism to get response
  • Availability of a Package Manager (NPM) – NPM is the largest software repository for NodeJS with over 700,000 code packages. You can quickly install distributed packages in seconds using npm package manager. No registration or login required to download publicly published packages.
  • NodeJS is lightweight and Scalable – Node.js being lightweight makes it fit for use in developing microservices. You also get better flexibility by breaking a monolith application into microservices, and the ease to scale.
  • No Buffering in NodeJS – Applications written in NodeJS never buffer any data. This means a user can easily stream videos without interruption as data is output in chunks.

The main applications of NodeJS is in the following key areas

  • Creating Web applications – good fit for creating Single Page Applications
  • Creating Chat Applications – With NodeJS you can build real-time chats of any complexity
  • Creation of Streaming Applications – It is a good choice when creating streaming services for listening to music or watching videos.
  • Creating Real Time Applications – Applications used for project management, audio and video conferencing and co-browsing. It’s able to handle lots of I/O operations making it suitable for creating real-time applications.
  • Browser Games – Development of games using Node.js is another attractive use case
  • Creation of REST APIs, static file servers, OS wrappers, microcontrollers, drones, e.t.c

Commonly used stacks Of Node.js are;

  • MERN Stack: MongoDB + ExpressJS + React+ NodeJS
  • MEAN Stack: MongoDB + ExpressJS + Angular + NodeJS
  • MEVN Stack: MongoDB + ExpressJS + VueJS+ NodeJS

What’s new in Node.js 18 LTS?

As of the update day of this article, Node.js community has released a new major release – Node.js 18 LTS. This release will enter a long-term support (LTS) in October 2022. For this release of Node.js we shall highlight some of the new and interesting features.

  • It ships with V8 JavaScript engine version 10.x – V8 JavaScript engine is the runtime environment written in C++
  • OPENSSL 3 support – Node.js 18 is the first major version promoted to LTS with OpenSSL3
  • Default DNS resolution – NodeJS now respect the order that is returned based on the dns entries. It no longer prefers IPv4 over IPV6
  • Experimental Fetch – WHATWG Fetch added for high level API in 18 release
  • Experimental test runner – This offers a quick and easy way to run a test suite without any additional dependencies
  • Stream improvements
  • ECMAScript modules improvements
  • Improving support for AbortController and AbortSignal

Install Node.js 18 on CentOS 8|RHEL 8|Rocky Linux 8

Follow the steps in the next sections to install Node.js 18 LTS on CentOS 7 / RHEL 7 Linux system.

Option 1) Install Node.js 18 on CentOS 8|RHEL 8|Rocky Linux 8 using RPM repository

Add Node.js 18 LTS repository on CentOS 8|RHEL 8|Rocky Linux 8

curl --silent --location https://rpm.nodesource.com/setup_18.x | sudo bash -

Repository is configured in the file /etc/yum.repos.d/nodesource-el7.repo. With the repository configured proceed to install Node.js 18 LTS on CentOS 7 / RHEL 7.

sudo yum -y install nodejs

For users who are building addons and extensions for Node.js, you may need to install development tools

sudo yum -y install gcc-c++ make

Install Yarn package manager if you need it

curl -sL https://dl.yarnpkg.com/rpm/yarn.repo | sudo tee /etc/yum.repos.d/yarn.repo
sudo yum -y install yarn

Check version of NodeJS installed

$ node -v
v18.0.0

Option 2) Install Node.js 18 on CentOS 8|RHEL 8|Rocky Linux 8 using NVM

One can also perform an installation of Node.js packages on Linux system using NVM. NVM(Node Version Manager) is a version manager for node.js invoked per-shell. It works on any POSIX-compliant shell (bash, sh, dash, ksh, zsh). By using nvm you can quickly install and use different versions of node via the command line.

As nvm is not installed on RHEL based systems by default, run the commands below to install it.

curl -LsS https://raw.githubusercontent.com/nvm-sh/nvm/master/install.sh | sh -s --

Start using nvm by sourcing your shell rc file:

### Bash ###
$ source ~/.bashrc

### Zsh ###
$ source ~/.zshrc

The script executed clones the nvm repository to ~/.nvm, and attempts to add the source lines from the snippet below to the correct profile file (~/.bash_profile~/.zshrc~/.profile, or ~/.bashrc).

List available versions of Node.js using ls-remote:

$ nvm ls-remote
....
        v16.7.0
        v16.8.0
        v16.9.0
        v16.9.1
.....
        v17.9.0
        v18.0.0

Let’s install Node.js 18 version:

$ nvm install 18

Sample execution output:

Downloading and installing node v18.0.0...
Downloading https://nodejs.org/dist/v18.0.0/node-v18.0.0-linux-x64.tar.xz...
############################################################################################################################################################################################ 100.0%
Computing checksum with sha256sum
Checksums matched!
^[[Now using node v18.0.0 (npm v8.6.0)
Creating default alias: default -> 18 (-> v18.0.0)

You can install other LTS versions using nvm command:

### Install Node.js 16 ###
$ nvm install 16
Downloading and installing node v16.14.2...
Downloading https://nodejs.org/dist/v16.14.2/node-v16.14.2-linux-x64.tar.xz...
############################################################################################################################################################################################ 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v16.14.2 (npm v8.5.0)

### Install Node.js 14 ###
$ nvm install 14
Downloading and installing node v14.19.1...
Downloading https://nodejs.org/dist/v14.19.1/node-v14.19.1-linux-x64.tar.xz...
############################################################################################################################################################################################ 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v14.19.1 (npm v6.14.16)

### Install Node.js 12 ###
$ nvm install 12
Downloading and installing node v12.22.12...
Downloading https://nodejs.org/dist/v12.22.12/node-v12.22.12-linux-x64.tar.xz...
############################################################################################################################################################################################ 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v12.22.12 (npm v6.14.16)

Listing Node.js versions installed:

$ nvm list
->    v12.22.12
       v14.19.1
       v16.14.2
        v18.0.0

Using Specific version of NodeJS

### Syntax ###
$ nvm use <version

# Examples
$ nvm use 18
Now using node v18.0.0 (npm v8.6.0)
$ nvm use 16
Now using node v16.14.2 (npm v8.5.0)

Creating Sample app on Node.js

Once we have Node.js 18 LTS installed, let’s build our first web server. Create a file named app.js

vi myapp.js

Mine contains the following contents:

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, running on NodeJS 18');
});

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

Now, run your Node web server using:

node myapp.js

Visit http://localhost_or_serverip:3000 and you will see a message saying “Hello World”.

Manage Node.js applications using PM2

PM2 is an acronym of Process Management Module which is used to run and manage Node.js applications. PM2 can automatically restart the service when it goes down and bring it back to live. PM2 can be installed on Linux, macOS, and Windows operating systems.

Insta

### Using NPM ###
sudo npm install -g pm2

### Using NVM ###
npm install pm2 -g

Starting the Node.js app with PM2.

pm2 start myapp.js

Once the application is started, you will get a response like below:

[PM2] Starting /home/rocky/myapp.js in fork_mode (1 instance)
[PM2] Done.
┌─────┬──────────┬─────────────┬─────────┬─────────┬──────────┬────────┬──────┬───────────┬──────────┬──────────┬──────────┬──────────┐
│ id  │ name     │ namespace   │ version │ mode    │ pid      │ uptime │ ↺    │ status    │ cpu      │ mem      │ user     │ watching │
├─────┼──────────┼─────────────┼─────────┼─────────┼──────────┼────────┼──────┼───────────┼──────────┼──────────┼──────────┼──────────┤
│ 0   │ myapp    │ default     │ N/A     │ fork    │ 24806    │ 0s     │ 0    │ online    │ 0%       │ 32.6mb   │ rocky    │ disabled │
└─────┴──────────┴─────────────┴─────────┴─────────┴──────────┴────────┴──────┴───────────┴──────────┴──────────┴──────────┴──────────┘

PM2 cheat sheet

List all Node.js processes / applications

$ pm2 ls
┌─────┬──────────┬─────────────┬─────────┬─────────┬──────────┬────────┬──────┬───────────┬──────────┬──────────┬──────────┬──────────┐
│ id  │ name     │ namespace   │ version │ mode    │ pid      │ uptime │ ↺    │ status    │ cpu      │ mem      │ user     │ watching │
├─────┼──────────┼─────────────┼─────────┼─────────┼──────────┼────────┼──────┼───────────┼──────────┼──────────┼──────────┼──────────┤
│ 0   │ myapp    │ default     │ N/A     │ fork    │ 24806    │ 31s    │ 0    │ online    │ 0%       │ 48.1mb   │ rocky    │ disabled │
└─────┴──────────┴─────────────┴─────────┴─────────┴──────────┴────────┴──────┴───────────┴──────────┴──────────┴──────────┴──────────┘

Start the process

pm2 start myapp.js

Restart all the instances of an application

pm2 restart pm2 desc myapp

Enable PM2 to automatically start on system restart

Show more details about the app

$ pm2 show 0
 Describing process with id 0 - name myapp
┌───────────────────┬───────────────────────────────────────┐
│ status            │ online                                │
│ name              │ myapp                                 │
│ namespace         │ default                               │
│ version           │ N/A                                   │
│ restarts          │ 1                                     │
│ uptime            │ 23s                                   │
│ script path       │ /home/rocky/myapp.js                  │
│ script args       │ N/A                                   │
│ error log path    │ /home/rocky/.pm2/logs/myapp-error.log │
│ out log path      │ /home/rocky/.pm2/logs/myapp-out.log   │
│ pid path          │ /home/rocky/.pm2/pids/myapp-0.pid     │
│ interpreter       │ node                                  │
│ interpreter args  │ N/A                                   │
│ script id         │ 0                                     │
│ exec cwd          │ /home/rocky                           │
│ exec mode         │ fork_mode                             │
│ node.js version   │ 18.0.0                                │
│ node env          │ N/A                                   │
│ watch & reload    │ ✘                                     │
│ unstable restarts │ 0                                     │
│ created at        │ 2022-04-22T22:29:12.399Z              │
└───────────────────┴───────────────────────────────────────┘
...

Check the details for a process.

pm2 desc myapp

Scaling the application to n processes

$ pm2 scale myapp 5
[PM2] Scaling up application
[PM2] Scaling up application
[PM2] Scaling up application
[PM2] Scaling up application
┌─────┬──────────┬─────────────┬─────────┬─────────┬──────────┬────────┬──────┬───────────┬──────────┬──────────┬──────────┬──────────┐
│ id  │ name     │ namespace   │ version │ mode    │ pid      │ uptime │ ↺    │ status    │ cpu      │ mem      │ user     │ watching │
├─────┼──────────┼─────────────┼─────────┼─────────┼──────────┼────────┼──────┼───────────┼──────────┼──────────┼──────────┼──────────┤
│ 0   │ myapp    │ default     │ N/A     │ fork    │ 24843    │ 8h     │ 1    │ online    │ 0%       │ 59.1mb   │ rocky    │ disabled │
│ 1   │ myapp    │ default     │ N/A     │ fork    │ 25912    │ 0s     │ 0    │ online    │ 0%       │ 47.0mb   │ rocky    │ disabled │
│ 2   │ myapp    │ default     │ N/A     │ fork    │ 25918    │ 0s     │ 0    │ online    │ 0%       │ 43.9mb   │ rocky    │ disabled │
│ 3   │ myapp    │ default     │ N/A     │ fork    │ 25925    │ 0s     │ 0    │ online    │ 0%       │ 28.3mb   │ rocky    │ disabled │
│ 4   │ myapp    │ default     │ N/A     │ fork    │ 25928    │ 0s     │ 0    │ online    │ 0%       │ 14.8mb   │ rocky    │ disabled │
└─────┴──────────┴─────────────┴─────────┴─────────┴──────────┴────────┴──────┴───────────┴──────────┴──────────┴──────────┴──────────┘

Monitor the logs and metrics of the application

pm2 monit myapp

Use Ctrl + C to quit monitoring screen.

To check the logs for a particular process or an application, use the commands:

pm2 logs 0
pm2 logs myapp

Flush all the logs

pm2 flush myapp

Stopping the application:

pm2 stop myapp

Delete the app

$ pm2 delete myapp
[PM2] Applying action deleteProcessId on app [myapp](ids: [ 0, 1, 2, 3, 4 ])
[PM2] [myapp](0) ✓
[PM2] [myapp](1) ✓
[PM2] [myapp](2) ✓
[PM2] [myapp](3) ✓
[PM2] [myapp](4) ✓

Conclusion

Node.js allows you to build platform-independent web applications with efficiency. In this article, we have covered in detail the process of installing Node.js 18 on CentOS 8|RHEL 8|Rocky Linux 8 Linux system. We also shared the procedure of starting your application in Node server. Installing and using PM2 enabled us to easily manage application lifecycle.

Similar guides:

Cart

Your Cart is Empty

Back To Shop