How to install Node.js on Rocky Linux 8

0 Shares
0
0
0
0

Introduction

Node.js is a JavaScript runtime for server-side programming. It allows developers to create scalable backend functionality using JavaScript, a language that many are familiar with from browser-based web development.

In this guide, we will show you three different ways to install Node.js on a Rocky Linux 8 server:

  • Using dnf to install the nodejs package from the default Rocky software repository
  • Using dnf with the Nodesource software repository to install specific versions of the nodejs package
  • Install nvm, the Node Version Manager, and use it to install and manage multiple versions of Node.js

For many users, using dnf with the default package sources will be sufficient. If you need newer (or older) versions of Node, you should use the Nodesource repository. If you are actively developing Node applications and need to switch between Node versions, choose the nvm method.

Prerequisites

This guide assumes you are using Rocky Linux 8. Before you begin, you will need to set up a non-root user account with sudo privileges on your system.

Option 1 – Install Node.js with DNF from the default repositories

Rocky Linux 8 includes a version of Node.js in its default repositories that can be used to provide a consistent experience across multiple systems. At the time of writing, the version available in the repositories is 10.24.0. This won't be the latest version, but it should be stable and sufficient for quick testing of the language.

You can use the dnf package manager to get this version:

sudo dnf install nodejs -y

Verify that the installation was successful by querying for its version number:

node -v
Output
v10.24.0

If the package in the repositories meets your needs, that's all you need to do to get Node.js up and running. The Node.js package from Rocky's default repositories also comes with npm, the Node.js package manager. This allows you to install modules and packages for use with Node.js.

At this point, you have successfully installed Node.js and npm using dnf and the default Rocky software repositories. The next section will show you how to use an alternative repository to install different versions of Node.js.

Option 2 – Install Node.js with DNF using the NodeSource repository

To install a different version of Node.js, you can use the NodeSource repository. NodeSource is a third-party repository that has more versions of Node.js than the official Rocky repositories. Node.js versions 14, 16, and 17 are available as of this writing.

First you need to configure the repository locally to access its packages. From your home directory, use curl to retrieve the installation script for your desired version, making sure to replace 18.x with your preferred version string (if different).

cd ~
curl -sL https://rpm.nodesource.com/setup_18.x -o nodesource_setup.sh

See the NodeSource documentation for more information about available versions.

You can check the contents of the downloaded script with vi (or your favorite text editor):

vi nodesource_setup.sh

Running third-party shell scripts is not always considered best practice, but in this case, NodeSource implements its own logic to ensure that the correct commands are passed to your package manager based on the distribution and version required. If you are satisfied that it is safe to run the script, exit your editor, then run the script with sudo:

sudo bash nodesource_setup.sh
Output
…
## Your system appears to already have Node.js installed from an alternative source.
Run `sudo yum remove -y nodejs npm` to remove these first.
## Run `sudo yum install -y nodejs` to install Node.js 18.x and npm.
## You may run dnf if yum is not available:
sudo dnf install -y nodejs
## You may also need development tools to build native addons:
sudo yum install gcc-c++ make
## To install the Yarn package manager, run:
curl -sL https://dl.yarnpkg.com/rpm/yarn.repo | sudo tee /etc/yum.repos.d/yarn.repo
sudo yum install yarn

The repository will be added to your configuration and your local package cache will be automatically updated. You can now install the Node.js package in the same way you did in the previous section. It might be a good idea to completely remove your old Node.js packages before installing the new version using sudo dnf remove nodejs npm . This will not affect your settings in any way, just the installed versions. Third-party repositories don't always package their software in a way that acts as a direct upgrade over the stock packages, and if you have problems, you can always try to revert to a clean list.

sudo dnf remove nodejs npm -y
```command
sudo dnf install nodejs -y

Verify that you have the new version installed by running Node with the -v version flag:

node -v
Output
v18.6.0

The NodeSource nodejs package includes both the node binary and npm, so you don't need to install npm separately.

At this point you have successfully installed Node.js and npm using dnf and the NodeSource repository. The next section shows how to use Node Version Manager to install and manage multiple versions of Node.js.

Option 3 – Install Node using Node Version Manager

Another way to install Node.js that is flexible is to use nvm, the Node Version Manager. This piece of software allows you to install and maintain many different independent versions of Node.js and their associated Node packages simultaneously.

To install NVM on your Rocky Linux 8 machine, visit the project's GitHub page. Copy the curl command from the README file displayed on the home page. This will give you the latest version of the installation script.

Before passing the command to bash, it's always a good idea to check the script to make sure it doesn't do anything you don't agree with. You can do this by removing the | bash section at the end of the curl command:

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh

Take a look and make sure you are comfortable with the changes it makes. When you are satisfied, run the command again with | bash added at the end. The URL you use will change depending on the latest version of nvm, but as of now, the script can be downloaded and run by typing:

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

This will install the nvm script into your user account. To use it, you must first source your bashrc file:

source ~/.bashrc

Now you can ask NVM which Node version is available:

nvm list-remote
Output
. . .
v16.11.1
v16.12.0
v16.13.0 (LTS: Gallium)
v16.13.1 (LTS: Gallium)
v16.13.2 (LTS: Gallium)
v16.14.0 (LTS: Gallium)
v16.14.1 (LTS: Gallium)
v16.14.2 (LTS: Gallium)
v16.15.0 (LTS: Gallium)
v16.15.1 (LTS: Gallium)
v16.16.0 (Latest LTS: Gallium)
v17.0.0
v17.0.1
v17.1.0
v17.2.0
…

That's a very long list! You can install a version of Node by typing in each of the releases you see. For example, to get v16.16.0 (an LTS release), you could type:

nvm install v16.16.0

You can see the different versions you have installed by typing:

nvm list
Output
-> v16.16.0
system
default -> v16.16.0
iojs -> N/A (default)
unstable -> N/A (default)
node -> stable (-> v16.16.0) (default)
stable -> 16.16 (-> v16.16.0) (default)
lts/* -> lts/gallium (-> v16.16.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.20.0 (-> N/A)
lts/gallium -> v16.16.0

This shows the current active version on the first line (->v16.16.0) followed by some aliases and the versions that those aliases refer to.

You can also install a version based on these aliases. For example, to install Fermium, run the following:

nvm install lts/fermium
Output
Downloading and installing node v14.19.0...
Downloading https://nodejs.org/dist/v14.19.0/node-v14.19.0-linux-x64.tar.xz...
################################################################################# 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v14.19.0 (npm v6.14.16)

You can verify that the installation is successful using the same technique as in other sections, by typing:

node -v
Output
v14.19.0

The correct version of Node is installed on our machine as expected. A compatible version of npm is also available.

Result

There are many ways to get Node.js up and running on a Rocky Linux server. Your circumstances will determine which of the above methods is best for your needs. While using the version packaged in the Rocky repositories is the easiest method, using nvm or the NodeSource repository offers more flexibility.

Leave a Reply

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

You May Also Like