Last updated: Apr 4, 2024
Reading time·3 min
The error "E: Unable to locate package npm" occurs when the Debian repository
on your Ubuntu machine cannot find the npm
package.
You can solve the error by updating your Ubuntu package information with the
sudo apt update
command or by installing Node.js from NodeSource.
Here is the complete error message.
sudo apt-get install npm sudo apt-get install nodejs Reading package lists... Done Building dependency tree Reading state information... Done E: Unable to locate package npm E: Unable to locate package nodejs
The first thing you should try is to update the package information on your
Ubuntu machine before issuing the sudo apt-get install npm
command.
sudo apt-get update
The sudo apt-get update
command fetches the latest version of the package list
from the Debian repository and any third-party repositories you have configured.
Make sure you have the build-essential
packages installed as well.
These are meta-packages that are necessary for compiling software.
sudo apt-get install -y build-essential
Try installing the npm
package after updating your package information.
sudo apt-get install npm
If the error persists, try to run the sudo apt-get upgrade
command before
installing npm
.
sudo apt-get upgrade
The sudo apt-get upgrade
command downloads and installs the updates for
outdated packages.
Rerun the sudo apt-get install npm
after running the upgrade
command.
sudo apt-get install npm
If the error persists, install Node.js from the NodeSource repository.
First, make sure you have curl
installed by running the following commands.
sudo apt update sudo apt install curl
The NodeSource repository can be used to install different Node.js versions.
To install Node.js version 19.X
, run the following command.
# 19.X - Using Ubuntu curl -fsSL https://deb.nodesource.com/setup_19.x | sudo -E bash - &&\ sudo apt-get install -y nodejs # 19.X - Using Debian, as root curl -fsSL https://deb.nodesource.com/setup_19.x | bash - &&\ apt-get install -y nodejs
You can install Node.js version 18.X
(the long-term supported version) by
running the following command.
# 18.X - Using Ubuntu curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash - &&\ sudo apt-get install -y nodejs # 18.X Using Debian, as root curl -fsSL https://deb.nodesource.com/setup_18.x | bash - &&\ apt-get install -y nodejs
If you need to install version 16.X
, use the following command instead.
# 16.X - Using Ubuntu curl -fsSL https://deb.nodesource.com/setup_16.x | sudo -E bash - &&\ sudo apt-get install -y nodejs # 16.X Using Debian, as root curl -fsSL https://deb.nodesource.com/setup_16.x | bash - &&\ apt-get install -y nodejs
You can verify that Node.js and NPM are installed by running the following commands.
node --version npm --version
The
NodeSource
repository installs both Node.js and npm
with a single command.
To compile and install native addons from npm
, you might also need to install
build tools:
sudo apt-get install -y build-essential
If you need to uninstall Node.js and npm
, follow the instructions in
this section of the NodeSource repository.
An alternative approach is to use the
nvm package to manage
your Node.js and npm
versions.
The nvm
(Node version manager) package can be used to switch between different
Node.js versions and allows you to use multiple versions without any clashes.
You can install nvm
with a single curl
command.
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh | bash
Alternatively, you can follow the more detailed instructions in this section of the NVM repository.