Skip to content

How to Install Node.js and npm on Linux (apt, dnf and nvm)

Updated 8 min read

Updated: 11 October 2026 · Applies to: Node.js 24 LTS (24.21.0) and npm 11 on Debian 13, Ubuntu 24.04 and 26.04, AlmaLinux 10, Rocky Linux 9 and Fedora 44; nvm 0.40.8

To install Node.js on a Linux server, add the NodeSource repository for the current LTS release and install the nodejs package, which includes npm. On Ubuntu or Debian:

curl -fsSL https://deb.nodesource.com/setup_lts.x -o nodesource_setup.sh
sudo -E bash nodesource_setup.sh
sudo apt install -y nodejs
node -v

On AlmaLinux, Rocky Linux, RHEL or Fedora, use https://rpm.nodesource.com/setup_lts.x and sudo dnf install -y nodejs. In our tests on all six systems listed above, the NodeSource packages installed Node.js v24.21.0 with npm 11.19.0. The sections below cover the distribution packages, nvm, the official binaries and the problems people hit most.

Which Node.js version to install

According to the Node.js project, production applications should only use Active LTS or Maintenance LTS releases. At the time of writing Node.js 24 is the Active LTS and Node.js 22 the Maintenance LTS; Node.js 26 is the Current release and becomes the next LTS. Node.js 20 and older are end of life at nodejs.org (a distribution may still patch its own package).

The version your distribution ships is often older. These were the default nodejs packages in our tests:

  • Ubuntu 24.04: 18.19.1 (end of life upstream)
  • Debian 13: 20.19.2
  • Ubuntu 26.04: 22.22.1
  • Rocky Linux 9: 16.20.2 by default, 18 to 24 as module streams
  • AlmaLinux 10: 22.23.2, plus a separate nodejs24 package
  • Fedora 44: 22.23.1 (packages nodejs20, nodejs22 and nodejs24 exist side by side)

Method 1: NodeSource repository (recommended for servers)

NodeSource packages the Node.js releases as .deb and .rpm files, so you get updates through apt upgrade or dnf upgrade.

  1. Ubuntu and Debian:
    sudo apt install -y curl
    curl -fsSL https://deb.nodesource.com/setup_24.x -o nodesource_setup.sh
    sudo -E bash nodesource_setup.sh
    sudo apt install -y nodejs
    The script adds /etc/apt/sources.list.d/nodesource.sources and a pin file in /etc/apt/preferences.d/, so the NodeSource package wins over the distribution's older one. You do not need the separate npm package; npm and npx come with nodejs. On Ubuntu 26.04 the same commands worked with its new sudo (sudo-rs).
  2. AlmaLinux, Rocky Linux, RHEL and Fedora:
    curl -fsSL https://rpm.nodesource.com/setup_24.x -o nodesource_setup.sh
    sudo bash nodesource_setup.sh
    sudo dnf install -y nodejs
    The script adds /etc/yum.repos.d/nodesource-nodejs.repo with module_hotfixes=1, so on Rocky Linux 9 it installed 24.21.0 without touching the module streams.
  3. Check it:
    $ node -v
    v24.21.0
    $ npm -v
    11.19.0
    $ node -e 'console.log("Hello from Node.js " + process.version + " on " + process.platform + " " + process.arch)'
    Hello from Node.js v24.21.0 on linux x64

setup_lts.x follows the current LTS (it configured node_24.x in our test), while setup_24.x stays on the 24 line. Pick the fixed version on production servers, so a new major version only arrives when you choose.

Method 2: the distribution's own packages

Simpler, signed by your distribution, but often an older major version:

sudo apt install nodejs npm        # Debian, Ubuntu (npm is a separate package)
sudo dnf install nodejs            # AlmaLinux 10, Fedora
  • Rocky Linux 9: the plain package is still Node.js 16. List the module streams and enable a newer one first; on Rocky Linux 9 this installed 24.21.0 with npm 11.19.0:
    dnf module list nodejs
    sudo dnf module enable nodejs:24
    sudo dnf install nodejs
  • AlmaLinux 10: sudo dnf install nodejs24 nodejs24-npm installs Node.js 24, but in our test it only created the commands node-24, npm-24 and npx-24, and plain node gave command not found. Use NodeSource if your scripts expect node, or call node-24 directly. Fedora 44 behaved the other way round: sudo dnf install nodejs pulled in nodejs22 plus nodejs22-bin, which provides the plain node command (22.23.1).

Method 3: nvm (several versions, per user)

nvm installs Node.js in your home folder, without sudo, and lets you switch between versions per project. It suits developers and CI users more than system services.

  1. Install nvm as your normal user (not root), then open a new terminal:
    curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.8/install.sh | bash
    The installer adds three lines to ~/.bashrc that load nvm.
  2. Install the current LTS:
    $ nvm install --lts
    Checksums matched!
    Now using node v24.21.0 (npm v11.19.0)
    Creating default alias: default -> lts/* (-> v24.21.0 *)
  3. Add and switch versions:
    nvm install 22          # installed v22.23.3 in our test
    nvm use 24              # Now using node v24.21.0 (npm v11.19.0)
    nvm alias default 24    # the version new shells start with
    nvm ls                  # list installed versions

nvm only loads in interactive shells. In our test bash -c "node -v" as the same user gave node: command not found, so cron jobs and services must use the full path, such as /home/dev/.nvm/versions/node/v24.21.0/bin/node. For a server app, the packages from method 1 or 2 are simpler.

Method 4: the official binaries

Node.js also publishes ready-made Linux builds. They work on any distribution, but you update them by hand. For x86_64:

V=v24.21.0
curl -fsSLO https://nodejs.org/dist/$V/node-$V-linux-x64.tar.xz
curl -fsSLO https://nodejs.org/dist/$V/SHASUMS256.txt
grep "node-$V-linux-x64.tar.xz" SHASUMS256.txt | sha256sum -c -
sudo tar -xJf node-$V-linux-x64.tar.xz -C /usr/local --strip-components=1 --exclude='*.md' --exclude=LICENSE

The checksum line must end in OK. This puts node, npm, npx and corepack in /usr/local/bin; in our test node -v then printed v24.21.0. On ARM servers use the linux-arm64 file instead.

Install global npm tools without sudo

With the NodeSource or distribution packages, npm install -g as a normal user fails, because global packages go to /usr/lib/node_modules:

$ npm install -g cowsay
npm error code EACCES
npm error syscall mkdir
npm error path /usr/lib/node_modules/cowsay
npm error errno -13

Do not fix this with sudo npm. Give your user its own global folder instead:

mkdir -p ~/.npm-global
npm config set prefix ~/.npm-global
echo 'export PATH=$HOME/.npm-global/bin:$PATH' >> ~/.profile
source ~/.profile
npm install -g cowsay

Project dependencies need no global install at all: run npm install in the project folder and they go into its node_modules. More commands are in the npm commands cheat sheet.

Keep a Node.js app running

An app started with node app.js stops when you close the SSH session. A systemd service keeps it running, starts it at boot and restarts it after a crash. This is the unit we tested, with the app in /opt/hello running as its own user nodeapp:

sudo useradd --system --create-home --home-dir /opt/hello --shell /usr/sbin/nologin nodeapp
sudo nano /etc/systemd/system/hello.service

[Unit]
Description=Hello Node.js app
After=network.target

[Service]
User=nodeapp
WorkingDirectory=/opt/hello
ExecStart=/usr/bin/node /opt/hello/app.js
Restart=on-failure
Environment=NODE_ENV=production

[Install]
WantedBy=multi-user.target

Load and start it, then check it:

$ sudo systemctl daemon-reload
$ sudo systemctl enable --now hello
$ systemctl status hello --no-pager
● hello.service - Hello Node.js app
     Loaded: loaded (/etc/systemd/system/hello.service; enabled; preset: enabled)
     Active: active (running) since Sat 2026-10-10 13:58:12 UTC; 3s ago
   Main PID: 3561 (MainThread)
     Memory: 13.2M (peak: 13.4M)
     CGroup: /system.slice/hello.service
             └─3561 /usr/bin/node /opt/hello/app.js
$ journalctl -u hello -n 20

When we killed the process, systemd logged Scheduled restart job, restart counter is at 1 and started it again a second later. Use /usr/local/bin/node in ExecStart if you installed the official binaries, and the full nvm path if you used nvm.

Update, switch or remove Node.js

  • Updates within the same major version arrive with sudo apt upgrade or sudo dnf upgrade.
  • To move to a newer major version with NodeSource, run its setup script for that version (for example setup_26.x once Node.js 26 is LTS) and install nodejs again. Test your app first: native modules may need npm rebuild.
  • Remove Node.js with sudo apt purge nodejs or sudo dnf remove nodejs. To drop the NodeSource repository too, delete the files its script created: on Debian and Ubuntu /etc/apt/sources.list.d/nodesource.sources, /usr/share/keyrings/nodesource.gpg and the nodejs and nsolid files in /etc/apt/preferences.d/; on RPM systems /etc/yum.repos.d/nodesource-nodejs.repo and nodesource-nsolid.repo.

Common problems

  • Old version after installing: check where node comes from with which -a node and apt policy nodejs. An nvm version in your shell hides the system one.
  • node: command not found after installing nvm: open a new shell or run source ~/.bashrc.
  • App stops when you close SSH: run it as a systemd service (above) or with a process manager such as PM2.
  • npm uses a lot of disk space: see how to clear the npm cache.

On a CWP server, see install Node.js and run an app on CWP. For your own Node.js server with full root access, a KVM VPS runs any of the methods above.

Official documentation: Node.js downloads, NodeSource distributions and nvm.

Was this answer helpful?

Still stuck? Open a ticket