Querying the package list with apt-cache

Look Inside

Article from Issue 177/2015

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:


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.


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.

Figure 1: The stats command provides an overview of system packages.

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.

Figure 2: Use the policy command to configure the apt-get preference file.

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.

Figure 3: The output from unmet may indicate either the need to update a dependency list or the need for another repository to meet the dependencies.

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

Figure 4: The pkgnames command simply lists all available packages.

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.

Figure 5: Use the show command to read detailed information about one or more 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.

Figure 6: The madison command lists the different versions of a package in your repositories.

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.

Figure 7: Apt-cache can display both dependencies and reverse dependencies.

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.

Figure 8: Apt-cache's search command, showing the top results for a search on KDE.

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • apt vs. apt-get

    The apt command-line utility is a successor to the well known apt-get, offering simpler installation and maintenance for the DEB packages used with Debian, Ubuntu, Knoppix, and many other Linux distros.

  • Command Line: Apt-get

    Dependency tangles fall away with the Debian package system.

  • Ask Klaus

    Klaus Knopper answers your Linux questions.

  • Command Line – Pinning Sources

    Debian discourages the use of pinning to set preferences for package repositories, because the practice can have disastrous results. We take a closer look.

  • APT2 to Accelerate Debian Package Installation

    Debian and Ubuntu developer Julian Andres Klode has reported in his blog about the progress of his APT2 software, an alternative implementation of the Debian Advanced Packaging Tool (APT).

comments powered by Disqus
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.

Learn More