Debian 9, code-named ‘Stretch’, has a whopping repository of 68,000+ packages. Whether it be games, fonts, editors, databases, or development tools, the chance is you will find it there. The key, however, is to find it. Unless you know the exact package name, finding a particular package in such a large repository is like looking for a needle in a haystack. Sometimes, the package name is different from the name of the program. For instance, iostat, a useful program for reporting CPU and disk I/O statistics, is packaged under the name sysstat. Other times, you know neither the package name nor the command name. Instead, you only know its function, what it is supposed to do.
To look for packages, several methods exist with varying levels of power and usability. The Debian website has a list of all Debian packages. So, you can use your browser’s search function, or download the list as a compressed text file and use grep. Either way, much manual work is required to filter the list. The good news is that powerful automated tools are available to speed up filtering.
Debian provides both graphical and command-line package managers to search and filter packages. GUI-based tools, such as Synaptic, generally excel in usability. However, not all machines, especially production servers, have the graphical desktop installed. For those machines, Synaptic is simply not an option. In this post, we focus on command-line tools to search for Debian packages.
Background information
Debian system administrators define the sources of packages in the file /etc/apt/sources.list. Among other things, the file specifies that only packages from a particular Debian release (e.g., Stretch vs Buster), and from certain component categories (e.g., main, contrib, and non-free) can be downloaded. Metadata of all eligible packages are downloaded to the local package cache, via the command apt-get update.
Upstream package maintainers may add and remove packages at any time. To make sure that your local package cache has the latest metadata information, you should update the cache (‘apt-get update’) before you do a search.
Now that you are ready to search, you can do so as a regular user. There is no need to become root to execute a search. This is because a search merely makes queries, not changes, to the package cache. In addition, the machine does not even need to be on-line because the search commands are only accessing the local cache, not remote repositories.
The scope of a search is restricted to what is specified in the sources.list file. For instance, if the file permits only main and contrib component packages, then your searches will not return non-free packages.
To summarize:
- Before you execute a search, run ‘apt-get update’ to refresh the package cache.
- You may run a search command as a regular user (no root privilege required).
- No Internet connection is required to run a search command.
- Search results are restricted to the scope defined by the sources.list file.
apt-cache
If you know the package name, albeit partially, you can pass the partial name as a regular expression argument to apt-cache. For instance, you know that the package name is a variation of the theme dos to unix, such as dostounix, dos2unix, dos-to-unix, etc. The search command and its output are as follows:
dos2unix – convert text file line endings between CRLF and LF
Notes:
- –names-only (or -n in short) specifies that only package names, not descriptions, are matched. (The default is both name and description.)
- dos.+unix is a regular expression which matches the package name dos2unix. Please refer to regex(7) for syntax help.
What do you do if you only know what the package does, i.e., its function, but not its name? In this case, you are looking for a program to convert text files from the DOS format to UNIX. The search command to use and its output are the following:
dos2unix – convert text file line endings between CRLF and LF
flip – convert text file line endings between Unix and DOS formats
fondu – convert between Mac and UNIX font formats
tofrodos – Converts DOS <–> Unix text files, alias tofromdos
By default, apt-cache tries to match the search keywords against both the package name and the package’s long description. The hope here is that the functional keywords will match the words that the package maintainer uses in its long description. Note that multiple keywords (convert, dos, unix) are AND-ed together.
apt
The apt command is a newer package management tool which merges the basic functionalities of apt-get and apt-cache. It is designed to be used interactively, rather than in a script. For instance, apt output is colorized to improve readability. Esthetics aside, the apt and apt-cache commands share a similar syntax including command options.
The example below searches for a package by matching the regular expression dos.+unix, against package names only (by default, against both name and description).
Sorting… Done
Full Text Search… Done
dos2unix/stable,now 7.3.4-3 amd64 [installed]
convert text file line endings between CRLF and LF
apt-file
Although apt-cache (and apt) can help you look for a package by matching functional keywords, the search is often hit and miss. Success depends too much on how the package maintainer writes the package’s long description. In reality, though, you may know additional information about the package, such as the name of the command or a file that is included in the package. The tool to use then is apt-file.
The apt-file command is not installed by default on a Debian system. To install, run as root:
Before you run apt-file for the first time, update the package cache by updating the local package cache (‘apt-get update’).
Suppose you want to find the package which contains the iostat command. The command to use and the abbreviated output are as follows:
… <12 lines snipped> …
linux-manual-4.9: /usr/share/man/man9/rpc_count_iostats_metrics.9.gz
linux-manual-4.9: /usr/share/man/man9/rpc_free_iostats.9.gz
manpages-fr-extra: /usr/share/man/fr/man8/nfsiostat.8.gz
munin-plugins-extra: /usr/share/munin/plugins/iostat
munin-plugins-extra: /usr/share/munin/plugins/iostat_ios
nagios-plugin-check-multi: /usr/share/doc/nagios-plugin-check-multi/examples/nagiostats.cmd
nfs-common: /usr/sbin/nfsiostat
nfs-common: /usr/share/man/man8/nfsiostat.8.gz
nixstatsagent: /usr/lib/python2.7/dist-packages/nixstatsagent/plugins/iostat.py
sysstat: /usr/bin/cifsiostat
sysstat: /usr/bin/iostat
sysstat: /usr/share/man/man1/cifsiostat.1.gz
sysstat: /usr/share/man/man1/iostat.1.gz
zsh-common: /usr/share/zsh/functions/Completion/Unix/_iostat
apt-file works by matching the argument iostat as a substring against the pathnames of all files in a package. The above search returns 46 hits, i.e., a total of 46 files in the local cache contain iostat in their pathnames, which is a lot to manually examine.
To narrow down the search, turn the argument into a regular expression by using the -x flag.
munin-plugins-extra: /usr/share/munin/plugins/iostat
sysstat: /usr/bin/iostat
The regular expression, /iostat$, only matches pathnames that end exactly with iostat. For regular expression syntax, please refer to perlreref(1).
The new output has only 2 hits, and you can quickly read off the name of the package (sysstat) that contains the iostat command.
Conclusion
Linux distributions in general and Debian, in particular, are known for their enormous official package repositories. But, finding the one you want among tens of thousands of packages can be quite a challenge. Rest assured that you don’t need to know the exact package name. The apt-cache and apt commands can filter packages based on keywords in the package description. Alternatively, if you know the name of the program binary file, the apt-file command can use it to identify packages that contain a file with the same name.