
How to Install the Latest Node.js and npm on Rocky Linux 8
Building modern, high-performance web applications often requires Node.js, a powerful JavaScript runtime. While Rocky Linux 8 is a stable and secure server environment, its default software repositories may not offer the most recent versions of Node.js. Sticking with outdated versions can mean missing out on crucial performance improvements, security patches, and new features.
This guide provides a clear, step-by-step process for installing the latest versions of Node.js and its package manager, npm, on your Rocky Linux 8 system. We will cover the two most effective and popular methods, ensuring you have the right tools for your development or production environment.
Prerequisites
Before you begin, ensure you have the following:
- A system running Rocky Linux 8.
- A non-root user account with
sudoprivileges. - Access to a terminal or command-line interface.
Method 1: Installing Node.js with the NodeSource Repository (Recommended)
Using the official NodeSource repository is the most straightforward and reliable method for installing a specific, up-to-date version of Node.js. This approach is ideal for production servers where stability and easy updates are a priority.
Step 1: Add the NodeSource Repository
First, you need to add the NodeSource repository to your system’s package manager. The script will detect your OS and automatically configure the correct repository for dnf.
This command downloads and executes the setup script for the latest Long-Term Support (LTS) version of Node.js, which is recommended for most users.
curl -fsSL https://rpm.nodesource.com/setup_lts.x | sudo -E bash -
If you specifically need the absolute latest “Current” release with the newest features (often for testing purposes), you can use this command instead:
curl -fsSL https://rpm.nodesource.com/setup_current.x | sudo -E bash -
Step 2: Install Node.js and npm
Once the repository is added, your system’s dnf package manager is ready to install Node.js. The following command installs the runtime, npm, and all necessary dependencies.
Execute the installation command:
sudo dnf install nodejs -y
The -y flag automatically confirms the installation, skipping the interactive prompt.
Step 3: Verify the Installation
After the installation completes, it’s essential to verify that Node.js and npm are correctly installed and available in your system’s path.
Check the Node.js version:
node -v
Check the npm version:
npm -v
If both commands return a version number (e.g., v18.18.2 and 9.8.1), you have successfully installed Node.js on your Rocky Linux 8 system.
Method 2: Installing Node.js with NVM (Node Version Manager)
For developers who work on multiple projects that may require different Node.js versions, the Node Version Manager (NVM) is an excellent tool. NVM allows you to install and switch between multiple Node.js versions on the fly without affecting the entire system.
Step 1: Install the NVM Script
You can install NVM by downloading and executing its installation script directly from the project’s official repository.
Run the installer script:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.5/install.sh | bash
Note: It’s a good practice to check the NVM GitHub page for the latest version number to ensure you are using the most current script.
This script will download NVM to a .nvm directory in your user’s home folder and add the necessary source lines to your shell profile (~/.bashrc, ~/.zshrc, etc.).
Step 2: Activate NVM
For the changes to take effect in your current terminal session, you need to source your profile file.
source ~/.bashrc
Alternatively, you can simply close and reopen your terminal.
Step 3: Install Your Desired Node.js Version
With NVM installed, you can now easily install any version of Node.js.
To install the latest LTS version (recommended for general use):
nvm install --lts
To install a specific version (e.g., version 20.9.0):
nvm install 20.9.0
You can see a list of all available versions by running nvm ls-remote.
Step 4: Use and Manage Versions
NVM makes it simple to switch between installed versions.
To use a specific version in your current terminal:
nvm use 20.9.0
To set a default version for all new terminal sessions:
nvm alias default 20.9.0
Again, you can verify your active Node.js and npm versions with node -v and npm -v.
Post-Installation Security and Best Practices
- Keep Your System Updated: Regularly run
sudo dnf updateto ensure your system and Node.js (if installed via NodeSource) receive the latest security patches. - Use LTS for Production: For applications running in a production environment, always favor the Long-Term Support (LTS) version. LTS releases prioritize stability and security over new features.
- Manage Project Dependencies: When working on a Node.js project, use the
npm auditcommand to scan your project’s dependencies for known security vulnerabilities and get recommendations for fixes. This is a critical step in maintaining a secure application.
By following these instructions, you are now fully equipped to develop and deploy modern JavaScript applications on your Rocky Linux 8 server with an up-to-date and secure Node.js environment.
Source: https://kifarunix.com/install-latest-nodejs-on-rocky-linux-8/


