NVM (Node Version Manager) allows installing and switching between multiple Node.js versions on the same machine without system-level package manager involvement. This is essential for developers or servers that need to run different Node.js versions for different projects — for example, running a legacy application on Node.js 16 while developing a new application on Node.js 22. NVM installs each Node.js version in ~/.nvm/versions/node/ and provides nvm use to switch the active version in the current shell. Project-specific versions can be pinned with a .nvmrc file in the project root. This guide covers installing NVM on RHEL 9, managing multiple Node.js versions, and using .nvmrc for per-project version pinning.

Prerequisites

  • RHEL 9 with sudo/root access
  • curl or wget available

Step 1 — Install NVM

# Install NVM (check https://github.com/nvm-sh/nvm for the latest version)
NVM_VERSION="0.39.7"
curl -o- "https://raw.githubusercontent.com/nvm-sh/nvm/v${NVM_VERSION}/install.sh" | bash

# Load NVM into current shell
export NVM_DIR="$HOME/.nvm"
source "$NVM_DIR/nvm.sh"

# Verify
nvm --version

Step 2 — Add NVM to Shell Profile

# Add to ~/.bashrc (install script usually does this automatically)
cat >> ~/.bashrc << 'EOF'
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"
[ -s "$NVM_DIR/bash_completion" ] && . "$NVM_DIR/bash_completion"
EOF

source ~/.bashrc

Step 3 — Install and Manage Node.js Versions

# List available LTS versions
nvm ls-remote --lts | tail -20

# Install specific versions
nvm install 20      # Latest Node.js 20.x LTS
nvm install 22      # Latest Node.js 22.x LTS
nvm install 18.20.4 # Exact version

# List installed versions
nvm ls

# Switch active version
nvm use 22
node --version

# Set default version for new shells
nvm alias default 20

Step 4 — Use .nvmrc for Per-Project Version Pinning

cd /var/www/myproject

# Create .nvmrc with the required Node.js version
echo "20" > .nvmrc

# Now anyone cloning the project can run:
nvm use  # Automatically reads .nvmrc and switches to Node.js 20

# Or install the version if not present:
nvm install  # Reads .nvmrc and installs if needed

Step 5 — Install Global Packages Per Version

# Global packages are per-version — reinstall after switching
nvm use 22
npm install -g pm2 yarn

# Copy global packages from a previous version
nvm install 22 --reinstall-packages-from=20

Step 6 — Remove Old Versions

# Uninstall a Node.js version
nvm uninstall 18

# Check disk usage
du -sh ~/.nvm/versions/node/*

Conclusion

NVM on RHEL 9 provides flexible multi-version Node.js management for development environments. For production servers, a system-wide Node.js installation from NodeSource is generally preferable since NVM is per-user and not directly compatible with systemd services (which run as different users). When using NVM for production deployments, specify the full binary path in systemd unit files: ExecStart=/home/deploy/.nvm/versions/node/v20.x.x/bin/node server.js rather than relying on NVM’s shell-level nvm use.

Next steps: How to Install Node.js with NodeSource on RHEL 9, How to Deploy a Node.js Application with PM2 on RHEL 9, and How to Install and Use npm on RHEL 9.