How to Manage Node.js Versions with NVM on RHEL 7
Managing Node.js versions is one of the most common challenges for developers and system administrators working with JavaScript applications. Different projects often require different Node.js versions—a legacy application might target Node.js 14 while a new microservice targets Node.js 20. Upgrading the system-wide Node.js installation to meet one project’s needs can break another. NVM (Node Version Manager) solves this problem cleanly by allowing you to install multiple Node.js versions in your home directory and switch between them instantly using simple commands. This guide covers installing NVM on RHEL 7, installing and managing multiple Node.js versions, setting a default version, switching versions per-project with .nvmrc files, and understanding how global npm packages are handled per version.
Prerequisites
- RHEL 7 server with a regular (non-root) user account
- curl or wget installed (
sudo yum install curl) - bash shell (default on RHEL 7)
- Basic familiarity with the Linux command line
- Git installed if you plan to install NVM from source (
sudo yum install git)
Step 1: Install NVM via the curl Install Script
NVM installs entirely in your home directory under ~/.nvm, so no root access is required for the install itself. Run the official install script:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
If curl is unavailable, use wget:
wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
The installer clones the NVM repository into ~/.nvm and appends the following block to your ~/.bashrc:
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"
[ -s "$NVM_DIR/bash_completion" ] && . "$NVM_DIR/bash_completion"
Step 2: Load NVM into the Current Shell Session
After installation, reload your shell configuration to make the nvm command available without opening a new terminal:
source ~/.bashrc
Verify that NVM is available:
nvm --version
Expected output: a version string such as 0.39.7. If the command is not found, check that the NVM lines were correctly added to ~/.bashrc:
tail -10 ~/.bashrc
Step 3: List Available Node.js Versions
Before installing, review the available versions:
# List all available LTS releases
nvm ls-remote --lts
# List all available versions (very long output)
nvm ls-remote
# List only versions from a specific release line
nvm ls-remote 18
Step 4: Install Multiple Node.js Versions
Install the latest LTS version, a specific version, and an older version to demonstrate multi-version management:
# Install the latest LTS release
nvm install --lts
# Install Node.js 20 (current LTS as of 2024)
nvm install 20
# Install Node.js 18 (previous LTS)
nvm install 18
# Install an older specific version
nvm install 16.20.2
NVM installs each version into a separate directory under ~/.nvm/versions/node/. Each version has its own node, npm, and npx binaries, and its own global package storage.
Step 5: Switch Between Node.js Versions
Use nvm use to switch the active version in the current shell session:
# Switch to Node.js 20
nvm use 20
node --version # v20.x.x
# Switch to Node.js 18
nvm use 18
node --version # v18.x.x
# Switch to the latest LTS
nvm use --lts
node --version
Note that nvm use only affects the current shell session. Opening a new terminal will revert to the default version.
Step 6: Set a Default Node.js Version
The default alias controls which version is active when you open a new shell. Set it with nvm alias:
# Set Node.js 20 as the default
nvm alias default 20
# Verify
nvm alias default
# Output: default -> 20 (-> v20.14.0)
You can create other named aliases as well:
nvm alias production 18
nvm alias legacy 16
nvm use production
Step 7: List Installed Versions
View all locally installed Node.js versions and the active one:
nvm ls
Example output:
-> v20.14.0
v18.20.3
v16.20.2
default -> 20 (-> v20.14.0)
lts/* -> lts/iron (-> v20.14.0)
lts/iron -> v20.14.0
lts/hydrogen -> v18.20.3
The arrow (->) indicates the currently active version. Named aliases are listed at the bottom.
Step 8: Switch Versions Per-Project with .nvmrc
The .nvmrc file tells NVM which Node.js version a project requires. Place it in the project root directory:
cd ~/myproject
# Create .nvmrc specifying Node.js 18
echo "18" > .nvmrc
# Or specify a precise version
echo "18.20.3" > .nvmrc
When you enter the project directory, run:
nvm use
NVM reads the .nvmrc file and switches to the specified version automatically:
Found '/home/user/myproject/.nvmrc' with version <18>
Now using node v18.20.3 (npm v10.5.0)
To make version switching truly automatic when you cd into a directory, add the following function to your ~/.bashrc:
cdnvm() {
command cd "$@" || return $?
nvm_path="$(nvm_find_up .nvmrc | command tr -d 'n')"
if [[ ! $nvm_path = *[^[:space:]]* ]]; then
declare default_version
default_version="$(nvm version default)"
if [[ $default_version == "N/A" ]]; then
nvm alias default node
fi
if [[ $(nvm current) != "$default_version" ]]; then
nvm use default
fi
elif [[ -s "$nvm_path/.nvmrc" && -r "$nvm_path/.nvmrc" ]]; then
declare nvm_version
nvm_version=$(<"${nvm_path}"/.nvmrc)
if [[ "$nvm_version" == "default" ]]; then
nvm use default
else
nvm use "$nvm_version"
fi
fi
}
alias cd='cdnvm'
cdnvm "$PWD"
After saving and sourcing ~/.bashrc, your shell will switch Node.js versions automatically whenever you change directories.
Step 9: Managing Global npm Packages Per Version
Each Node.js version installed by NVM has its own isolated global package store. This means packages installed globally for Node.js 20 are not available when Node.js 18 is active, and vice versa.
# With Node.js 20 active, install a global tool
nvm use 20
npm install -g pm2 nodemon typescript
# Switch to Node.js 18 — those tools are NOT available here
nvm use 18
pm2 --version # command not found
# Install them for Node.js 18 as well
npm install -g pm2 nodemon typescript
To migrate global packages from one version to another when upgrading, NVM provides a built-in flag:
# Install Node.js 20 and copy global packages from Node.js 18
nvm install 20 --reinstall-packages-from=18
This installs Node.js 20 and then runs npm install -g for each globally installed package from the Node.js 18 environment, saving time when upgrading versions.
Step 10: Uninstalling a Node.js Version
Remove a Node.js version that is no longer needed:
# Uninstall a specific version
nvm uninstall 16.20.2
# You cannot uninstall the currently active version; switch first
nvm use 20
nvm uninstall 16
Step 11: Updating NVM Itself
NVM updates are released periodically with bug fixes and support for new Node.js features. Update NVM by re-running the install script with the newer version number:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
source ~/.bashrc
nvm --version
NVM is now fully configured on your RHEL 7 system, giving you complete control over Node.js version management. You can install any Node.js version on demand, set project-specific versions through .nvmrc files, automate version switching when changing directories, and manage global packages independently for each version. This setup is particularly valuable in environments where multiple projects with different Node.js requirements coexist on the same server. Combined with PM2 for process management and Nginx for reverse proxying, NVM forms part of a robust Node.js deployment workflow on RHEL 7.