E: Unable to locate package npm Ubuntu error [Solved]

avatar
Borislav Hadzhiev

Last updated: Apr 4, 2024
3 min

banner

# E: Unable to locate package npm Ubuntu error [Solved]

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.

shell
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

# Updating the package information on your Ubuntu machine

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.

shell
sudo apt-get update

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.

shell
sudo apt-get install -y build-essential

install build essential

Try installing the npm package after updating your package information.

shell
sudo apt-get install npm

sudo apt get install npm

If the error persists, try to run the sudo apt-get upgrade command before installing npm.

shell
sudo apt-get upgrade

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.

shell
sudo apt-get install npm

sudo apt get install npm

If the error persists, install Node.js from the NodeSource repository.

# Installing Node.js and NPM from the NodeSource repository

First, make sure you have curl installed by running the following commands.

shell
sudo apt update sudo apt install curl

make sure curl is installed

The NodeSource repository can be used to install different Node.js versions.

To install Node.js version 19.X, run the following command.

shell
# 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

installing nodesource version 19

You can install Node.js version 18.X (the long-term supported version) by running the following command.

shell
# 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.

shell
# 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.

shell
node --version npm --version

verify nodejs npm installed

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:

shell
sudo apt-get install -y build-essential

install build essential

If you need to uninstall Node.js and npm, follow the instructions in this section of the NodeSource repository.

# Using NVM to manage your Node.js and npm versions

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.

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

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.