Skip to content

Installing Node.js with nvm on Ubuntu and Debian

Install and pin Node.js versions per project with nvm, then convert the developer workflow into something predictable on a production host.

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

  1. Create or pick the service user that will own the runtime. For a single-app box, deploy is conventional. Run the next steps as that user, not as root.

    sudo adduser --disabled-password --gecos "" deploy
    sudo -iu deploy
  2. Install nvm from the official release. Pin the version rather than master so the same script reproduces a year from now:

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

    The installer appends three lines to ~/.bashrc (or ~/.zshrc). Re-source the shell or start a fresh login shell.

  3. 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
  4. Drop an .nvmrc in the project root containing just the major (or exact) version:

    echo "20" > /var/www/app/.nvmrc
    cd /var/www/app && nvm install && nvm use

    Anyone running nvm use inside the repo gets the right Node without thinking about it.

  5. 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-failure

    When the runtime is upgraded, edit the unit file (or symlink ~/.nvm/versions/node/current) and run systemctl daemon-reload && systemctl restart app.

Operational notes

  • Do not run npm install as root — it executes the package’s lifecycle scripts, which can be hostile in transitive dependencies. Always run installs as the deploy user.
  • nvm install --lts floats with each LTS release; in production prefer an explicit major like 20 or an exact 20.18.0 so an off-hours pull does not surprise you.
  • The .bashrc hook 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, run npm rebuild in the project before starting the service, or you will hit NODE_MODULE_VERSION errors 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.