How to Install Node.js on Ubuntu

Install Node.js on Ubuntu with apt nodejs, NodeSource setup scripts, NVM for multiple versions, or the official nodejs.org linux-x64 tar.xz under /opt, verify with node --version and npm --version, and uninstall per method.

Published

Updated

Read time 8 min read

Reviewed byDeepak Prasad

Install Node.js on Ubuntu banner with Node.js logo and terminal version output

Node.js is the server-side JavaScript runtime built on Chrome’s V8 engine. On Ubuntu you can install it from apt (distro nodejs in universe), NodeSource (vendor apt repo for a chosen major), NVM (per-user version switching), or the official linux-x64 tar.xz from nodejs.org. Each path ships node, npm, and usually npx, but they land in different paths and update differently.

This guide covers all four methods on amd64 Ubuntu, with verify steps, a minimal HTTP smoke test, update and uninstall paths, and troubleshooting. Current ranking guides agree on the same split: apt for a single distro build, NodeSource for a pinned system-wide major, NVM when you switch versions per project, and a tarball when you want a fixed upstream binary without adding a repository. Pick one primary method per machine unless you deliberately isolate versions (for example system apt Node plus NVM in your developer account).

Tested on: Ubuntu 26.04 LTS (Resolute Raccoon); kernel 7.0.0-27-generic.

NOTE
Use one install channel at a time on a given PATH. Stacking NodeSource nodejs, a tarball under /opt, and NVM without careful ordering leads to node --version surprises—run which -a node after every install.

Quick command summary

Task Command
Install from Ubuntu apt sudo apt update && sudo apt install -y nodejs npm
Install from NodeSource (22.x example) curl -fsSL https://deb.nodesource.com/setup_22.x | sudo -E bash - then sudo apt install -y nodejs
Install NVM curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.5/install.sh | bash
Install LTS via NVM nvm install --lts
Install from tar.xz wget https://nodejs.org/dist/v22.17.0/node-v22.17.0-linux-x64.tar.xz then extract under /opt/nodejs
Check versions node --version and npm --version
List all node binaries which -a node
Switch NVM version nvm use 22
Remove apt / NodeSource sudo apt purge -y nodejs npm

Prerequisites

  • Ubuntu 22.04 LTS, 24.04 LTS, 26.04 LTS, or newer on x86_64 (amd64). ARM hosts should use distro nodejs, NVM arm64 tarballs, or install Node.js on Debian patterns on arm64.
  • sudo for apt, /opt, and system-wide symlinks — see apt command.
  • Outbound HTTPS to archive.ubuntu.com, deb.nodesource.com, nodejs.org, or raw.githubusercontent.com (NVM), depending on method.
  • curl or wget for download-based installs.

See check Ubuntu version if you are unsure which release apt should target. For broader CLI reference, see Linux commands.


Choose an install method

Method Best for Jump to
apt install nodejs Fast system-wide Node aligned with Ubuntu security updates Method 1
NodeSource apt repo Pin a specific major (20, 22, 24) with apt upgrade Method 2
NVM Developers switching Node majors per project Method 3
Official tar.xz Air-gapped or fixed upstream binary under /opt without a repo Method 4

For most tutorials and IDEs, NVM or NodeSource keeps you on a supported LTS. apt is fine when Ubuntu’s packaged major matches your stack.


Method 1: Install Node.js from Ubuntu apt

Ubuntu ships nodejs and npm in the universe component. Enable universe if your image is minimal, refresh indexes, and install:

bash
sudo apt update
sudo apt install -y nodejs npm

Verify with the packaged binary (not every node on PATH is the apt build—IDEs and other tools sometimes ship their own):

bash
/usr/bin/node --version
/usr/bin/npm --version
which -a node
text
v22.22.1
9.2.0
/root/.cursor-server/bin/linux-x64/.../node
/usr/bin/node
/bin/node

On Ubuntu 26.04 the packaged major was 22; on 22.04 or 24.04 LTS you may see a different major—always run /usr/bin/node --version after install. Ubuntu’s build may lag the current LTS on nodejs.org but receives distro security maintenance.

HINT
If apt install nodejs reports no installation candidate, run sudo apt install software-properties-common and sudo add-apt-repository universe, then sudo apt update again.

Method 2: Install Node.js from NodeSource

NodeSource publishes setup scripts that add a signed apt repository for Node 20.x, 22.x, or 24.x. Remove an existing distro nodejs package first so which node does not keep resolving to the older build.

Purge conflicting packages, install prerequisites, and run the setup script (22.x example):

bash
sudo apt purge -y nodejs npm
sudo apt update
sudo apt install -y ca-certificates curl gnupg
curl -fsSL https://deb.nodesource.com/setup_22.x | sudo -E bash -
sudo apt install -y nodejs

Verify:

bash
node --version
npm --version
which node

After a successful setup, node --version should report a 22.x line and npm is bundled with the NodeSource package (no separate npm apt install).

Future updates:

bash
sudo apt update && sudo apt upgrade -y nodejs
NOTE
On the test host used for this refresh, curl -fsSL https://deb.nodesource.com/setup_22.x failed with SSL certificate verify result: unable to get local issuer certificate (20) before the signing key could be imported. That is an environment trust issue, not a NodeSource script bug—on a normal Ubuntu desktop with current ca-certificates, the script prints repository configuration lines and exits cleanly. Fix corporate TLS interception or refresh CA trust, then rerun the pipe.

Method 3: Install Node.js with NVM

NVM (Node Version Manager) installs multiple Node versions under ~/.nvm and switches what node resolves to in your shell. It is the usual choice when one laptop runs Node 20 for legacy apps and Node 24 for greenfield work.

This article keeps NVM steps short; the full workflow (bash vs zsh, .nvmrc, Snap curl pitfalls) is in install NVM on Ubuntu.

Quick path after cURL and git are installed:

bash
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.5/install.sh | bash
source ~/.bashrc
command -v nvm
nvm install --lts
node --version
npm --version
text
nvm
v24.18.0
11.16.0

Set a default for new shells:

bash
nvm alias default lts/*

Switch versions anytime with nvm use 22 or nvm use 24.


Method 4: Install Node.js from the official tar.xz

Node.js publishes prebuilt linux-x64 archives at nodejs.org/dist. Use this when you want a fixed upstream binary under /opt without NodeSource or NVM—common on servers with strict change control.

Download the tarball

Pick a version from the download page:

bash
cd /tmp
NODE_VER=v22.17.0
wget "https://nodejs.org/dist/${NODE_VER}/node-${NODE_VER}-linux-x64.tar.xz"
ls -lh "node-${NODE_VER}-linux-x64.tar.xz"
text
-rw-r--r-- 1 root root 30M Jun 25  2025 node-v22.17.0-linux-x64.tar.xz

Inspect contents before extracting:

bash
tar -tf "node-${NODE_VER}-linux-x64.tar.xz" | head
text
node-v22.17.0-linux-x64/
node-v22.17.0-linux-x64/bin/
node-v22.17.0-linux-x64/bin/node
node-v22.17.0-linux-x64/bin/npm
...

Install under /opt and expose binaries

Extract with the tar command and link into /usr/local/bin so node is on default PATH:

bash
sudo mkdir -p /opt/nodejs
sudo tar -xJf "node-${NODE_VER}-linux-x64.tar.xz" -C /opt/nodejs --strip-components=1
sudo ln -sf /opt/nodejs/bin/node /usr/local/bin/node
sudo ln -sf /opt/nodejs/bin/npm /usr/local/bin/npm
sudo ln -sf /opt/nodejs/bin/npx /usr/local/bin/npx
sudo ln -sf /opt/nodejs/bin/corepack /usr/local/bin/corepack

Verify:

bash
/usr/local/bin/node --version
/usr/local/bin/npm --version
which node
text
v22.17.0
10.9.2
/usr/local/bin/node
IMPORTANT
Official Node.js linux-x64 tarballs target x86_64 only. On ARM laptops or Raspberry Pi, use distro nodejs, NVM with the matching arch tarball, or the linux-arm64 build from nodejs.org—not linux-x64.

Verify Node.js and npm

Regardless of method, confirm the runtime and package manager:

bash
node --version
npm --version
npx --version
which -a node

Optional REPL smoke test:

bash
node -e "console.log('Node OK:', process.version)"
text
Node OK: v22.17.0

If node is missing or shows an unexpected version, check which command and your PATH—NVM users must source ~/.bashrc or open a new terminal.


Run a minimal HTTP server

Create hello.js:

javascript
const http = require('http');
http.createServer((req, res) => {
  res.writeHead(200, { 'Content-Type': 'text/plain' });
  res.end('Hello from Node.js on Ubuntu\n');
}).listen(8080, () => console.log('Listening on http://127.0.0.1:8080'));
Output

Run it:

bash
node hello.js

In another terminal:

bash
curl -s http://127.0.0.1:8080/
text
Hello from Node.js on Ubuntu

Stop the server with Ctrl+C. For interactive exploration, see Node.js REPL.


Keep Node.js updated

Install path Update command
apt sudo apt update && sudo apt upgrade -y nodejs npm
NodeSource sudo apt update && sudo apt upgrade -y nodejs
NVM nvm install --lts or nvm install 24 then nvm alias default 24
tar.xz Download newer tarball, re-extract to /opt/nodejs, refresh symlinks

After any upgrade, re-run node --version and reinstall global npm tools if their majors changed.


Uninstall Node.js

apt or NodeSource

bash
sudo apt purge -y nodejs npm
sudo apt autoremove -y

If you used NodeSource, remove its list file when you no longer want the repo:

bash
sudo rm -f /etc/apt/sources.list.d/nodesource.list
sudo apt update

NVM

bash
nvm uninstall 22
nvm uninstall 24
# when done: rm -rf ~/.nvm and remove NVM block from ~/.bashrc

See install NVM on Ubuntu for the full removal checklist.

tar.xz under /opt

bash
sudo rm -rf /opt/nodejs
sudo rm -f /usr/local/bin/node /usr/local/bin/npm /usr/local/bin/npx /usr/local/bin/corepack

For mixed installs and orphan cleanup, see remove software on Ubuntu.


Troubleshooting

Symptom Likely cause Fix
node: command not found PATH not loaded (NVM) or symlinks missing (tar.xz) source ~/.bashrc or recreate /usr/local/bin links
node --version shows wrong major Another node earlier on PATH which -a node; call /usr/bin/node or fix NVM default
E: Unable to locate package nodejs Universe disabled or outdated indexes Enable universe; sudo apt update
NodeSource curl SSL verify error Missing or intercepted CA trust sudo apt install ca-certificates; fix proxy TLS; retry setup script
nvm: command not found Installer not sourced source ~/.nvm/nvm.sh or open new terminal
Wrong architecture tarball Downloaded linux-x64 on ARM Pick linux-arm64 from nodejs.org or use apt/NVM
npm very old with apt Node Ubuntu pins npm separately from nodejs Use NodeSource/NVM/tar.xz for bundled npm, or upgrade deliberately

References


Summary

Install Node.js on Ubuntu with sudo apt install nodejs npm when the distro major is enough, NodeSource when you need a specific LTS major via apt, NVM when you switch versions per project, or the official linux-x64 tar.xz under /opt when you want a pinned upstream binary without a repository. Verify with node --version, npm --version, and which -a node, then uninstall using the path that matches how you installed.

Next: wire Node into your editor (VS Code, Sublime), or continue with the Node.js tutorial index.


Frequently Asked Questions

1. How do I install Node.js on Ubuntu?

Enable universe, run sudo apt update && sudo apt install -y nodejs npm for the distro build, or add NodeSource with curl -fsSL https://deb.nodesource.com/setup_22.x | sudo -E bash - then sudo apt install -y nodejs. Developers who switch versions per project should use NVM (ubuntu-install-nvm) or the official nodejs.org linux-x64 tar.xz under /opt.

2. Is Node.js in the Ubuntu apt repositories?

Yes—nodejs and npm live in universe on current Ubuntu releases. Versions track what Ubuntu security teams package (for example Node 22 on 26.04), not necessarily the newest upstream Current release. Use NodeSource, NVM, or a tarball when you need a specific major.

3. Should I use apt, NodeSource, NVM, or the tar.xz on Ubuntu?

apt is fastest for one system-wide version aligned with Ubuntu updates. NodeSource pins a major (20, 22, 24) via apt. NVM installs per-user under ~/.nvm and is best when projects need different majors. tar.xz is for air-gapped hosts or a fixed upstream binary under /opt without adding a repo.

4. How do I check the Node.js and npm version on Ubuntu?

Run node --version and npm --version after install. Use which -a node when multiple binaries exist. For apt or NodeSource, /usr/bin/node is the system package; NVM installs live under ~/.nvm/versions/node.

5. How do I update Node.js on Ubuntu?

apt or NodeSource: sudo apt update && sudo apt upgrade nodejs. NVM: nvm install --lts or nvm install 24 then nvm alias default. tar.xz: download a newer tarball, extract over /opt/nodejs, refresh symlinks in /usr/local/bin.

6. How do I uninstall Node.js from Ubuntu?

apt/NodeSource: sudo apt purge -y nodejs npm and remove /etc/apt/sources.list.d/nodesource.list if present. NVM: nvm uninstall VERSION, then remove ~/.nvm. tar.xz: delete /opt/nodejs and remove /usr/local/bin symlinks. Pick the uninstall that matches how you installed—do not mix methods on one machine without cleaning up first.
Deepak Prasad

R&D Engineer

Founder of GoLinuxCloud with more than 15 years of expertise in Linux, Python, Go, Laravel, DevOps, Kubernetes, Git, Shell scripting, OpenShift, AWS, Networking, and Security. With extensive …