Step-by-Step Guide: Installing Node.js on CentOS with NVM (Node Version Manager)


2 views

While you can install Node.js through yum or by downloading binaries directly, I strongly recommend using NVM (Node Version Manager) for several reasons:

  • Allows multiple Node.js versions to coexist
  • No need for sudo/root privileges
  • Easy version switching
  • Automatic PATH management

Before we begin, make sure your system has:

sudo yum install -y gcc-c++ make
curl -sL https://rpm.nodesource.com/setup_14.x | sudo -E bash -

Run the following command to install NVM:

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

After installation, source your shell configuration:

source ~/.bashrc

Now you can install any Node.js version. For the current LTS version:

nvm install --lts

To install a specific version (e.g., 16.14.2):

nvm install 16.14.2

Check that Node.js and npm are properly installed:

node -v
npm -v

To set a default version that loads automatically:

nvm alias default 16.14.2

If you prefer using yum, first enable the NodeSource repository:

curl -sL https://rpm.nodesource.com/setup_16.x | sudo bash -
sudo yum install -y nodejs

If you encounter permission errors:

sudo chown -R $(whoami) ~/.npm

For command not found errors after NVM installation:

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"

Before we begin, make sure you have:

  • A CentOS 7/8 server
  • SSH access with sudo privileges
  • Basic command line knowledge

This is the recommended method as it provides the latest stable version:


# First, install the EPEL repository
sudo yum install epel-release -y

# Install NodeSource repository
curl -sL https://rpm.nodesource.com/setup_14.x | sudo bash -

# Install Node.js and npm
sudo yum install nodejs -y

# Verify installation
node -v
npm -v

If you need multiple Node.js versions:


# Install NVM
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash

# Source the profile
source ~/.bashrc

# Install specific Node.js version
nvm install 16

# Set default version
nvm alias default 16

For custom builds and latest features:


# Install required tools
sudo yum groupinstall 'Development Tools' -y
sudo yum install openssl-devel -y

# Download and extract Node.js
wget https://nodejs.org/dist/v16.14.2/node-v16.14.2.tar.gz
tar xvf node-v16.14.2.tar.gz
cd node-v16.14.2

# Configure and build
./configure
make -j4
sudo make install

Permission Errors: Use sudo or fix directory permissions

Command Not Found: Add Node.js to PATH or restart your terminal

Create a simple test file:


echo "console.log('Hello from Node.js')" > test.js
node test.js