Installing Node.js on a Linux machine involves several steps, depending on the Linux distribution you are using (e.g., Ubuntu, CentOS, Fedora, etc.).
Why Node.js?
Node.js is a JavaScript runtime that is built on Chrome’s V8 engine, allowing developers to run JavaScript on the server-side. It’s commonly used for building scalable and efficient applications like web servers, real-time applications, and APIs.
Prerequisites
Before you begin, make sure you have the following:
- A Linux machine or virtual machine.
- Root or sudo privileges on the system.
- An active internet connection to download the necessary files.
Step-by-Step Guide to Installing Node.js
1. Update Package Index (Recommended)
First, it’s always a good practice to update your package manager’s cache to ensure you install the latest versions of the required packages.
For Debian-based distributions (like Ubuntu), run:
sudo apt update
For Red Hat-based distributions (like CentOS or Fedora), run:
sudo yum update
2. Install Node.js Using Package Manager
A. Installing Node.js on Ubuntu or Debian-based Systems
There are multiple ways to install Node.js on Ubuntu or Debian. The two most common approaches are using the NodeSource repository or installing from the official Ubuntu/Debian repositories.
Option 1: Install Node.js from NodeSource Repository (Recommended)
NodeSource is a third-party repository that provides updated versions of Node.js.
- Add the NodeSource repository:
First, download the setup script for your version of Node.js. You can install either the LTS (Long-Term Support) or Current version. The LTS version is recommended for production environments.
For the LTS version (recommended):
curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo -E bash -
For the Current version:
curl -fsSL https://deb.nodesource.com/setup_current.x | sudo -E bash -
This script adds the NodeSource repository to your system and updates the package manager cache.
- Install Node.js: After adding the repository, you can install Node.js by running:
sudo apt install -y nodejs
- Verify the installation: After installation, verify that Node.js and npm (Node Package Manager) were installed correctly:
node -v # To check the Node.js version npm -v # To check the npm version
Option 2: Install Node.js from Ubuntu/Debian Repositories (Older Version)
The version available in the default package manager may be outdated. However, you can install Node.js using the following steps:
- Install Node.js:
sudo apt install nodejs sudo apt install npm
- Verify installation:
node -v npm -v
B. Installing Node.js on CentOS or RHEL-based Systems
For CentOS, Red Hat, or Fedora, the installation steps for Node.js are similar to Ubuntu, but using yum
or dnf
package managers.
Option 1: Install Node.js from NodeSource Repository
- Install NodeSource repository: First, download the setup script for the Node.js version you want. You can use the LTS or Current version based on your preference.
For the LTS version:
curl -fsSL https://rpm.nodesource.com/setup_lts.x | sudo bash -
For the Current version:
curl -fsSL https://rpm.nodesource.com/setup_current.x | sudo bash -
- Install Node.js: After setting up the repository, install Node.js by running:
sudo yum install -y nodejs # For CentOS/RHEL 7 sudo dnf install -y nodejs # For CentOS/RHEL 8 and Fedora
- Verify installation: Check if Node.js and npm are correctly installed:
node -v npm -v
Option 2: Install Node.js Using EPEL Repository (Not Recommended for Latest Versions)
If you don’t want to use NodeSource, you can install Node.js from the EPEL repository, but the version may be outdated. To install:
- Enable EPEL repository (if not already enabled):
sudo yum install epel-release
- Install Node.js:
sudo yum install nodejs
- Verify installation:
node -v npm -v
C. Installing Node.js on Fedora
On Fedora, you can install Node.js directly using the dnf
package manager.
- Install Node.js:
sudo dnf install nodejs
- Verify installation:
node -v npm -v
3. Install Using NVM (Node Version Manager)
An alternative and recommended method for installing Node.js, especially if you want to manage multiple versions of Node.js, is by using NVM (Node Version Manager).
NVM allows you to install, manage, and switch between multiple versions of Node.js. This is helpful if you’re working on multiple projects that require different versions of Node.js.
- Install NVM: Download and install the
nvm
script usingcurl
:curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash
After running the script, close and reopen your terminal, or you can source the
nvm
script manually:source ~/.bashrc
- Install Node.js using NVM: Once
nvm
is installed, you can install Node.js:For the LTS version:
nvm install --lts
For the Current version:
nvm install node
- Verify installation: Check if Node.js is installed:
node -v npm -v
You can also switch between Node.js versions using:
nvm use <version_number>
4. Troubleshooting Installation
- npm is not installed: If npm is not installed with Node.js, you can install it separately using:
sudo apt install npm # On Ubuntu/Debian sudo yum install npm # On CentOS/RedHat sudo dnf install npm # On Fedora
- Permission issues: Sometimes, installation may fail due to permission issues. One way to solve this is to change the directory where npm stores its global packages:
mkdir ~/.npm-global npm config set prefix '~/.npm-global'
- Uninstalling Node.js: If you need to uninstall Node.js, you can do so with the following commands:
sudo apt remove nodejs # On Ubuntu/Debian sudo yum remove nodejs # On CentOS/RHEL sudo dnf remove nodejs # On Fedora
Conclusion
The process of installing Node.js on Linux can vary depending on the distribution you’re using. By following the appropriate steps outlined above, you should be able to install Node.js easily on your system. Whether you’re using the package manager, NVM, or NodeSource, each method allows you to take full advantage of Node.js’s powerful runtime for building server-side applications.