Different projects often need different Node.js versions — NVM (Node Version Manager) lets you install and switch between multiple versions seamlessly on the same server.
Why You Need Version Management
A Node.js version that works for one project might be incompatible with another's requirements — NVM lets you maintain multiple installed versions and switch between them per-project or per-session, rather than being locked to one system-wide version.
Installing NVM
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
source ~/.bashrc
Installing a Specific Node.js Version
nvm install 20
nvm install 18.19.0
Listing Installed Versions
nvm list
Switching Between Versions
nvm use 20
nvm use 18.19.0
Setting a Default Version
nvm alias default 20
New shell sessions automatically use this version unless overridden.
Using a Project-Specific Version (.nvmrc)
echo "20" > .nvmrc
nvm use
Reading from .nvmrc lets each project specify its exact required version, and anyone (including yourself later) can simply run nvm use in that directory to switch to the correct version automatically.
Automatically Switching Versions When Entering a Directory
nano ~/.bashrc
cd() {
builtin cd "$@"
if [ -f .nvmrc ]; then
nvm use
fi
}
Automatically activates the correct Node.js version whenever you cd into a directory containing an .nvmrc file, avoiding manual version switching.
Using NVM in Production/Non-Interactive Contexts (systemd Services)
NVM is primarily designed for interactive shell use — for systemd services, reference the specific NVM-installed Node binary path directly rather than relying on NVM's shell-based switching mechanism:
ExecStart=/home/user/.nvm/versions/node/v20.11.0/bin/node /opt/myapp/server.js
Removing an Installed Version
nvm uninstall 16
Updating NVM Itself
cd ~/.nvm
git fetch --tags origin
git checkout `git describe --abbrev=0 --tags`
Common Errors
"nvm: command not found" after installation — verify the NVM initialization lines were correctly added to your shell profile (.bashrc/.zshrc) and that you've either restarted your shell or sourced the profile file.
Continue Reading
- How to Install Node.js on Ubuntu & Debian (with NVM)
- How to Deploy a Node.js Application with PM2 and Nginx
- Understanding Package Managers: npm vs yarn vs pnpm
Browse more articles in Programming Languages & Runtimes.