Of the Debian system management tools, apt-cache is perhaps one of the lesser known commands. Here, we look at some of the facilities that apt-cache provides. If you’re managing any Debian (or Debian-derived) systems, this is a command worth being aware of.
The apt-cache command is part of the apt package, and will (or should) be installed on all Debian systems. It examines the package cache on the system, but it does not make changes to the system (other than possibly updating the package cache), and thus most uses of apt-cache do not require the user to be root.
We’ll look here at the following commands provided by apt-cache:
dependsandrdependspolicysearchstats
apt-cache depends and apt-cache rdepends
Like most distributions, Debian has a concept of package dependencies whereby package A requires package B to be installed. Debian can also suggest or recommend additional packages which, whilst not essential, may enhance the functionality of a given package. The Debian GNU/Linux FAQ defines the exact meanings of depends, suggests, etc.
The normal package dependency may be examined with the apt-cache depends command:
$ apt-cache depends vim vim Depends: vim-common Depends: vim-runtime Depends: libacl1 Depends: libc6 [...] Suggests: vim-doc Suggests: vim-scripts
Above, we can see that the vim editor depends on the vim-common package (amongst others). We can also see the “suggested” packages as well.
The rdepends (“reverse dependencies”) command looks at this from the other perspective, and lists packages that, in turn, depend on the vim package. Whereas vim requires all the packages listed by the depends command in order to run, it doesn’t require any of the rdepends packages:
$ apt-cache rdepends vim
vim
Reverse Depends:
byobu
vim-athena
vim-gtk
vim-gtk3
vim-nox
|vim-vimoutliner
vim-athena
vim-gtk
vim-gtk3
vim-nox
[...]
So, if you install vim-vimoutliner, you’ll need to have vim installed as well. By default, Debian will automatically install required dependencies when installing a package.
The output here is slightly misleading in that apt-cache includes packages that suggest vim, not just that depend on vim, as we can see here:
$ apt-cache depends byobu byobu |Depends: debconf [...] Breaks: <byobu-extras> [...] Suggests: vim
Some packages are incompatible with others, and here we can see that the byobu package “breaks” byobu-extras. In this particular case, the-extraspackage is now provided by byobu itself and thus should not be installed as a separate package.
apt-cache policy
The apt-cache policy command shows which versions of a package apt knows about, which version is installed (if any), and which version would be installed if an install or update were done. It takes into account any package pinnings defined in the /etc/apt/preferences file (or /etc/apt/preferences.d/* files):
$ apt-cache policy chromium chromium: Installed: 57.0.2987.98-1~deb8u1 Candidate: 61.0.3163.100-1~deb9u1 Version table: 61.0.3163.100-1~deb9u1 500 500 http://security.debian.org stretch/updates/main amd64 Packages *** 57.0.2987.98-1~deb8u1 500 100 /var/lib/dpkg/status
Here we can see that an outdated version of the Chromium browser is installed. Looking at the major version numbers, we can see at the top that version 57 is installed, and that the system knows about, and would install, version 61 if we were to run an update or reinstall.
Further down, we can see that version 61 is a security release. The three asterisks mark the currently installed version, which is not found in any currently defined repository.
When multiple versions of a package are available for installation, as above, Debian allows packages to be “pinned” to a given version (or source). For example, we can ensure that Chromium remains at the current version by putting the following in the /etc/apt/preferences file:
Package: chromium Pin: version 57.0.2987.98-1~deb8u1 Pin-Priority: 600
We can use apt-cache policy to confirm that the preferences file is having the desired effect:
$ apt-cache policy chromium
chromium:
Installed: 57.0.2987.98-1~deb8u1
Candidate: 57.0.2987.98-1~deb8u1
Version table:
61.0.3163.100-1~deb9u1 500
500 http://security.debian.org stretch/updates/main amd64 Packages
*** 57.0.2987.98-1~deb8u1 600
100 /var/lib/dpkg/status
apt-cache search
The apt-cache search command will search package names and, by default, descriptions for the regular expression passed on the command line. Here we search for all of the words ‘directory’, ‘tree’ and ‘color’ or ‘colour’:
$ apt-cache search directory tree colou?r dirdiff - Display and merge changes between two directory trees libgtkextra-3.0 - useful set of widgets for creating GUI's for GTK+ knews - Graphical threaded news reader texlive-latex-extra - TeX Live: LaTeX additional packages tree - displays an indented directory tree, in color vfu - A versatile text-based filemanager basilisk2 - 68k Macintosh emulator
apt-cache stats
The apt-cache search command simply lists package cache stats. Here’s beginning of the output:
$ apt-cache stats Total package names: 67236 (1,345 k) Total package structures: 67263 (2,960 k) Normal packages: 51809 Pure virtual packages: 536 Single virtual packages: 5749 Mixed virtual packages: 591 Missing: 8578 Total distinct versions: 54280 (4,342 k) Total distinct descriptions: 106424 (2,554 k) Total dependencies: 343831/91632 (8,343 k) Total ver/file relations: 57486 (1,380 k) Total Desc/File relations: 40888 (981 k) Total Provides mappings: 9733 (234 k) Total globbed strings: 152535 (3,278 k) Total slack space: 22.1 k Total space accounted for: 25.8 M [...]
Conclusion
From Debian Stretch (9), some of the functionality of apt-cache is also provided by the apt command, so for example:
$ apt policy vim
vim:
Installed: 2:8.0.0197-4
Candidate: 2:8.0.0197-4+deb9u1
Version table:
2:8.0.0197-4+deb9u1 500
500 http://mirror.tiger-computing.wbp/debian stretch/main amd64 Packages
*** 2:8.0.0197-4 100
100 /var/lib/dpkg/status
As always, the man page holds the details of all the commands and switches. For me, the apt-cache policy command is probably the most useful apt-cache command, but the others have their place too.
Could This Linux Tip Be Improved?
Let us know in the comments below.


