How to fix Ubuntu error: Unable to locate package npm

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. 👍

Take your skills to the next level ⚡️

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

No spam. Unsubscribe anytime.