How to Install Node.js on RHEL 7

Node.js is a server-side JavaScript runtime built on Chrome’s V8 engine. It is widely used for building REST APIs, real-time applications, microservices, and command-line tooling. Red Hat Enterprise Linux 7 does not ship a recent version of Node.js in its default repositories, so several installation methods are available depending on your requirements: the NodeSource binary repository provides the most straightforward yum-based approach, while nvm (Node Version Manager) gives you the flexibility to run multiple Node.js versions simultaneously. This tutorial covers both methods, verifies the installation, installs PM2 as a process manager, and walks through a simple Node.js HTTP server to confirm everything is working correctly.

Prerequisites

  • RHEL 7 server with root or sudo access
  • curl installed (yum install curl)
  • An active internet connection to download packages
  • EPEL repository enabled for dependency packages
  • Basic knowledge of the Linux command line

Step 1: Enable the EPEL Repository

Some Node.js dependencies and utilities are available from the Extra Packages for Enterprise Linux (EPEL) repository. Enable it before proceeding:

sudo yum install epel-release -y

Step 2: Install Node.js via the NodeSource Repository

NodeSource maintains official RPM repositories for all current Node.js release lines. The setup script adds the appropriate yum repository for the version you choose. The following example installs Node.js 18 (LTS):

# Download and run the NodeSource setup script for Node.js 18
curl -sL https://rpm.nodesource.com/setup_18.x | sudo bash -

The script will:

  1. Detect your OS and version
  2. Add the NodeSource repository file to /etc/yum.repos.d/
  3. Import the NodeSource GPG signing key
  4. Run yum clean all to clear the package cache

After the script completes, install Node.js using yum:

sudo yum install nodejs -y

This single package includes both the node runtime and the npm package manager.

Step 3: Verify the Node.js and npm Installation

Confirm that both binaries are installed correctly and check their versions:

node --version
npm --version
which node
which npm

Expected output for Node.js 18:

v18.20.3
10.5.0
/usr/bin/node
/usr/bin/npm

Step 4: Update npm to the Latest Version

The version of npm bundled with Node.js may not be the latest. Update it globally:

sudo npm install -g npm@latest
npm --version

You should see the updated version number after the command completes.

Step 5: Write a Basic Node.js HTTP Server

Create a simple HTTP server to verify the runtime is working correctly:

mkdir -p ~/nodetest && cd ~/nodetest

cat > server.js <<'EOF'
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 from Node.js on RHEL 7n');
});

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

Run the server:

node server.js

In another terminal or with curl, test the response:

curl http://localhost:3000/

You should see: Hello from Node.js on RHEL 7. Press Ctrl+C to stop the server.

Step 6: Install Node.js via nvm (Alternative Method)

nvm allows you to install multiple Node.js versions side by side and switch between them on demand. This is the preferred approach for development environments or when different projects require different Node.js versions.

Install nvm using the official install script:

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

The installer adds the following lines to your ~/.bashrc automatically:

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"
[ -s "$NVM_DIR/bash_completion" ] && . "$NVM_DIR/bash_completion"

Load nvm into your current shell session:

source ~/.bashrc

Install and activate the latest LTS release:

nvm install --lts
nvm use --lts
node --version

Step 7: Install PM2 Globally

PM2 is a production process manager for Node.js applications. It keeps your app running, restarts it on crashes, provides log aggregation, and integrates with systemd for boot-time startup.

sudo npm install -g pm2

# If using nvm, install without sudo:
npm install -g pm2

pm2 --version

Start your server with PM2:

cd ~/nodetest
pm2 start server.js --name hello-server
pm2 status

PM2 will display a process table showing the application name, process ID, status, CPU usage, and memory consumption. See the dedicated PM2 tutorial for full deployment instructions including systemd integration.

Step 8: Open Firewall Port for Node.js

If you want to access your Node.js application directly on port 3000 from outside the server, open that port in firewalld:

sudo firewall-cmd --permanent --add-port=3000/tcp
sudo firewall-cmd --reload
sudo firewall-cmd --list-ports

For production deployments, it is better to place Nginx or Apache in front of Node.js and only expose ports 80 and 443, keeping the application port internal.

Step 9: Initialize a Node.js Project with npm

For any real application, initialize a proper npm project structure:

mkdir -p ~/myapp && cd ~/myapp
npm init -y

This creates a package.json file with default values. Install Express as a practical example:

npm install express

Create a minimal Express application:

cat > app.js <<'EOF'
const express = require('express');
const app = express();
const PORT = process.env.PORT || 3000;

app.use(express.json());

app.get('/', (req, res) => {
    res.json({ message: 'Node.js + Express on RHEL 7', version: process.version });
});

app.listen(PORT, () => {
    console.log(`Express app listening on port ${PORT}`);
});
EOF

node app.js

Node.js is now installed and fully functional on your RHEL 7 server. You have two installation paths available: the NodeSource yum repository for system-wide installation that integrates with standard package management tooling, and nvm for flexible per-user version management. PM2 is installed and ready to manage your application processes. From here, consider exploring the PM2 deployment guide to set up automatic startup via systemd, log rotation, and cluster mode to take full advantage of your server’s CPU cores.