How to fix Ubuntu error: Unable to locate package npm

Learn how to fix the error: Unable to locate package npm in Ubuntu computers

Posted on May 13, 2022


When trying to install NPM on Ubuntu computers, you may get an error saying Unable to locate package npm.

The example error is as shown below:

$ sudo apt-get install npm
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
E: Unable to locate package npm

There are two methods you can try to fix the error:

  • Update your Ubuntu package list with apt-get update
  • Fetch nodejs from the NodeSource APT repository and use the bundled npm package

Let’s see how to perform the methods above next.

Update Ubuntu package list

If you’re installing a fresh Ubuntu operating system, then you need to run apt-get update to fetch the latest versions of your packages.

You can try running the commands below:

sudo apt-get update
sudo apt-get install npm

If you still see the error when installing npm, then you need to perform an apt-get upgrade as well:

sudo apt-get upgrade
sudo apt-get install npm

Now, Ubuntu should be able to locate the package and ask you to allow the installation:

$ sudo apt-get install npm
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done

After this operation, 824 MB of additional disk space will be used.
Do you want to continue? [Y/n]

Once the installation is finished, you should be able to use NPM to install node packages.

Install nodejs from the NodeSource binary distributions

Most of the time, the version of NodeJS and NPM that comes from the Ubuntu repository is behind the latest stable version released by the developers.

To install a specific version of NodeJS and NPM, you can use the repository provided by NodeSource.

For example, you can install the latest LTS version of NodeJS, which is v16.x with the commands below:

# Using Ubuntu
curl -fsSL https://deb.nodesource.com/setup_16.x | sudo -E bash -
sudo apt-get install -y nodejs

# Using Debian, as root
curl -fsSL https://deb.nodesource.com/setup_16.x | bash -
apt-get install -y nodejs

You only need to change the setup_x.x parameter by the end of the repository URL.

By using NodeSource repositories, the npm package will be bundled together with the nodejs package.

You don’t need to install npm manually with apt-get install npm.

Once the installation is finished, run npm -v to get the version of NPM installed on your computer:

$ npm -v
8.5.5

And that’s how you resolve the error: Unable to locate package npm in Ubuntu or Debian-based OS.

I hope this tutorial has been useful for you. 👍

Related articles:

Level up your programming skills

I'm sending out an occasional email with the latest programming tutorials. Drop your email in the box below and I'll send new stuff straight into your inbox!

No spam. Unsubscribe anytime.