Install Nodejs and NPM on Rocky 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 lets developers use JavaScript to write command line tools and for server-side scripting --running scripts server-side to produce dynamic web page content before the page is sent to the user's web browser.
NPM means Node Package Manager is the default package manager for Node.js and also the largest repository for open-source Node.js packages
This tutorial guide you on how to install nodejs and NPM on Rocky Linux 8 system.
Method 1: using Appstream repo
This method is to install latest stable version from available Appstream repo.
Step 1: Install Nodejs from Appstream repo
Nodejs is available on the default Rocky Linux 8 AppStream repos. we will check avaialbale versions.
dnf module list nodejs
output:
Last metadata expiration check: 1 day, 3:23:07 ago on Wed 06 Oct 2021 12:35:28 PM IST.
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
its showing nodejs 10 version is showing as the default repository module.But we can install latest stable version of 14 from above output, which is avaialable on the repository.
so to enable the latest NodeJS stream, run the following command.
sudo dnf module enable nodejs:14
Also Read -> How to Install Apache Maven on Rocky Linux
Step 2:install Nodejs
Now install nodejs as below.
sudo dnf install nodejs
verify nodejs and npm versions
$ node -v
v14.17.5
$ npm -v
6.14.14
Method2: using Nodesource repository
this method is used to install latest release version of nodejs .
Step 1: Install Nodejs from Nodesource Repositories
To install current stable release from the official Nodejs site, you need to install Nodejs dnf repository and run the following command to install it.
curl -sL https://rpm.nodesource.com/setup_16.x | sudo -E bash -
Also Read -> How to Install PostgreSQL on Rocky Linux
Step 2: install nodejs
now install nodejs
sudo dnf install nodejs
verify the versions
$ node -v
v16.10.0
$ npm -v
7.24.0
Method 3: using NVM
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.
Step 1: Installing nvm
wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash
Now Reload the ~/.bashrc configuration as below
source ~/.bash_profile
then check nvm version
$ nvm --version
0.39.0
Now list all the available versions of Node.js with following commnad.
nvm list-remote
it will display very long list of nodejs versions.you can choose required version from the list.
sample output:
v0.1.14
v0.1.15
v0.1.16
v0.1.17
v0.1.18
-------
-------
-------
v16.4.0
v16.4.1
v16.4.2
v16.5.0
v16.6.0
v16.6.1
v16.6.2
v16.7.0
v16.8.0
v16.9.0
v16.9.1
v16.10.0
Alos Read -> How to Install Elasticsearch on Rocky Linux 8
lets choose latest version from above list .you can select required version.
Step 2: Install nodejs
nvm install v16.10.0
now check the versions as below
$ node --version
v16.10.0
$ npm --version
7.24.0
To review which version of Node.js currently active,we will run below command
$ nvm current
v16.10.0
Also Read -> How to Install PgAdmin 4 on Rocky Linux 8
switch between versions
NVM allows user to manage multiple versions of Node.js. For testing,we install the latest stable version of NVM, we will run below command.
nvm install --lts
Installing latest LTS version.
Downloading and installing node v14.18.0...
Downloading https://nodejs.org/dist/v14.18.0/node-v14.18.0-linux-x64.tar.xz...
################################################################################################################################################################# 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v14.18.0 (npm v6.14.15)
Installing a new version of Node.js automatically switches from the currently active version to the new updated version.
[root@RockyLinux ~]# nvm current
v14.18.0
now if you want to switch back to previous version v16, run following command
[root@RockyLinux ~]# nvm use v16.10.0
Now using node v16.10.0 (npm v7.24.0)
[root@RockyLinux ~]# nvm current
v16.10.0
That's it. Now you have successfully installed nodejs using diffrent methods on Rocky Linux 8 system.
Also Read -> How to Migrate from Centos 8 to Alma Linux 8