Senior DevOps Technical Writer
Node.js is a JavaScript runtime for server-side programming. It allows developers to create scalable backend functionality using JavaScript, a language many are already familiar with from browser-based web development.
In this guide, you will review three different ways of getting Node.js installed on a Rocky Linux 9 server:
dnf
to install the nodejs
package from Rocky’s default software repositorydnf
with the Nodesource software repository to install specific versions of the nodejs
packagenvm
, the Node Version Manager, and using it to install and manage multiple versions of Node.jsFor many users, using dnf
with the default package sources will be sufficient. If you need specific newer (or legacy) versions of Node, you should use the Nodesource repository. If you are actively developing Node applications and need to switch between node
versions frequently, choose the nvm
method.
This guide assumes that you are using Rocky Linux 9. Before you begin, you should have a non-root user account with sudo
privileges set up on your system. You can learn how to do this by following the Rocky Linux 9 initial server setup tutorial.
Rocky Linux 9 contains 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 in the repositories is 16.14.0. This will not be the latest version, but it should be stable and sufficient for quick experimentation with the language.
To get this version, you can use the dnf
package manager:
- sudo dnf install nodejs -y
Check that the install was successful by querying node
for its version number:
- node -v
Outputv16.14.0
If the package in the repositories suits your needs, this is all you need to do to get set up with Node.js. The Node.js package from Rocky’s default repositories also comes with npm
, the Node.js package manager. This will allow you to install modules and packages to 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 alternate repository to install different versions of Node.js.
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 available than the official Rocky repositories. Node.js v14, v16, and v18 are available as of the time of writing.
First, you’ll need to configure the repository locally, in order to get access to its packages. From your home directory, use curl
to retrieve the installation script for your preferred 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
Refer to the NodeSource documentation for more information on the available versions.
You can inspect the contents of the downloaded script with vi
(or your preferred text editor):
- vi nodesource_setup.sh
Running third party shell scripts is not always considered a best practice, but in this case, NodeSource implements their own logic in order to ensure the correct commands are being passed to your package manager based on distro and version requirements. If you are satisfied that the script is safe to run, 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 updated automatically. You can now install the Node.js package in the same way you did in the previous section. It may be a good idea to fully remove your older Node.js packages before installing the new version, by using sudo dnf remove nodejs npm
. This will not affect your configurations at all, only the installed versions. Third party repositories don’t always package their software in a way that works as a direct upgrade over stock packages, and if you have trouble, you can always try to revert to a clean slate.
- sudo dnf remove nodejs npm -y
-
- ```command
- sudo dnf install nodejs -y
Verify that you’ve installed the new version by running node
with the -v
version flag:
- node -v
Outputv18.9.0
The NodeSource nodejs
package contains 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 will show how to use the Node Version Manager to install and manage multiple versions of Node.js.
Another way of installing Node.js that is particularly 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, at the same time.
To install NVM on your Rocky Linux 9 machine, visit the project’s GitHub page. Copy the curl
command from the README file that displays on the main page. This will get you the most recent version of the installation script.
Before piping the command through to bash
, it is always a good idea to audit the script to make sure it isn’t doing anything you don’t agree with. You can do that by removing the | bash
segment 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 is making. When you are satisfied, run the command again with | bash
appended at the end. The URL you use will change depending on the latest version of nvm, but as of right now, the script can be downloaded and executed by typing:
- curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
This will install the nvm
script to your user account. To use it, you must first source your .bashrc
file:
- source ~/.bashrc
Now, you can ask NVM which versions of Node are available:
- nvm list-remote
Output. . .
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 (LTS: Gallium)
v16.17.0 (Latest LTS: Gallium)
v17.0.0
v17.0.1
v17.1.0
v17.2.0
…
It’s a very long list! You can install a version of Node by typing any of the release versions you see. For instance, to get version v16.16.0 (an LTS release), you can 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 (-> N/A)
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.17.0 (-> N/A)
This shows the currently active version on the first line (-> v16.16.0
), followed by some named aliases and the versions that those aliases point to.
Note: if you also have a version of Node.js installed through dnf
, you may see a system
entry here. You can always activate the system-installed version of Node using nvm use system
.
You can install a release based on these aliases as well. For instance, to install fermium
, run the following:
- nvm install lts/gallium
OutputDownloading and installing node v16.17.0...
Downloading https://nodejs.org/dist/v16.17.0/node-v16.17.0-linux-x64.tar.xz...
################################################################################# 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v16.17.0 (npm v8.15.0)
You can verify that the install was successful using the same technique from the other sections, by typing:
- node -v
Outputv16.17.0
The correct version of Node is installed on our machine as we expected. A compatible version of npm
is also available.
There are quite a few ways to get up and running with Node.js on your Rocky Linux server. Your circumstances will dictate which of the above methods is best for your needs. While using the packaged version in Rocky’s repositories is the easiest method, using nvm
or the NodeSource repository offers additional flexibility.
For more information on programming with Node.js, please refer to our tutorial series How To Code in Node.js.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!