#41 - CLI Monday: apt-file and yum provides

Links, Code, and Transcript

In this episode, we are going to look at using apt-file on Ubuntu, and yum provides on CentOS, to see which package provides that file you are missing.

Say for example, that you are looking for a command, or maybe a library to compile against, but it is missing on your system? You know what the file path should be, but you are not sure which package provides that file. Well, I thought we would look at ways to solve the problem on both Ubuntu and CentOS. I know this is not something you are going to be using everyday, but if you run into a situation where you need to install a package because of missing files, this episode will likely help you out.

Our first example is going to take place on an Ubuntu 14.04 machine. Lets start with a deliberately simple use case, say that we have a dependency on /etc/apache2/apache2.conf. As you can see, it is missing on our system, but lets say there was some dependency on it, and you wanted to get it installed? How do we know which package provides it? Well, you likely already know which package provides it, but we can use the apt-file command to tell us for sure.

vagrant@e41:~$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 14.04.1 LTS
Release:    14.04
Codename:   trusty
vagrant@e41:~$ ls /etc/apache2/apache2.conf
ls: cannot access /etc/apache2/apache2.conf: No such file or directory

Unfortunately, on Ubuntu this command is not installed by default, so lets install it now, by running sudo apt-get install apt-file. Then once it is installed, down here you will notice that it says the system cache is empty. We can run, apt-file update as root, to build a system wide cache, or we can run the command as a regular user, and it will create the cache in our users home directory. Basically, what this cache does it create a searchable mapping, between packages, and the files those packages provide. In this case, lets opt for a system wide cache, just in case there are multiple users that will want to use this apt-file command. Lets run, sudo apt-file update.

vagrant@e41:~$ sudo apt-get install apt-file
The system-wide cache is empty. You may want to run 'apt-file update'
as root to update the cache. You can also run 'apt-file update' as
normal user to use a cache in the user's home directory.
vagrant@e41:~$ sudo apt-file update
Downloading complete file http://archive.ubuntu.com/ubuntu/dists/trusty/Contents-amd64.gz
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 28.0M  100 28.0M    0     0  1904k      0  0:00:15  0:00:15 --:--:-- 3898k
File is up-to-date.

Okay, so at this point, we have apt-file installed, and the cache has been configured. Lets head back to our example of looking for the /etc/apache2/apache2.conf file. As you can see, it does not exist, so lets ask apt-file if it knows what package provides it, by running, apt-file search /etc/apache2/apache2.conf. As you likely guessed, the apache2 package provides the file we are looking for. So, what are we really trying to solve here? Well, the typical scenario would be that you are missing some type of system library, say for example that you are trying to compile something for source, and you have some type of missing header. It can be really frustrating to try and figure out what package provide the files you are looking for. Likely you will be searching Google for the path names or error messages.

vagrant@e41:~$ ls /etc/apache2/apache2.conf
ls: cannot access /etc/apache2/apache2.conf: No such file or directory
vagrant@e41:~$ apt-file search /etc/apache2/apache2.conf
apache2: /etc/apache2/apache2.conf

Okay, so now that we have solved our example problem on Ubuntu 14.04, lets review how this works on CentOS 7. On Redhat Enterprise Linux and all of its derivatives, you can use the yum command to find files that you would like to install. Lets try something similar to our previous example, of a missing Apache configuration file. On CentOS, the configuration file is located in /etc/httpd/conf/httpd.conf, and as you can see it is missing on this example system too. The one nice thing about Redhat is that you do not need to install additional software, or build any package to file cache for the specific function. You can actually just use the yum command to find what package this files belongs to, by running, yum provides /etc/httpd/conf/httpd.conf, and as you can see there are two packages that contain our file. These are actually the same package, just with different version numbers. I should mention that yum provides can also be written as yum whatprovides, these two commands are aliased together.

[vagrant@e41 ~]$ cat /etc/redhat-release 
CentOS Linux release 7.0.1406 (Core) 
[vagrant@e41 ~]$ ls /etc/httpd/conf/httpd.conf
ls: cannot access /etc/httpd/conf/httpd.conf: No such file or directory
[vagrant@e41 ~]$ yum provides /etc/httpd/conf/httpd.conf
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirror.its.sfu.ca
 * extras: mirror.its.sfu.ca
 * updates: centos.mirror.nexicom.net

httpd-2.4.6-17.el7.centos.1.x86_64 : Apache HTTP Server
Repo        : base
Matched from:
Filename    : /etc/httpd/conf/httpd.conf

httpd-2.4.6-18.el7.centos.x86_64 : Apache HTTP Server
Repo        : updates
Matched from:
Filename    : /etc/httpd/conf/httpd.conf

That is pretty much it for this episode, and as I said earlier, this is not something you are likely going to be using everyday, but if you run into a situation where you need to install a missing library, or system utility, these commands will help you solve the problem.

[vagrant@e41 ~]$ yum provides lscgroup
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: www.muug.mb.ca
 * extras: www.muug.mb.ca
 * updates: mirror.its.sfu.ca
libcgroup-tools-0.41-6.el7.x86_64 : Command-line utility programs, services and daemons for libcgroup
Repo        : base
Matched from:
Filename    : /usr/bin/lscgroup
  • Published
  • Duration
    4 minutes
  • Download
    MP4 or WebM
You may also like...