Most engineers reach for nvm because their laptop juggles three Node major versions. On a server, the same tool gives an operator the ability to pin Node per deployment without polluting apt. This article installs nvm cleanly on Ubuntu 22.04 / 24.04 and Debian 12, then locks a version per project so the production runtime never drifts on a git pull.
How to verify
Check whether anything is already installed under the system or for the current user:
command -v node && node -v
command -v nvm || echo "nvm not in PATH for this shell"
ls -la "$HOME/.nvm" 2>/dev/null | head
dpkg -l | grep -E "^ii\s+nodejs|^ii\s+npm" || echo "no apt nodejs"
If apt already installed nodejs and npm, remove them before introducing nvm — mixed installs lead to npm writing to /usr/lib/node_modules while node lives in ~/.nvm/versions/node/..., and the two disagree about which native modules are loadable.
What’s happening
nvm is a shell function that prepends a chosen Node version’s bin directory to PATH when the shell starts. It is not a daemon, not a system service, and not “installed” in the system package sense — it lives entirely under $HOME/.nvm and only the user that ran the installer sees it.
That matters in production. A deployment user (typically deploy or the application’s service account) gets its own nvm install. systemd units that need Node must reference the absolute path under ~/.nvm/versions/node/vXX.Y.Z/bin/node, because systemd does not source .bashrc and nvm use is meaningless to it. The .nvmrc file at the root of a repository records the version that engineering tested against.
The procedure
-
Create or pick the service user that will own the runtime. For a single-app box,
deployis conventional. Run the next steps as that user, not as root.sudo adduser --disabled-password --gecos "" deploy sudo -iu deploy -
Install nvm from the official release. Pin the version rather than
masterso the same script reproduces a year from now:curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.1/install.sh | bashThe installer appends three lines to
~/.bashrc(or~/.zshrc). Re-source the shell or start a fresh login shell. -
Install the LTS the application targets, then mark it the default:
nvm install 20 nvm alias default 20 nvm use default node -v && npm -v -
Drop an
.nvmrcin the project root containing just the major (or exact) version:echo "20" > /var/www/app/.nvmrc cd /var/www/app && nvm install && nvm useAnyone running
nvm useinside the repo gets the right Node without thinking about it. -
For systemd-managed services, do not rely on
nvm use. Resolve the absolute path once and use it directly:# /etc/systemd/system/app.service [Service] User=deploy WorkingDirectory=/var/www/app Environment=NODE_ENV=production ExecStart=/home/deploy/.nvm/versions/node/v20.18.0/bin/node server.js Restart=on-failureWhen the runtime is upgraded, edit the unit file (or symlink
~/.nvm/versions/node/current) and runsystemctl daemon-reload && systemctl restart app.
Operational notes
- Do not run
npm installas root — it executes the package’s lifecycle scripts, which can be hostile in transitive dependencies. Always run installs as the deploy user. nvm install --ltsfloats with each LTS release; in production prefer an explicit major like20or an exact20.18.0so an off-hours pull does not surprise you.- The
.bashrchook adds ~0.3 s to every login shell. On a host you SSH into hundreds of times a day, source nvm only in interactive shells ([[ $- == *i* ]] && source ~/.nvm/nvm.sh). - nvm does not handle native module rebuilds across Node majors. After
nvm install 22, runnpm rebuildin the project before starting the service, or you will hitNODE_MODULE_VERSIONerrors at boot. - For CI runners and immutable images, prefer a single-version install of Node from NodeSource or a distroless container — nvm is a developer tool, and CI does not need its flexibility.
Once Node is pinned, the next operational concern is keeping the process alive — that lives in PM2 process management for Node.js. Stack Harbor handles runtime pinning and upgrade cycles as part of the operating layer we run for clients on managed operations — when an LTS goes EOL we have the rollout plan ready before the calendar date.