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

Fixing : Unable To Locate Package

If you’re having trouble locating a package on your Linux system, know it’s a frequently occurring problem for those new to package installations or executions, especially on Debian/Ubuntu-based systems. Nonetheless, there’s no need to be concerned since we’ve got you covered. This article will provide a detailed walkthrough on resolving this problem using the APT package manager. To begin, we’ll review the source lists and the components that make them up. Afterward, we’ll update the APT cache and install the package you want. So, without further ado, let’s tackle this error head-on and fix it.

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

Let’s review 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 follows the common logic, and they have a direct implication for 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

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

2. Explaining Source Lists

Let’s analyze the first 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 that 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:
    • 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 this:

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 the following:

$ 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

In summary, encountering the “Unable to locate package” error is a typical challenge for those new to installing packages on Debian/Ubuntu-based systems. Nevertheless, as demonstrated in this write-up, resolving this issue is a straightforward process. We can locate and install the necessary packages by examining and upgrading the source lists and refreshing the apt cache. Confirming that we have added all the official source repositories of our specific distribution and version is crucial to prevent any conflicts during future upgrades. Adhering to these uncomplicated steps can save considerable time and annoyance and improve our proficiency in package management on our Linux systems.

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 package’s name wrong.