How to fix “Unable To Locate Package”? – (Fix with APT Sources)

Fixing : Unable To Locate Package

A lot of times when installing or trying to run a package, you get an error “Unable to locate package”. This is another common error one faces as a beginner, especially on Debian/Ubuntu-based systems. In this module, we are going to learn how to fix this issue step-by-step.

Steps to fix the “Unable to locate package” error in Linux

Let’s go over the steps to fix the error on our Linux systems today. We’ll be working on the error as per the apt package manager.

1. Analyzing Source Lists

Source lists are used to locate archives of the package distribution system in use on the system. This means that when we request a package for installation, the source lists are responsible for locating the same from its archives, hence it is following the common logic, they have a direct implication to the error. The man pages describe source lists as such:

The source list /etc/apt/sources.list and the files contained in /etc/apt/sources.list.d/ are designed to support any number of active sources and a variety of source media. The files list one source per line (one-line style) or contain multiline stanzas defining one or more sources per stanza (deb822 style), with the most preferred source listed first (in case a single version is available from more than one source). The information available from the configured sources is acquired by apt-get update (or by an equivalent command from another APT front-end).

Let us assume that we want to install the very popular port scanning tool nmap on a Debian machine, using apt which yields the following:

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

So apt couldn’t find the nmap package from its archives. We know that apt uses the source lists under /etc/apt/sources.list to locate packages. So open the same with your favorite text editor and with superuser privileges. Here we should find something like this :

# deb http://deb.debian.org/debian bullseye main
# deb-src http://deb.debian.org/debian bullseye main

Make sure that you have all of the official source repositories of your particular distribution and version. You can get a list of official mirror’s on the Distro’s official site.

2. Explaining Source Lists

Let’s analyze the first line line to understand what each component signifies :

# deb http://deb.debian.org/debian bullseye main
  • #: This signifies a comment, i.e, the following line is not to be considered.
  • deb: It signifies that this archive contains pre-compiled binaries in .deb format while deb-src contains the source code of packages which can be built and modified as desired. [Read on how to easily build packages from the source here]
  • http://deb.debian.org/debian: Next up, we have the repository’s URL from where the package manager downloads the package metadata and other information to learn about what packages are available and where to download them.
  • bullseye: This signifies the code name of our Debian/Ubuntu system and can be obtained with :
$ lsb_release -c
Codename:	bullseye
  • main: The package repository might be divided into many sub-sections like :
    • main: It contains officially supported open source software
    • restricted: It contains officially supported closed source software
    • universe: It contains open source software maintained by the community
    • multiverse: It contains unsupported, closed source, and patent-encumbered software

So as we can see both of our lines are commented out. Hence apt has no source repositories from where it can locate packages from. So, we uncomment both lines so that it looks like:

deb http://deb.debian.org/debian bullseye main
deb-src http://deb.debian.org/debian bullseye main

3. Updating APT Cache

Now that we have updated our source lists to include the desired mirrors, we can update the apt cache with:

$ sudo apt update

Subsequently, doing an apt install should fetch us our package :

$ sudo apt install nmap

Note : Sometimes, even including older mirrors or other release repositories in the /etc/apt/sources.list is also advised to fetch unavailable packages but they often cause conflicts during future upgrades. Hence, in such scenarios, it is advised to install the required packages from .deb files or remove/comment out the lines from /etc/apt/sources.list after installation of the required packages.

Conclusion

Hence, we learned how to fix this very common error. In case the error persists, we can refer to other mirrors but always heed to the other the above note. Besides, we can always build packages from source, install them from pre-compiled binaries, or use other package managers like snap, aptitude, etc.

On a final note, remember that this error is generally related to either missing/incorrect sources in the apt sources list OR you typed the name of the package wrong.