On this page
- When is nvm the right tool?
- How do you install nvm?
- How do you install and switch Node.js versions?
- How do you pin a version per project?
- How do you run an nvm-managed Node.js from systemd?
- What about cron jobs and scripts?
- How do you keep nvm and its Node.js versions up to date?
- How do you fix the common nvm problems?
- Questions
When is nvm the right tool?
Reach for nvm when one server hosts projects that need different Node.js majors, when you want to test an app against the next LTS before switching, or when you have no sudo rights. Each user gets an independent set of versions under ~/.nvm, and nothing touches system packages.
For a single production app that only ever needs the current LTS, a system install is simpler and gets updates with apt or dnf. That route is described on installing Node.js and npm. You can also mix the two: a system Node for tools, and nvm in the deploy account for apps.
How do you install nvm?
Log in as the user that will run the apps (this page uses deploy), not root. Make sure curl and the basic build tools are present, then fetch the install script for a tagged release. The tag below was current at the time of writing; check the project releases page and use the newest one.
# Ubuntu and Debian
sudo apt install -y curl build-essential
# Rocky and Alma
sudo dnf install -y curl gcc-c++ make
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.3/install.sh | bashThe installer clones nvm into ~/.nvm and appends a few lines to ~/.bashrc. Load them into the current session and confirm the command exists:
source ~/.bashrc
command -v nvm
nvm --versioncommand -v nvm prints nvm because it is a shell function, not a file on disk. That detail matters later when you call Node from systemd.
How do you install and switch Node.js versions?
Install by major number, by exact version or by alias. nvm downloads a prebuilt binary for your architecture, so each install takes seconds.
nvm install --lts
nvm install 22
nvm install 24.11.1
nvm ls
nvm ls-remote --lts | tail -n 5Switch the current shell with nvm use, and set what new shells start with by changing the default alias:
nvm use 22
node -v
nvm alias default 24
nvm currentGlobal npm packages are stored per version. When you move to a new release, bring them along with nvm install 24 --reinstall-packages-from=22, otherwise tools such as PM2 seem to disappear after the switch.
How do you pin a version per project?
Put a .nvmrc file in the project root containing the version. Anyone who enters the folder and runs nvm use or nvm install gets exactly that release.
cd ~/apps/shop
echo "24" > .nvmrc
nvm install
nvm use
node -vWriting just the major keeps you on its newest patch whenever you run nvm install again. Pin a full version such as 24.11.1 when you need byte-for-byte repeatable builds. Add the same requirement to package.json under engines so npm warns on a mismatch.
How do you run an nvm-managed Node.js from systemd?
Use the absolute path to the binary. systemd does not read .bashrc, so the nvm function and its PATH changes do not exist there. Find the path first:
nvm which 24It prints something like /home/deploy/.nvm/versions/node/v24.11.1/bin/node. Use that in the unit file:
[Unit]
Description=Shop API
After=network.target
[Service]
User=deploy
WorkingDirectory=/home/deploy/apps/shop
Environment=NODE_ENV=production
ExecStart=/home/deploy/.nvm/versions/node/v24.11.1/bin/node server.js
Restart=on-failure
[Install]
WantedBy=multi-user.targetSave it as /etc/systemd/system/shop.service, then run sudo systemctl daemon-reload and sudo systemctl enable --now shop. The full path pins the version explicitly, so after an upgrade you edit the unit and restart. A stable alternative is a symlink you control, for example ~/node-current, that you repoint during upgrades. More about unit files is in writing systemd services, and PM2 users should read keeping apps running with PM2, which handles nvm paths on its own.
What about cron jobs and scripts?
Cron also runs without your shell profile. Either call the absolute path, or load nvm at the top of a wrapper script:
#!/usr/bin/env bash
export NVM_DIR="$HOME/.nvm"
. "$NVM_DIR/nvm.sh"
cd /home/deploy/apps/shop
nvm use --silent
node scripts/nightly-report.jsMake it executable and call it from crontab -e. The wrapper respects the project .nvmrc, so the job follows the app when you change versions.
How do you keep nvm and its Node.js versions up to date?
Update nvm itself by running the install script of a newer tag; it upgrades the existing checkout in place. For Node.js, install the newest patch of your line and move the default alias:
nvm install 24 --reinstall-packages-from=current
nvm alias default 24
nvm lsNothing patches old versions automatically, which is the main trade-off against a package-manager install. Put a reminder in your maintenance routine, then remove releases you no longer use with nvm uninstall 22.20.0 to free disk space. Each version takes roughly 100 to 200 MB with its global packages.
How do you fix the common nvm problems?
- nvm: command not found: the profile lines were not loaded. Run
source ~/.bashrc, or check that the installer wrote to the file your shell actually reads (zsh uses~/.zshrc). - Works over SSH, fails in a service: the service has no nvm PATH. Use the absolute binary path as shown above.
- sudo node not found: sudo resets PATH. Avoid running apps as root; if a one-off command truly needs it, call
sudo "$(nvm which current)". - Prefix warning: nvm refuses to work if
~/.npmrcsets a globalprefix. Remove that line withnpm config delete prefix.
To remove nvm completely, delete ~/.nvm and the lines it added to your profile.
Plans that fit this guide
Base city prices. Every plan includes a dedicated IPv4 address, IPv6, full root and all 18 images.
Questions about Several Node.js versions with nvm
Should I install nvm as root?
No. nvm is designed for one user at a time. Install it in the account that owns and runs the application, such as deploy, and keep root out of the app path.
Can nvm and a system Node.js coexist?
Yes. Run nvm use system to fall back to the package-manager install, and nvm use 24 to go back. Services pick whichever binary path you write into them.
Does nvm get security updates for Node.js automatically?
No. You decide when to run nvm install for a newer patch. Check the Node.js security release notes and update your pinned versions on a schedule.
Is .nvmrc read automatically when I cd into a folder?
Not by default. You run nvm use in the folder. Shell hooks that switch automatically exist in the nvm README if you want them.
Does nvm work on Alpine or FreeBSD?
Alpine uses musl, so the prebuilt glibc binaries do not run and nvm compiles from source instead, which is slow. On FreeBSD use the system packages. This guide covers Ubuntu, Debian, Rocky and Alma.