Installing Node.js on Linux

Node.js is a JavaScript runtime environment based on the Chrome V8 engine, enabling developers to perform high-performance network programming using the JavaScript language on the server side. For Linux systems, it is one of the preferred tools for building modern web backend services. This article provides a detailed guide on installing Node.js in a Linux environment, using Ubuntu/Debian and CentOS/RHEL as examples.

Before installation, ensure that your system's package list is updated and that necessary dependencies (such as curl, wget, git, or build tools) are installed. The following commands are typically used to initialize the environment:

  • Ubuntu/Debian: sudo apt update && sudo apt install curl -y
  • CentOS/RHEL: yum groupinstall "Development Tools" && yum install git wget -y
    NVM (Node Version Manager) is currently the most flexible and stable Node.js version management tool recommended by the industry. It allows users to switch between multiple different versions of Node.js within the same system without requiring complex system-level configuration modifications.

1. Clone the Official Repository

First, obtain the NVM source code and install it into your user directory:

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

This command automatically downloads the latest stable NVM script and adds it to your shell configuration file (such as .zshrc or .bashrc).

2. Load Environment Variables

After installation, you must reopen the terminal and execute the following operations to activate the newly installed module:

  • Ubuntu/Debian: source ~/.bashrc (if using ZSH, change to source ~/.zshrc)
  • CentOS/RHEL: source ~/.bash_profile

3. Verify and Install Node.js

After loading the environment variables, you can enter the following commands to check the current version and install a specific version of Node.js:

nvm --version              # Check if NVM is working correctly
nvm install 18            # Install LTS version (usually v18.x)
node -v                   # Verify the installed Node version
npm list                 # Check npm package manager status

System-Level Installation Using Official Scripts

If you do not require multi-version management functionality or prefer to deploy directly via the operating system's built-in package manager, you may also consider using the official Node.js installer packages. This method is simple to configure but offers slightly less flexibility compared to NVM.

Ubuntu/Debian User Guide

For Debian-based distributions, pre-compiled binary files can be obtained via apt:

  1. Add Software Repository (Optional: Skip if already exists): Run sudo apt install software-properties-common and then execute the corresponding official script command to update APT configuration.
  2. Install Node.js: Simply input sudo apt update && sudo apt install nodejs npm -y. This operation will simultaneously install the latest stable version of Node.js and npm, automatically handling dependencies.

CentOS/RHEL/Fedora User Guide

For RedHat-based distributions, it is recommended to use the official .rpm package provided or the EPEL repository:

  1. Add EPEL Repository (Requires installing epel-release first): Run sudo yum install epel-release -y.
  2. Download and Install Node.js RPM: Visit NodeSource to obtain the .rpm file corresponding to your architecture, or use the command below to pull it directly:
    sudo curl https://rpm.nodesource.com/setup-lts.x | sudo bash -
    sudo yum install nodejs -y
    

Post-Installation Configuration and Verification Steps

Regardless of the installation method used, final system-level configuration is required to ensure environment variables are correctly applied. This step is crucial for avoiding path errors in subsequent development environments.

1. Update Shell Configuration Files

Execute the following commands in the terminal to refresh environment variables for the current session:

  • Ubuntu/Debian: export PATH=/usr/bin:/bin:$PATH (if not manually added)
  • CentOS/RHEL: source ~/.bash_profile

2. Verify Installation Status

Confirm that Node.js, npm, and NVM (if installed) are working correctly by running a series of standard check commands:

node -v              # Should output the version number, e.g., v18.x.y
npm --version        # Should display npm version information
nvm list             # (Only if using NVM) List all available Node versions

# Test a simple project environment
mkdir test-project && cd test-project
node -e "console.log('Hello from Node.js')" 

If the above commands execute normally without errors, the installation is successful. You can now begin creating new backend projects or running existing codebases.