How to Install NodeJS and NPM on Rocky Linux/Alma Linux 8

Node.js is an open-source, cross-platform, back-end JavaScript runtime environment that runs on the V8 engine and executes JavaScript code outside a web browser. Node. js is primarily used for non-blocking, event-driven servers, due to its single-threaded nature. It’s used for traditional web sites and back-end API services, but was designed with real-time, push-based architectures in mind. Node.js can be used both on the frontend and the backend.

NPM(Node Package Manager) is the default package manager for Node.js and also the largest repository for open-source Node.js packages.

In this tutorial we will learn how to install Node.js and npm on Rocky Linux 8 but it also works on other RHEL 8 based distributions.

Table of Content

  1. Ensuring that the server is up to date
  2. Installing Node.js in Rocky Linux 8
  3. Installing Node.js in Rocky Linux 8
  4. Using NVM to Install Node.js in Rocky Linux 8

1. Ensure that the Server is up to date

Before proceeding, ensure that the server packages are updated. Use this command:

sudo dnf update -y

2. Installing Node.js in Rocky Linux 8

We are going to install latest stable version from available Appstream repo. Node.js is available as a module called nodejs in the default Rocky Linux 8 AppStream repos. To check the available versions, use dnf module command:

$ sudo dnf module list nodejs
Last metadata expiration check: 0:43:00 ago on Wed 12 Jan 2022 09:33:32 AM UTC.
Rocky Linux 8 - AppStream
Name                    Stream                 Profiles                                              Summary
nodejs                  10 [d]                 common [d], development, minimal, s2i                 Javascript runtime
nodejs                  12                     common [d], development, minimal, s2i                 Javascript runtime
nodejs                  14                     common [d], development, minimal, s2i                 Javascript runtime
nodejs                  16                     common [d], development, minimal, s2i                 Javascript runtime

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

From the above output, nodejs version 10 is the default. To install another latest version, we need to enable it using this command:

sudo dnf module enable -y nodejs:16

Now we can install nodejs using this command:

sudo dnf install -y nodejs

Once installed, we can verify nodejs and npm versions using this command:

$ node -v
v16.13.1

For npm:

$ npm -v
8.1.2

3. Using NVM to install Node.js in Rocky Linux 8

NVM is node version manager and it is used to control and manage multiple active versions of Node.js in one system. It is a command line utility and a bash script that allows programmers to shift between different versions of Node.js. It provides a command-line interface where you can install different versions with a single command, set a default, switch between them and much more.

Install nvm using this command:

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

This is the output on my machine

$ curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 15037  100 15037    0     0  19630      0 --:--:-- --:--:-- --:--:-- 19630
=> Downloading nvm as script to '/home/rocky/.nvm'

=> Appending nvm source string to /home/rocky/.bashrc
=> Appending bash_completion source string to /home/rocky/.bashrc
=> 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

Now Reload the ~/.bashrc configuration as below

source ~/.bashrc

check nvm version

$ nvm --version
0.39.1

list all the available versions of Node.js with following command

nvm list-remote

it will display very long list of nodejs versions.you can choose required version from the list.

To install a specific version, use this command:

nvm install v17.3.1

now check the versions as below

$ node --version
v17.3.1

$ npm --version
8.3.0

To review which version of Node.js currently active,we will run below command

$ nvm current
v17.3.1

Switching between versions

NVM allows user to manage multiple versions of Node.js. For testing, let us install the latest stable version of NVM, use the command below:

nvm install --lts

Installing a new version of Node.js automatically switches from the currently active version to the new updated version.

$ nvm current
v16.13.2

To switch back to the previous version:

$ nvm use v17.3.1
Now using node v17.3.1 (npm v8.3.0)

$ nvm current
v17.3.1

Conclusion

That’s it. We have successfully installed nodejs using different methods on a Rocky Linux 8 system.

comments powered by Disqus
Citizix Ltd
Built with Hugo
Theme Stack designed by Jimmy