Using Package Managers to Install Node.js
For CentOS/RHEL Systems
EPEL Repository Method (For Older Versions)
# Install EPEL repository
sudo yum install epel-release -y
# Install Node.js
sudo yum install nodejs -y
# Verify installation
node -v
# Complete uninstallation
sudo yum remove nodejs npm -y
sudo yum clean all
NodeSource Repository Method (Recommended)
# Add NodeSource repository (using Node.js 18.x as example)
curl -fsSL https://rpm.nodesource.com/setup_18.x | sudo bash -
# Install Node.js
sudo yum install -y nodejs
# Verify installation
node -v
npm -v
# Complete uninstallation
sudo yum remove -y nodejs
sudo rm -rf /etc/yum.repos.d/nodesource*.repo
sudo yum clean all
# Remove any remaining files
sudo rm -rf /usr/lib/node_modules
sudo rm -rf /usr/local/lib/node_modules
sudo rm -rf ~/.npm
For Ubuntu/Debian Systems
# Add NodeSource repository
curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -
# Install Node.js
sudo apt-get install -y nodejs
# Verify installation
node -v
npm -v
# Complete uninstallation
sudo apt-get purge -y nodejs
sudo apt-get autoremove -y
sudo rm -rf /etc/apt/sources.list.d/nodesource.list
sudo apt-get update
# Remove remaining files
sudo rm -rf /usr/lib/node_modules
sudo rm -rf /usr/local/lib/node_modules
sudo rm -rf ~/.npm
Using NVM to Install Node.js (Recommended)
NVM (Node Version Manager) allows you to install and manage multiple Node.js versions on the same system.
# Install NVM
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
# Load NVM environment (or reopen terminal)
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \."$NVM_DIR/nvm.sh"
[ -s "$NVM_DIR/bash_completion" ] && \."$NVM_DIR/bash_completion"
# List available Node.js versions
nvm list-remote
# Install a specific Node.js version
nvm install 18.19.0
# Switch Node.js version
nvm use 16.18.1
# Set default version
nvm alias default 18.19.0
# Verify installation
node -v
npm -v
# Uninstall a specific version
nvm uninstall 18.19.0
# Completely uninstall NVM and all Node.js versions
rm -rf ~/.nvm
# Remove NVM-related lines from ~/.bashrc, ~/.zshrc, ~/.profile, etc.
# For example, remove lines similar to:
# export NVM_DIR="$HOME/.nvm"
# [ -s "$NVM_DIR/nvm.sh" ] && \."$NVM_DIR/nvm.sh"
# [ -s "$NVM_DIR/bash_completion" ] && \."$NVM_DIR/bash_completion"
Compiling from Source Code
Installing Dependencies
# For CentOS/RHEL
sudo yum groupinstall -y "Development Tools"
sudo yum install -y python3 gcc-c++ make openssl-devel
# For Ubuntu/Debian
sudo apt-get update
sudo apt-get install -y python3 g++ make libssl-dev
Downloading Source Code
# Download Node.js source code
wget https://nodejs.org/dist/v18.19.0/node-v18.19.0.tar.gz
# Extract source code
tar -xzf node-v18.19.0.tar.gz
# Enter source code directory
cd node-v18.19.0
Compiling and Installation
# Configure build options (specify installation directory)
./configure --prefix=/usr/local/nodejs
# Compile (using multiple cores for acceleration)
make -j$(nproc)
# Install
sudo make install
# Configure environment variables
echo 'export PATH=/usr/local/nodejs/bin:$PATH' | sudo tee /etc/profile.d/nodejs.sh
source /etc/profile.d/nodejs.sh
# Verify installation
node -v
npm -v
# Uninstall source-compiled Node.js
sudo rm -rf /usr/local/nodejs
sudo rm -f /etc/profile.d/nodejs.sh
# Reopen terminal or execute the following command to apply environment variable changes
source /etc/profile
System Requirements and Version Selection
System Requirements
| Node.js Version |
Minimum System Requirements |
| Node.js 18.x |
CentOS/RHEL 8+, Ubuntu 20.04+, GCC 9+ |
| Node.js 16.x |
CentOS/RHEL 7+, Ubuntu 18.04+, GCC 7+ |
| Node.js 14.x |
CentOS/RHEL 7+, Ubuntu 16.04+, GCC 6+ |
Version Selection Recommendations
- Production environments: Prioritize LTS (Long Term Support) versions
- Development environments: Can use the latest Current versions
- Legacy systems: Older systems like CentOS 7 should use Node.js 16.x
Troubleshooting
Permission Issues
# Resolve global package installation permission issues
mkdir -p ~/.npm-global
npm config set prefix ~/.npm-global
echo 'export PATH=~/.npm-global/bin:$PATH' >> ~/.bashrc
source ~/.bashrc
Dependency Issues
- CentOS 7 compilation errors: If encountering C++ compiler version errors, use devtoolset:
sudo yum install -y centos-release-scl
sudo yum install -y devtoolset-9-gcc devtoolset-9-gcc-c++
source /opt/rh/devtoolset-9/enable
# Then try compiling again
- Library version incompatibility: If encountering GLIBC or GLIBCXX version errors, recommend using NVM to install a compatible Node.js version.
Uninstallation Verification
# Check for remaining Node.js files
which node
which npm
# Find possible remaining files
find /usr -name "*node*"
find /usr/local -name "*node*"
find /opt -name "*node*"
# Check global npm packages
ls -la /usr/lib/node_modules
ls -la /usr/local/lib/node_modules
# Check Node.js related paths in environment variables
echo $PATH | tr ':' '\n' | grep -i node
# Check Node.js related settings in configuration files
grep -r "node" ~/.bashrc ~/.profile /etc/profile /etc/profile.d/
Complete Node.js Uninstallation Steps
Regardless of the installation method, these steps can help completely remove Node.js:
# 1. Uninstall using the appropriate package manager
# For yum/dnf:
sudo yum remove -y nodejs npm
# For apt:
sudo apt-get purge -y nodejs npm
# 2. Remove NVM (if used)
rm -rf ~/.nvm
# Remove NVM-related lines from configuration files
# 3. Remove source-installed Node.js
sudo rm -rf /usr/local/nodejs
sudo rm -rf /usr/local/bin/node
sudo rm -rf /usr/local/bin/npm
sudo rm -f /etc/profile.d/nodejs.sh
# 4. Remove global npm packages and cache
sudo rm -rf /usr/lib/node_modules
sudo rm -rf /usr/local/lib/node_modules
rm -rf ~/.npm
# 5. Remove Node.js configuration files
rm -rf ~/.node-gyp
rm -f ~/.npmrc
# 6. Check and remove other possible installation locations
sudo rm -rf /opt/node
# 7. Update system cache
# For yum/dnf:
sudo yum clean all
# For apt:
sudo apt-get update
# 8. Verify complete removal
which node
which npm