apt – install the package that contains a specific file

Sometimes a command that you execute complains about a dependency being missing on the system. From the file name in the error message it is often not possible to know exactly which package needs to be installed to resolve the dependency.

Similar to yum – install package that contains a specific file on RHEL-based distros, Debian / Ubuntu also offer a utility to search the package repository for packages that would install certain files. This utility is not installed by default and is called “apt-file”.

To install “apt-file” execute the following command.

$ sudo apt-get install apt-file

The “apt-file” utility uses the index provided by the different repositories to search. This index file needs to be downloaded before the first search can be started and should probably be refreshed from time to time to have accurate results. This is the “update” command used to perform this update of the indexes.

$ apt-file update
...
Downloading Index http://security.ubuntu.com/ubuntu/dists/xenial-security/Contents-amd64.diff/Index:
No Index available.
Downloading complete file http://security.ubuntu.com/ubuntu/dists/xenial-security/Contents-amd64.gz
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 4409k  100 4409k    0     0  1893k      0  0:00:02  0:00:02 --:--:-- 1893k
Ignoring source without Contents File:
  http://archive.canonical.com/ubuntu/dists/xenial/Contents-amd64.gz

Note that not all repositories provide such an index file. While updating the index of apt-file, the repositories that do not provide such an index will be stated.

With the freshly updated index, the first search can be performed. Lets find out the package that installs hkt, a check utility for OpenPGP/GnuPG keys. The find command is used to perform the search through the indexes. When the “bin/” is added to find a program, the results will match the path the program is installed to.

$ apt-file find "bin/hkt"
hopenpgp-tools: /usr/bin/hkt

In the example above, the match will find the program if it is located in any “bin/” or “sbin/” directory allowing you to find any executable program that installs to a default location for executables.

$ dpkg -L hopenpgp-tools
/.
/usr
/usr/share
/usr/share/lintian
/usr/share/lintian/overrides
/usr/share/lintian/overrides/hopenpgp-tools
/usr/share/doc
/usr/share/doc/hopenpgp-tools
/usr/share/doc/hopenpgp-tools/copyright
/usr/share/doc/hopenpgp-tools/changelog.Debian.gz
/usr/bin
/usr/bin/hot
/usr/bin/hkt
/usr/bin/hokey

The command above shows how you can then use “dpkg” to show the rest of the files that would be installed with the package.


Read more of my posts on my blog at https://blog.tinned-software.net/.

This entry was posted in Linux Administration, Security and tagged , , , , , , , . Bookmark the permalink.