Querying the package list with apt-cache
Look Inside
We take a look at apt-cache, an essential utility for command line-based package management.
Debian-based systems have several dozen utilities for managing packages from the command line. After apt-get
and dpkg
, the most important is apt-cache
[1]. Apt-cache queries the packages in your repositories, providing the information you want before you install. If you want to know a package's statistics and dependencies, which package contains an application, or whether a package exists at all, then apt-cache is an essential utility for command line-based package management.
The cache referred to in the name is the list of information about all packages available in the Debian package management system – which means those installed from tarballs are excluded. This cache is based on the sources contained in /etc/apt/source.lists
, as well as some files in the /etc/apt/sources.lists.d
directory.
The main part of the cache, which contains information about the current state of each package, is stored in /var/lib/apt/lists
. Information about packages in transition, such as those held back during installation, is stored in /var/lib/apt/lists/partial/
. As you might guess from the locations, regular users can query apt-cache, but any alteration or updating of the cache requires the root account.
Apt-cache has a similar structure to apt-get itself:
apt-cache OPTIONS COMMAND PACKAGE/STRING
In some cases, apt-cache can be completed only by a command. Adding options or package names or strings provides more precise or detailed information, either about the package management system in general or the packages listed. Packages or strings can be entered in a space-separated list, just as with apt-get.
Options
Apt-cache's commands are versatile enough that few options are needed. However, apt-cache does include some options that help limit results. For example, -i
or --important
limits results to only important dependencies. More than half a dozen other options offer additional ways to limit the dependencies in results, including --no-recommends
, --no-suggests
, --no-replaces
, and --no-enhances
. There is even a --no-depends
option and a --names-only
option that omits package descriptions. However, unless you are dealing with a system with which you are very familiar, using any of these options involves the risk of missing essential information. Usually, you are probably better off getting the most complete package information available by specifying -f
or --full
.
Perhaps the most useful is --installed
, which limits the results of commands to installed packages.
System Overviews
One purpose of apt-cache is to provide an overview of package management. The stats
command (Figure 1) gives a summary of the cache on your system, beginning with the total number of packages, as well as the number of virtual packages. That is, it lists packages for a general function rather than a specific application, the number of virtual packages that can be fulfilled by only one package, and the number of packages that are both virtual and a specific package. The command also lists the number of missing packages, usually those listed somewhere as a dependency but not actually available.
The information summarized by stats
can also be presented in graphic form using the dotty
command to display it in Graphviz [2] or xvcg
to display it with VCG [3]. However, with the thousands of packages on a typical modern system, the result in both cases is a large graph that is too complex to be useful. If dotty
or xvcg
sounds useful to you, you should specify a small list of packages so that you can easily see the relationships between them. You can save the graphs by piping their data to a file.
Other high-level views of package management can assist in troubleshooting and configuration. For example, the policy
command (Figure 2) shows the structures of the repositories in /etc/apt/sources.list
and can be used to help configure the apt-get preference file, etc/apt/preferences
[4]. The policy
command lists the language used by the repository, as well as the sections of the repository, such as unstable
or sid
, which are different names for the same repository.
The policy
command also shows the priority assigned to each repository, which is used to decide the source to use if two repositories offer the same version of a package. For example, an already installed version is given a priority of 100 and kept in preference to reinstalling. At the end of its output, policy also lists versions that are pinned – that is, set in the preference file to be used rather than any other version.
Still another overview is available using unmet
(Figure 3), which lists unmet dependencies. Unmet dependencies are often created when a package is replaced by a newer version or a different application, but some packages have failed to update their dependency lists. Often, these unmet dependencies do not affect the running of a Debian-based system, but, when they do, apt-cache lets you see the problem without re-running apt-get to receive the same information. The list can be surprisingly large, considering Debian's reputation for good-quality packages, so you might want to use apt-cache unmet | less
so you can scroll through the results.
Finally, pkgnames
provides the simplest overview of all: a list of packages in alphabetical order, arranged one package per line. This command can be useful when you are establishing that a package is available, but the volume of packages today may mean that pkgnames should be used only as a last resort when the search command fails you (Figure 4).
Finding Package Information
Although apt-cache can give general information, most often it is used for information about specific packages. For detailed information about packages, the most useful command is show
. The show
command produces the shell equivalent of the information given for each package on the Debian website, including such information as dependencies and recommended packages, as well as their homepages and file names (Figure 5). If anything, they give even more information than the web pages, although the web pages are used by many to find the same information. The showsrc
command gives similar information for source packages.
If your specific need is to see which versions of a package are available, you can use the madison
command (Figure 6). You can get the same functionality outside of apt-cache by running the madison-light
application.
When a package has unmet dependencies, you can use unmet
, but specify the package. Conversely, you may prefer to view the full list of the other packages that a package needs to run using depends
(Figure 7). At times, you might want to check whether you have a conflicts between packages without running apt-get by running rdepends
– short for reverse dependencies – and referring to the packages that must be absent for a particular package to run.
However, the most commonly used apt-cache command is search
(Figure 8). Search requires a string rather than a package name and, by default, reports back results in both package names and package descriptions. Because the short description for each result is also displayed, search is the command of choice for checking whether a distribution packages an application or for finding the exact package name. The command is equivalent to the search page on the Debian web site, except that it goes one better by allowing the use of regular expressions in the search.
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
Thousands of Linux Servers Infected with Stealth Malware Since 2021
Perfctl is capable of remaining undetected, which makes it dangerous and hard to mitigate.
-
Halcyon Creates Anti-Ransomware Protection for Linux
As more Linux systems are targeted by ransomware, Halcyon is stepping up its protection.
-
Valve and Arch Linux Announce Collaboration
Valve and Arch have come together for two projects that will have a serious impact on the Linux distribution.
-
Hacker Successfully Runs Linux on a CPU from the Early ‘70s
From the office of "Look what I can do," Dmitry Grinberg was able to get Linux running on a processor that was created in 1971.
-
OSI and LPI Form Strategic Alliance
With a goal of strengthening Linux and open source communities, this new alliance aims to nurture the growth of more highly skilled professionals.
-
Fedora 41 Beta Available with Some Interesting Additions
If you're a Fedora fan, you'll be excited to hear the beta version of the latest release is now available for testing and includes plenty of updates.
-
AlmaLinux Unveils New Hardware Certification Process
The AlmaLinux Hardware Certification Program run by the Certification Special Interest Group (SIG) aims to ensure seamless compatibility between AlmaLinux and a wide range of hardware configurations.
-
Wind River Introduces eLxr Pro Linux Solution
eLxr Pro offers an end-to-end Linux solution backed by expert commercial support.
-
Juno Tab 3 Launches with Ubuntu 24.04
Anyone looking for a full-blown Linux tablet need look no further. Juno has released the Tab 3.
-
New KDE Slimbook Plasma Available for Preorder
Powered by an AMD Ryzen CPU, the latest KDE Slimbook laptop is powerful enough for local AI tasks.