Installing Node.js on Rocky Linux: A Step-by-Step Guide

Node.js is a versatile, cross-platform JavaScript runtime optimized for server-side programming. Built on Chrome’s high-performance V8 JavaScript engine, Node.js facilitates JavaScript execution in a server environment, easing the development of scalable backend applications. Its popularity among both full-stack and front-end developers underscores its integral role in modern software development.

As it continues to rise in prominence, Node.js stands as a staple in constructing server-side and networking applications, establishing itself as an industry standard. Whether for backend operations, desktop software, web fronts, or mobile apps, Node.js serves multiple platforms with ease.

In this guide, you’ll explore two methods to install Node.js on Rocky Linux:

  1. Installation via the AppStream repository: Ideal for those requiring a stable Node.js version for ongoing production environments.
  2. Installation using NVM (Node Version Manager): Suited for developers needing to switch between Node.js versions frequently, NVM handles multiple Node.js installations seamlessly.

Prerequisites

  • A Rocky Linux system with all packages and repositories updated to their latest versions.
  • You should have root access or a user with sudo privileges to install packages and modify system settings.

Method 1 – Installing Node.js from the AppStream Repository

Rocky Linux includes a repository module for Node.js that offers three LTS (Long-Term Support) versions. This method suits applications needing a stable Node.js version for production. If your applications demand a specific version of Node.js, consider using the second method.

Setting up the Repository

1. Start by listing all available Node.js repository modules on your Rocky Linux system:

sudo dnf module list | grep nodejs

Check Node.js repository module Rocky Linux

The default module for Node.js version 10 is selected with the ‘common’ profile.

2. To enable the latest stable LTS version (currently version 14), use:

sudo dnf module enable nodejs:14

Enable Repository Module Node.js 14

Optional different profiles can be activated like so:

sudo dnf module enable nodejs:14/minimal

3. Verify the Node.js module with:

sudo dnf module list nodejs

Verify Node.js repository module

Installing Node.js and npm on Rocky Linux

1. Install Node.js and npm:

sudo dnf install nodejs npm

Install Node.js and npm on Rocky Linux

As shown, Node.js 14.x from the AppStream repository is selected.

2. Verify the installation:

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

Check Node.js and nom binary path and version

Method 2 – Installing Node.js with NVM (Node Version Manager)

NVM is an indispensable tool for Node.js version management, enabling multiple installations on one machine. This flexibility allows developers to easily switch Node.js versions tailored to different projects.

Installing NVM (Node Version Manager)

1. Install NVM with the following:

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

Installing NVM Node Version Manager

2. Refresh your shell configuration:

source ~/.bashrc

3. Confirm the NVM installation:

command -v nvm

Verify NVM details:

nvm --version
nvm --help

Verify NVM Installation

NVM Basic Usage: Check Available Node.js Versions

1. List all Node.js versions on the remote repository:

nvm ls-remote

2. For LTS versions only:

nvm ls-remote --lts

NVM Basic Usage: Installing Node.js with NVM

1. Install a specific Node.js version:

nvm install v16.8.0

Install Specific Version of Node.js with NVM

Verify installation:

node --version
npm --version

2. For the LTS version:

nvm install --lts

Install Node.js LTS version with nvm

3. Install using codename:

nvm install lts/dubnium

nvm install Node.js based on codename release

NVM Basic Usage: Managing Multiple Node.js Versions

1. List installed Node.js versions:

nvm ls

Check Node.js Installed on the System with nvm

2. Change the default Node.js version:

nvm use v16.8.0

Confirm the current version:

nvm current

Switch different version of Node.js with NVM

Switch to an LTS version:

nvm use --lts

Verify the change:

nvm current

Switch different version of Node.js with NVM

Conclusion

Congratulations! You have successfully installed Node.js on Rocky Linux. The AppStream repository method is applicable to other RHEL-based distributions like CentOS, while the NVM method is widely compatible across most Linux distributions due to its POSIX compliance. Choose the method that suits your system and development needs best.

FAQ

Do I need to remove Node.js if I switch between the methods?

No. You can have Node.js installed through both the AppStream repository and NVM simultaneously. NVM will manage versions independently of AppStream.

Can I use NVM on other Linux distributions?

Yes. NVM is POSIX-compliant and can be used on most Unix-like operating systems, not just Rocky Linux.

Which installation method should I choose?

If you require a stable Node.js environment for production, use the AppStream method. If you regularly need to switch Node.js versions for development tasks, opt for NVM.