How to Install Node.js and npm on Ubuntu 22.04

By 

•

Updated on

•

8 min read

Install Node.js and npm on Ubuntu 22.04

Many web applications, build tools, and command-line utilities need Node.js and npm before you can install or run them. Node.js is a JavaScript runtime built on Chrome’s V8 engine, with npm as its default package manager and the world’s largest software registry. On Ubuntu 22.04, the Node.js version you end up with depends on where you install it from.

This guide covers three ways to install Node.js and npm on Ubuntu 22.04:

  • NodeSource repository - Install a specific Node.js version. NodeSource supports Node.js v26.x, v24.x, and v22.x.
  • nvm (Node Version Manager) - Manage multiple Node.js versions on the same machine. This is the preferred method for developers.
  • Ubuntu repository - The easiest way, but the included version (v12.22.9) is outdated and should not be used in production.

Choose the method that fits your needs. If you are not sure which version to install, check the documentation of the application you are deploying.

Quick Reference

TaskCommand
Install via NodeSourcesudo apt install nodejs (after adding repo)
Install via nvmnvm install --lts
Install via Ubuntu reposudo apt install nodejs npm
Check Node.js versionnode -v
Check npm versionnpm -v
List installed versions (nvm)nvm ls
Switch Node.js version (nvm)nvm use <version>
Set default version (nvm)nvm alias default <version>
Upgrade to a new major (nvm)nvm install <version> --reinstall-packages-from=<old>
Uninstall Node.jssudo apt remove nodejs or nvm uninstall <version>

Installing Node.js and npm from NodeSource

NodeSource is a company focused on providing enterprise-grade Node support. It maintains an APT repository containing multiple Node.js versions. Use this repository if your application requires a specific version of Node.js.

The first step is to install the dependencies necessary to add a new repository . Most likely, you will already have those packages installed on your system, but some packages may be missing:

Terminal
sudo apt update
sudo apt install ca-certificates curl gnupg

Next, import the NodeSource repository’s GPG key to your system:

Terminal
sudo mkdir -p /etc/apt/keyrings
curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | sudo gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg

At the time of writing, the NodeSource repository provides the following versions:

  • v26.x - The latest LTS version (Lithium).
  • v24.x - Maintenance LTS version (Krypton).
  • v22.x - Maintenance LTS version (Jod), supported until April 2027.

Node.js 20 reached end-of-life in April 2026. Its node_20.x repository is still online, but it no longer receives security fixes.

We will install Node.js version 26.x. If you need another version, change NODE_MAJOR=26 to your preferred version (for example, NODE_MAJOR=24). Run the following command to create the NodeSource repository file:

Terminal
NODE_MAJOR=26
echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_$NODE_MAJOR.x nodistro main" | sudo tee /etc/apt/sources.list.d/nodesource.list

Once the repository is enabled, install Node.js and npm:

Terminal
sudo apt update
sudo apt install nodejs

The nodejs package contains the node, npm, and npx binaries.

To verify that Node.js and npm were successfully installed, run the following command to print their versions:

Terminal
node --version
output
v26.13.1
Terminal
npm --version
output
11.20.0

After npm is installed, see the npm command guide for package installs, updates, scripts, and lock-file based installs.

To be able to compile native addons from npm, you will need to install the development tools:

Terminal
sudo apt install build-essential

Installing Node.js and npm using NVM

NVM (Node Version Manager) is a bash script that allows you to manage multiple Node.js versions on a per-user basis. With NVM, you can install and uninstall any Node.js version that you want to use or test.

Visit the nvm GitHub repository page and copy either the curl or wget command to download and install the nvm script:

Terminal
wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.41.0/install.sh | bash

Do not use sudo , as it will enable nvm for the root user.

The script will clone the project’s repository from GitHub to the ~/.nvm directory:

output
=> Close and reopen your terminal to start using nvm or run the following to use it now:

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion

As the output above says, you should either close and reopen the terminal or run the commands to add the path to the nvm script to the current shell session. You can do whatever is easier for you.

Once the script is in your PATH, verify that nvm was properly installed by typing:

Terminal
nvm -v
output
0.41.0

To get a list of all Node.js versions that can be installed with nvm, run:

Terminal
nvm list-remote

The command will print a vast list of all available Node.js versions.

output
...
       v22.23.4   (LTS: Jod)
       v22.23.5   (Latest LTS: Jod)
...
       v24.22.2   (LTS: Krypton)
       v24.22.3   (Latest LTS: Krypton)
...
       v26.13.0   (LTS: Lithium)
       v26.13.1   (Latest LTS: Lithium)    (Latest: node)

Each LTS release is labeled with the codename of its release line. The (Latest: node) label marks the newest Node.js release overall.

To install the latest LTS version of Node.js, run:

Terminal
nvm install --lts

The output should look something like this:

output
Installing latest LTS version.
Downloading and installing node v26.13.1...
...
Now using node v26.13.1 (npm v11.20.0)
Creating default alias: default -> lts/* (-> v26.13.1)

Once the installation is completed, verify it by printing the Node.js version:

Terminal
node -v
output
v26.13.1

Install two more versions, the latest Node.js 24 release and version 22.23.5:

Terminal
nvm install 24
nvm install 22.23.5

You can list the installed Node.js versions by typing:

Terminal
nvm ls

The output should look something like this:

output
->     v22.23.5
       v24.22.3
       v26.13.1
default -> lts/* (-> v26.13.1)
iojs -> N/A (default)
unstable -> N/A (default)
node -> stable (-> v26.13.1) (default)
stable -> 26.13 (-> v26.13.1) (default)
lts/* -> lts/lithium (-> v26.13.1)
...
lts/iron -> v20.20.2 (-> N/A)
lts/jod -> v22.23.5
lts/krypton -> v24.22.3
lts/lithium -> v26.13.1

The entry with an arrow (-> v22.23.5) is the Node.js version used in the current shell session. The default alias points to lts/*, which currently resolves to v26.13.1. The default version is the version that will be active when opening new shells.

To change the currently active version:

Terminal
nvm use 24
output
Now using node v24.22.3 (npm v11.19.1)

To change the default Node.js version:

Terminal
nvm alias default 24

For more detailed information about using the nvm script, visit the project’s GitHub page.

Installing Node.js and npm from the Ubuntu repository

Warning
The Node.js version in the Ubuntu 22.04 repositories (v12.22.9) reached end-of-life upstream in April 2022. Ubuntu published its last security update for the package in June 2024. Use NodeSource or nvm instead for production environments.

The installation is straightforward:

Terminal
sudo apt update
sudo apt install nodejs npm

This installs Node.js along with the tools needed to compile native addons from npm.

Verify the installation:

Terminal
node -v
output
v12.22.9
Terminal
npm -v
output
8.5.1

Both versions are several major releases behind the current Node.js and npm releases. Many current npm packages require a newer Node.js version and will not run on Node.js 12.

Upgrading Node.js

The upgrade steps depend on how you installed Node.js.

For a NodeSource install, write the new release line to the repository file and install the package again. The following commands move Node.js 24 to Node.js 26:

Terminal
NODE_MAJOR=26
echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_$NODE_MAJOR.x nodistro main" | sudo tee /etc/apt/sources.list.d/nodesource.list
sudo apt update
sudo apt install nodejs

APT upgrades the nodejs package in place. You do not need to remove the old version first. If your project uses packages with native addons, run npm rebuild in the project directory to compile them against the new Node.js version.

The Ubuntu repository stays on Node.js 12 for the whole life of Ubuntu 22.04. Running sudo apt upgrade will never move you to a newer release line. To get one, switch to NodeSource or nvm.

With nvm, install the new version and copy the global packages from the old one:

Terminal
nvm install 26 --reinstall-packages-from=24

The old version stays installed. Once your projects work on Node.js 26, make it the default with nvm alias default 26.

Troubleshooting

npm: command not found
The Ubuntu nodejs package does not include npm. Install it separately with sudo apt install npm. NodeSource and nvm install npm together with Node.js.

node: command not found after installing nvm
The installer adds nvm to your shell startup file. A terminal that was already open during the install has not read the new lines yet. Open a new terminal, or load nvm in the current one:

Terminal
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"

trying to overwrite '/usr/include/node/common.gypi'
The Ubuntu libnode-dev package is still installed. It ships the same file as the NodeSource nodejs package, so dpkg stops the installation. Remove the Ubuntu Node.js packages, then install from NodeSource again:

Terminal
sudo apt remove nodejs npm libnode-dev
sudo apt autoremove
sudo apt install nodejs

APT still installs Node.js 12 after adding NodeSource
APT has not loaded the NodeSource repository yet. Run sudo apt update, then check the candidate version with apt policy nodejs. If the candidate is still 12.22.9, check /etc/apt/sources.list.d/nodesource.list for typos.

Uninstalling Node.js

The uninstall method depends on how you installed Node.js.

NodeSource or Ubuntu repository:

Terminal
sudo apt remove nodejs npm
sudo apt autoremove

If you used NodeSource, also delete the repository file and the signing key:

Terminal
sudo rm /etc/apt/sources.list.d/nodesource.list /etc/apt/keyrings/nodesource.gpg
sudo apt update

nvm:

Terminal
nvm uninstall 22.23.5

To completely remove nvm, delete the ~/.nvm directory and remove the nvm lines from your ~/.bashrc or ~/.zshrc file.

Conclusion

NodeSource installs a supported Node.js release that you update with APT, like any other system package. nvm is the better choice when different projects need different Node.js versions on the same machine.

To keep a Node.js application running in the background, see our guide on managing Node.js processes with PM2 .

Linuxize Weekly Newsletter

A quick weekly roundup of new tutorials, news, and tips.

About the authors

Dejan Panovski

Dejan Panovski

Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 1000+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.

View author page