Node.js on Ubuntu

Servers from the NodeSource apt repository with automatic patches, development machines with nvm.

On a server Node.js comes from a signed apt repository pinned to one LTS line, security updates install themselves and the app runs as a systemd service under its own user. On your computer nvm switches versions per project. The guide covers both, how to move to a new major and how to migrate a server away from nvm.

Vladimír NeporAuthorVladimír Nepor
PublishedUpdated
A production server installs Node.js 26 from the signed NodeSource apt repository, unattended-upgrades applies its patches and the app runs as a systemd service under its own user. A development computer uses nvm with several versions side by side and the project version in .nvmrc.Production serverapt · deb.nodesource.comnode_26.x · pinned 600/usr/bin/nodesystemd · User=myapppatches automatic ✓major only by hand ✕Your computerdevelopment~/.nvm/versions/nodev22v24v26.nvmrc → 26nvm install · nvm usesame major02 · SERVERNodeSource reposigned, pinned to one LTSchapter →03 · UPDATESAutomatic patchesunattended-upgradeschapter →04 · APPsystemd serviceown user, never rootchapter →07 · DEVELOPMENTnvma version per projectchapter →
Contents

01 · Start

Choosing the method and version

There are several ways to get Node.js onto Ubuntu and each fits a different job. On a server you want one version, installed system-wide and patched automatically. On a developer machine you want to switch versions per project. The two needs have different answers.

MethodUse it forUpdatesVerdict
NodeSource apt repositoryProduction servers, CI runnersAutomatic patches and minor versions within one major (unattended-upgrades)Recommended for servers
nvmYour own computer, WSL, trying a new versionManual, per userRecommended for development
Docker imageApps that already run in containersBy rebuilding the image (e.g. node:26-bookworm-slim)Good; then Node.js is not installed on the host at all
Ubuntu archive (apt install nodejs)Tools that only need some Node.jsAutomatic, but the version is old and security fixes for universe come with Ubuntu ProOnly when the old version is enough
Why not nvm on a server

nvm is built to be installed per user and loaded per shell from ~/.bashrc. systemd does not read .bashrc, so services need hard-coded paths into someone's home directory, symlinks in /usr/local/bin that break with every version change, or extra group membership to read another user's home. Above all, nobody patches it: every Node.js security release means logging in and running nvm install by hand. The apt route solves all of that. If you keep nvm on a server anyway, the older approach is kept in its own chapter.

Which version

Production should run only an LTS line (Active or Maintenance). Lines that are not LTS (Current, and from 2026 Alpha) are for testing. State as of 3 November 2026, according to the Node.js release schedule:

LineStatusSupported untilFor a server
Node.js 26 (Lithium)Active LTS since 28 Oct 202630 Apr 2029New servers
Node.js 24 (Krypton)Maintenance LTS30 Apr 2028Fine for existing apps; plan the move to 26
Node.js 22 (Jod)Maintenance LTS30 Apr 2027Upgrade soon
Node.js 27Alpha, release in April 2027, LTS from October 202730 Apr 2030Not yet

From Node.js 27 there is one major release a year and every one becomes LTS. The commands below use 26; for another line replace node_26.x with e.g. node_24.x.

Remember
  • Server: NodeSource repository pinned to one LTS major, patches installed automatically, app as a systemd service under its own user.
  • Your computer: nvm, with the project's version in .nvmrc, the same major as the server.
  • A new major is a deliberate step: test with nvm locally, then change one line on the server.

02 · Server

Installing from the NodeSource repository

NodeSource builds the official Node.js releases as .deb packages, one repository per major line. You add it the same way as NodeSource's own setup script does, only by hand: you verify the key before apt trusts it and you see every file that is written. Run the commands as your admin user with sudo, not as the application user.

Prerequisite

Ubuntu 22.04, 24.04 or 26.04 on amd64 or arm64. If you used nvm for Node.js on this server before, follow Moving a server from nvm after this chapter.

Step 1: Remove Node.js from the Ubuntu archive (if installed)

Packages from the Ubuntu archive (nodejs, npm, libnode-dev) collide with the NodeSource package. Check with apt list --installed 2>/dev/null | grep -E '^(nodejs|npm|libnode)'; with empty output skip this step.

Terminal
sudo apt purge -y nodejs npm libnode-dev    # only if Node.js from the Ubuntu archive is installed
sudo apt autoremove -y

Step 2: Download the signing key and check its fingerprint

The key is first saved to a temporary file. Nothing trusts it yet:

Terminal
curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key -o /tmp/nodesource.asc
gpg --show-keys --with-fingerprint /tmp/nodesource.asc
Output
pub   rsa2048 2016-05-23 [SC]
      6F71 F525 2828 41EE DAF8  51B4 2F59 B5F9 9B1B E0B4
uid                      NSolid <nsolid-gpg@nodesource.com>
sub   rsa2048 2016-05-23 [E]

The fingerprint must be exactly 6F71 F525 2828 41EE DAF8 51B4 2F59 B5F9 9B1B E0B4 (verified on 3 November 2026; the key is named NSolid, after NodeSource's product, that is correct). If it differs, stop here.

Step 3: Store the key for apt

The key goes to /etc/apt/keyrings and is used only for this one repository (Signed-By below), not for all of apt:

Terminal
sudo install -d -m 0755 /etc/apt/keyrings
sudo gpg --dearmor --yes -o /etc/apt/keyrings/nodesource.gpg /tmp/nodesource.asc
sudo chmod 644 /etc/apt/keyrings/nodesource.gpg
rm /tmp/nodesource.asc

Step 4: Add the node_26.x repository

In the deb822 format that Ubuntu itself uses since 24.04 and that NodeSource's script writes too (works on 22.04 as well). The repository is bound to one major line:

Terminal
sudo tee /etc/apt/sources.list.d/nodesource.sources <<EOF
Types: deb
URIs: https://deb.nodesource.com/node_26.x
Suites: nodistro
Components: main
Architectures: $(dpkg --print-architecture)
Signed-By: /etc/apt/keyrings/nodesource.gpg
EOF

nodistro is correct: one repository serves all Debian and Ubuntu releases.

Step 5: Prefer the NodeSource package

Priority 600 (above the default 500) makes apt always take nodejs from NodeSource, even if the Ubuntu archive ever offers a package with a higher version string. The pin applies only to the nodejs package; the rest of the system is unaffected.

Terminal
sudo tee /etc/apt/preferences.d/nodejs <<'EOF'
Package: nodejs
Pin: origin deb.nodesource.com
Pin-Priority: 600
EOF

The file name is the same as the one NodeSource's script uses, so running the script later overwrites it instead of creating a duplicate.

Step 6: Install Node.js

The nodejs package from NodeSource includes npm and npx; do not install the separate npm package from Ubuntu.

Terminal
sudo apt update && sudo apt install -y nodejs

Step 7: Verify

Terminal
node -v && npm -v
which -a node
apt-cache policy nodejs
Output
v26.x.x
11.x.x
/usr/bin/node
nodejs:
  Installed: 26.x.x-1nodesource1
  Candidate: 26.x.x-1nodesource1
  Version table:
 *** 26.x.x-1nodesource1 600
        600 https://deb.nodesource.com/node_26.x nodistro/main amd64 Packages
     …

Expected: version 26, a single path /usr/bin/node and a *** next to the line from deb.nodesource.com with priority 600. Another path before /usr/bin/node (e.g. ~/.nvm/… or /usr/local/bin/node) means an older installation shadows the new one, see Troubleshooting.

03 · Server

Automatic security updates

apt upgrade already updates Node.js from the new repository. To have it done automatically like the rest of the system, allow the repository in unattended-upgrades. Because the repository is bound to node_26.x, only patch and minor releases of line 26 arrive automatically, never a jump to the next major.

Step 8: Check that unattended-upgrades runs

Terminal
systemctl is-enabled unattended-upgrades
cat /etc/apt/apt.conf.d/20auto-upgrades
Output
enabled
APT::Periodic::Update-Package-Lists "1";
APT::Periodic::Unattended-Upgrade "1";

A different output: set it up first following Unattended upgrades.

Step 9: Allow the NodeSource repository

By default unattended-upgrades installs only from the Ubuntu archives. A separate file adds NodeSource to the allowed origins (apt merges the lists from all files), and 50unattended-upgrades stays untouched so a package update cannot overwrite your change:

Terminal
sudo tee /etc/apt/apt.conf.d/51nodesource-upgrades <<'EOF'
Unattended-Upgrade::Origins-Pattern {
        "site=deb.nodesource.com";
};
EOF

The match is by host name (site). Matching by origin (o=…) is not useful here: the NodeSource repository publishes the generic origin . nodistro.

Step 10: Verify with a dry run

Terminal
sudo unattended-upgrade --dry-run --debug 2>&1 | grep -iE "allowed origins|nodejs"
Output
Allowed origins are: o=Ubuntu,a=noble, o=Ubuntu,a=noble-security, …, site=deb.nodesource.com

site=deb.nodesource.com must be in the Allowed origins line. If an update is waiting, a Checking: nodejs line follows. What was really installed is in the log:

Terminal
grep -i nodejs /var/log/unattended-upgrades/unattended-upgrades.log | tail -n 5
The running app keeps the old version until it restarts

The update replaces /usr/bin/node on disk; a running process keeps using the old binary in memory. Security fixes therefore only take effect after a restart of the service. Ubuntu's needrestart can do it automatically depending on its configuration; the command below lists services that still run the old binary. Restart them (sudo systemctl restart myapp) at a time that suits you, or put your app behind a process that can restart without downtime.

Terminal
sudo needrestart -r l    # lists services still running the old binary

04 · Server

Running the app as a service

Node.js is now system-wide and owned by root, which is standard. What matters is that the application does not run as root: it gets its own system user without a login, and systemd starts it, restarts it after a crash and starts it again after a reboot. Replace myapp with the name of your application.

Step 11: Create the application user and directories

Terminal
sudo useradd --system --create-home --home-dir /srv/myapp --shell /usr/sbin/nologin myapp
sudo install -d -o myapp -g myapp -m 0750 /srv/myapp/app /srv/myapp/data

app is for the code, data for what the app writes (uploads, SQLite, cache). See also user management (in Czech).

Step 12: Copy the code and install dependencies

Put the application into /srv/myapp/app (e.g. a git clone), make it owned by myapp and install exactly what the lock file says, without development dependencies:

Terminal
cd /srv/myapp/app
sudo -u myapp npm ci --omit=dev

Step 13: Create the systemd service

ExecStart points to /usr/bin/node, a path that stays the same through every update. No PATH tricks are needed:

/etc/systemd/system/myapp.service
[Unit]
Description=My Node.js app
Wants=network-online.target
After=network-online.target

[Service]
Type=simple
User=myapp
Group=myapp
WorkingDirectory=/srv/myapp/app
Environment=NODE_ENV=production
ExecStart=/usr/bin/node server.js
Restart=on-failure
RestartSec=5

# Hardening
NoNewPrivileges=yes
PrivateTmp=yes
ProtectSystem=strict
ProtectHome=yes
ReadWritePaths=/srv/myapp/data

[Install]
WantedBy=multi-user.target

The Hardening part makes the whole file system read-only for the app except ReadWritePaths and hides /home. If the app needs to write elsewhere, add the path there. Ports below 1024 (80, 443) belong to a reverse proxy (Caddy, Nginx) in front of the app; do not run Node.js as root for them.

Step 14: Start and enable the service

Terminal
sudo systemctl daemon-reload
sudo systemctl enable --now myapp
systemctl status myapp --no-pager
journalctl -u myapp -f

05 · Server

Moving to a new major version

A major version can break dependencies (native modules, removed APIs), so it is never installed automatically. Change it on purpose, ideally while the current line is still supported: Node.js 26 until 30 April 2029. The example moves to line 27 (LTS from October 2027).

Step 15: Test the app with the new version on your computer

With nvm the new line runs next to the current one:

Terminal
nvm install 27 && nvm use 27 && npm ci && npm test    # on your computer, in the project

Step 16: Switch the repository on the server

One line in the repository file changes. The pin and unattended-upgrades need no change, they match the host, not the version:

Terminal
sudo sed -i -E 's#node_[0-9]+\.x#node_27.x#' /etc/apt/sources.list.d/nodesource.sources
sudo apt update && sudo apt install -y nodejs
node -v
sudo systemctl restart myapp

If the app uses native modules, run sudo -u myapp npm rebuild (or npm ci again) before the restart.

Rolling back to the previous major

Point the repository back to the previous line and install a concrete version from it:

Terminal
sudo sed -i -E 's#node_[0-9]+\.x#node_26.x#' /etc/apt/sources.list.d/nodesource.sources
sudo apt update
apt list -a nodejs                                     # pick the version to return to
sudo apt install -y --allow-downgrades nodejs=<VERSION>
sudo systemctl restart myapp

06 · Alternative

Node.js from the Ubuntu archive

Ubuntu ships its own nodejs package. It is the simplest option, but the version is fixed for the whole life of the Ubuntu release and is usually behind or past upstream support:

UbuntuNode.js in the archiveUpstream support
26.04 LTS22.22until 30 Apr 2027
24.04 LTS18.19ended 30 Apr 2025
22.04 LTS12.22ended 30 Apr 2022

The package is in the universe component: security fixes for it are not guaranteed for free; Canonical provides them through Ubuntu Pro (ESM Apps). npm is a separate package here. Use it when a tool only needs some Node.js (a build script, an older utility), not for an application facing the internet.

Terminal
apt-cache policy nodejs    # which version your Ubuntu offers
sudo apt update && sudo apt install -y nodejs npm

07 · Development

nvm on your computer

On your own computer (Linux, Mac, WSL on Windows) nvm is the right tool: several Node.js versions side by side in your home directory, no sudo, a switch per project. Use the same major there as on the server. On Windows without WSL use the separate project nvm-windows or fnm.

Step 17: Install nvm

Check the latest release on GitHub (v0.40.8 when this was written) and read the script before you run it:

Terminal
curl -fsSL https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.8/install.sh | less    # read it first
Terminal
curl -fsSL https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.8/install.sh | bash
source ~/.bashrc
nvm --version

The installer puts nvm in ~/.nvm and adds its loader to ~/.bashrc (or ~/.zshrc). Run it as your normal user, never with sudo.

Step 18: Install Node.js versions

Terminal
nvm install --lts          # current LTS, becomes the default
nvm install 24              # another line next to it
nvm ls                      # installed versions
nvm use 24                  # this shell only
nvm alias default 26        # default for new shells

Step 19: Pin the version per project

.nvmrc in the repository tells everyone (and CI) which line the project uses. Keep it on the same major as the server:

Terminal
echo "26" > .nvmrc    # in the project root, commit it
nvm install                 # installs the version from .nvmrc
nvm use                     # switches to it

Step 20: Keep it up to date

nvm does not update anything by itself. From time to time:

Terminal
nvm install --lts --reinstall-packages-from=current    # newest LTS patch, keep global packages
nvm ls-remote --lts | tail -n 5                          # latest LTS releases
nvm uninstall 22                                         # remove an old line

To update nvm itself, run the installer of the new release again; your installed Node.js versions stay.

08 · Tools

pnpm and Yarn through Corepack

pnpm and Yarn are best taken through Corepack: the exact version comes from the packageManager field in package.json, so the server uses the same version as your computer. Corepack is bundled with Node.js up to 24; since Node.js 25 it has to be installed from npm.

Node.js 24

Terminal
sudo corepack enable    # Node.js 24 and older: Corepack is bundled

Node.js 25 and newer (26)

Terminal
sudo npm install -g corepack@latest    # Node.js 25 and newer: Corepack is no longer bundled
sudo corepack enable

Packages installed with npm install -g are not managed by apt and are not updated by unattended-upgrades. Update them now and then with sudo npm update -g. Install the project's dependencies as the app user:

Terminal
cd /srv/myapp/app
sudo -u myapp pnpm install --frozen-lockfile --prod

09 · Legacy

nvm on a server (older approach)

The original procedure of this guide, kept for servers that already run Node.js through nvm and will stay that way. For a new server use the apt installation; to switch an existing one, see Moving a server from nvm.

What you take on with nvm on a server

No automatic updates: every Node.js security release means nvm install, new symlinks or edited units, and a restart, by hand. The binaries belong to an ordinary account, yet services and sudo node run them; whoever takes over that account runs code as the service or as root. Paths contain the exact version, so links and units must change with every update.

Node.js with NVM can be installed under any account, see below:

  1. Check the NVM install script
    Terminal
    curl -fsSL https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.8/install.sh | less
  2. Install NVM
    Terminal
    curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.8/install.sh | bash

    NVM is installed with the permissions of the user running the command (you): into the user's home directory (~/.nvm), and its loader is added to ~/.bashrc.

  3. Reload the shell profile so the nvm command is available
    Terminal
    source ~/.bashrc
  4. List the available LTS versions
    Terminal
    nvm list-remote --lts
  5. Install Node.js
    Terminal
    nvm install --lts          # current LTS
    nvm install v26.13.0       # or a specific version

    Node.js is installed with the permissions of the user running the command (you).

  6. Show the active version
    Terminal
    node -v

Node.js installed this way belongs to this user only: node -v works for you, but not for other users nor with sudo node -v. There are two ways to make the node command available to other users and services:

  1. Symlink

    A symlink from /usr/local/bin/node to the Node.js binary in the user's account ($NVM_DIR/versions/node/$(nvm version)/bin/node):

    Terminal
    sudo ln -sfn "$NVM_DIR/versions/node/$(nvm version)/bin/node" /usr/local/bin/node

    The link points to one specific version: after installing another version, run the command again. Since Ubuntu 21.04 home directories have mode 750, so other users reach the target only if they can enter your home (see the group below).

  2. Environment variable in the service, with group membership

    In a systemd service, add an Environment variable whose PATH leads to the Node.js binaries of the user with nvm:

    /etc/systemd/system/<SERVICE>.service
    [Unit]
    Description=My service
    Wants=network-online.target
    After=network-online.target
    
    [Service]
    User=<SERVICE_USER>
    Group=<SERVICE_USER>
    Type=simple
    Restart=always
    RestartSec=5
    WorkingDirectory=/directory/to/service/client
    Environment="PATH=/home/<NVM_USER>/.nvm/versions/node/v26.13.0/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
    ExecStart=/home/<NVM_USER>/.nvm/versions/node/v26.13.0/bin/node server.js
    
    [Install]
    WantedBy=multi-user.target

    To make node accessible, the <SERVICE_USER> must be added to the <NVM_USER> group (it then can read everything group-readable in that home):

    Terminal
    sudo usermod -aG <NVM_USER> <SERVICE_USER>
    sudo systemctl daemon-reload && sudo systemctl restart <SERVICE>

    After every Node.js update change the version in the unit and restart the service.

Useful commands

  • which node / which corepack: location of Node.js / Corepack
  • nvm ls: installed Node.js versions
  • npm -v: npm version (nvm installs a compatible npm with every Node.js version)

Installing next versions

Terminal
nvm list-remote          # available Node.js versions
nvm install vXX.X.X      # becomes the active version; the previous one stays (nvm ls)

Switching to a selected version

Terminal
nvm use vXX.X.X

Setting the default version

Terminal
nvm alias default vXX.X.X

Uninstalling unused versions

Terminal
nvm uninstall vXX.X.X

10 · Migration

Moving a server from nvm

An earlier version of this guide recommended nvm on the server too, with symlinks in /usr/local/bin or a PATH in the systemd unit. This is how to move such a server to the apt installation. First install Node.js from NodeSource (steps 1 to 7).

Step 21: Find what still uses nvm

Terminal
ls -l /usr/local/bin | grep -i nvm
grep -rl nvm /etc/systemd/system
npm ls -g --depth=0    # as the nvm user: global packages you will need again

Note down global packages (pm2 and similar); install them again later with sudo npm install -g, or better as project dependencies.

Step 23: Point the services to /usr/bin/node

For every unit found in step 21:

Terminal
sudo systemctl edit --full myapp    # ExecStart=/usr/bin/node …, delete Environment="PATH=…/.nvm/…"
sudo systemctl restart myapp
systemctl status myapp --no-pager

Step 24: Clean up

Once all services run on /usr/bin/node, remove the group membership and nvm. The names are from the old guide (nodejsusr, /srv/nodejs), adjust them to yours:

Terminal
sudo gpasswd -d <SERVICE_USER> <NVM_USER>    # the group membership is no longer needed
sudo rm -rf /srv/nodejs/.nvm                   # nvm of the old system account
sudo userdel nodejsusr && sudo rm -rf /srv/nodejs    # only if the account served nothing else

11 · Problems

Troubleshooting

node -v shows another version

Another node comes earlier in PATH: nvm loaded from ~/.bashrc, or a symlink in /usr/local/bin. The first line of which -a wins. After removing it run hash -r or open a new shell.

Terminal
which -a node
type node
echo "$PATH"

apt installs the old Ubuntu version

apt-cache policy nodejs does not show deb.nodesource.com with priority 600: the repository did not load (look for errors in sudo apt update) or the pin file is missing. Check steps 4 and 5.

Warning: Target Packages is configured multiple times

The repository is defined twice, typically from an older setup in the one-line format (nodesource.list) next to the new nodesource.sources. Keep one:

Terminal
ls /etc/apt/sources.list.d/ | grep -i node
sudo rm /etc/apt/sources.list.d/nodesource.list    # keep only nodesource.sources

dpkg: trying to overwrite '/usr/include/node/…'

A development package from the Ubuntu archive is still installed. Remove it and let apt finish the installation:

Terminal
sudo apt purge -y libnode-dev
sudo apt -f install

NO_PUBKEY or The following signatures couldn't be verified

The path in Signed-By does not lead to the key, or the key file is not readable. ls -l /etc/apt/keyrings/nodesource.gpg must show -rw-r--r--; otherwise repeat step 3.

Sources

Official documentation and further reading

Need a secure server setup?

We design, secure and document networks and servers.

Servers & Networks