This brief tutorial explains a few different ways to find the package that provides a specific file in Linux. This could be useful when you manually compile and install a package. When you're compiling a package from source, there are chances that you may receive an error something like "No rule to make target '<somefile>', needed by '<somefile>'. Stop.". You may not know exactly which packages provides the missing file. In such cases, you can easily find out the packages that provides those missing files and install them in your Linux box as described in this guide.
Find The Package That Provides A Specific File In Linux
Arch Linux, Antergos, Manjaro Linux:
On Arch-based, there is a small command line tool called pkgfile, which is used to search files from packages.
Pkgfile comes pre-installed with Arch Linux. If it's not, you can install using the following command:
$ sudo pacman -S pkgfile
Then, run the following command to sync with pkgfile database:
$ sudo pkgfile -u
Now, you can find the package that provides a specific file, say for example alisp.h, using command:
$ pkgfile alisp.h extra/alsa-lib
As you see in the above output, alsa-lib package provides alisp.h file. And, the package is available from extra repository. You can now install this package as shown below.
$ sudo pacman -S alsa-lib
To list all files provided by the alsa-lib package, run:
$ pkgfile -l alsa-lib
Fedora, RHEL, CentOS, AlmaLinux, Rocky Linux:
In RPM-based systems such as Fedora, RHEL and its clones like CentOS, AlmaLinux and Rocky Linux, you can find the package that owning a particular file, using the following command:
$ dnf provides '*filename'
$ yum whatprovides '*filename'
If the file is already available in your system, say for example
/bin/ls, you can then find the package that owns the file using command:
# rpm -qf /bin/ls coreutils-8.22-18.el7.x86_64
You can also use the repoquery command as follows:
$ repoquery -f /bin/ls
If repoquery command is not available in your system, install yum-utils package.
$ sudo yum install yum-utils
$ sudo dnf install yum-utils
Debian, Ubuntu, Linux Mint:
In any DEB-based systems, you can find the package that provides a certain file using apt-file tool.
Install apt-file as shown below if it's not installed already:
$ sudo apt-get install apt-file
If you just installed apt-file, the system-wide cache might be empty. You need 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.
Let us update the database cache using command:
$ sudo apt-file update
And, then search for the packages that contains a specific file, say alisp.h, with command:
$ apt-file find alisp.h
$ apt-file search alisp.h
Sample would be:
Well, libasound2-dev it is! You can install this package using command:
$ sudo apt-get install libasound2-dev
If you already have the file, and just wanted to know which package it belongs to, you can use
dpkg command as shown below.
$ dpkg -S $(which alisp.h)
$ dpkg -S `which alisp.h`
If you know the full path of the file, say for example
/bin/ls, you can search for the packages it belongs to using the following command:
$ dpkg -S /bin/ls coreutils: /bin/ls
SUSE / openSUSE:
On SUSE and openSUSE, run the following command to find out which package a certain file belongs to.
$ zypper wp alisp.h
$ zypper se --provides --match-exact alisp.h
And, that's all. Hope this helps. If you know any other methods to find the package that contains a particular file, feel free to let us know in the comment section below. I will check and update the guide accordingly.
On Arch based distros, pacman -Qo will also show the package the file is from, without the need to install an extra tool.