Ubuntu

Find Which Package Contains Specific File on Ubuntu 20.04 LTS

Ubuntu has a lot of packages in the official package repository. It is really hard to find the package that you need to install unless you know the exact package name of the software/tool you’re trying to install. It becomes even harder if you want to find out which package to install to get a specific file (i.e. /etc/apache2/apache2.conf) or executable (i.e. /usr/bin/netstat) on Ubuntu.

In this article, I am going to show you how to find the exact name of the package which provides a specific file/executable on Ubuntu 20.04 LTS. So, let’s get started.

Installing apt-file:

You can easily find the name of the package which provides a specific file/executable on Ubuntu using apt-file. apt-file is available in the official package repository of Ubuntu 20.04 LTS. So, it’s very easy to install.

First, update the APT package repository cache with the following command:

$ sudo apt update

Now, install apt-file with the following command:

$ sudo apt install apt-file

To confirm the installation, press Y and then press <Enter>.

apt-file should be installed.

Now, apt-file command should be available on your Ubuntu 20.04 LTS machine as you can see in the screenshot below.

$ whereis apt-file

Updating apt-file Package Cache Database:

Just like the APT package manager, apt-file package cache database also needs to be up to date.

You can update the apt-file package cache database with the following command:

$ sudo apt-file update

apt-file package cache is being updated.

At this point, apt-file package cache should be updated.

Now, you’re ready to search for packages using apt-file.

Searching for Packages using apt-File:

Let’s say, you need to compile some software on your Ubuntu 20.04 LTS machine and for that you need the libpcre.so library file. But you don’t know which package to install.

You can simply search for packages which provides the libpcre.so library file as follows:

$ apt-file search 'libpcre.so'

As you can see, the packages that provides the libpcre.so library file are listed.

On the left side (before the colon :), the package names are listed. on the right side (after the colon :), the full file path (available in the package on the left side) that matched the search term (libpcre.so in this case) is listed.

Here, the package libpcre3-dev provides the library file libpcre.so as you can see in the screenshot below. So, if you need the library file libpcre.so, you will have to install the package libpcre3-dev on your Ubuntu 20.04 LTS machine.

If you know the partial path of the file you’re looking for, you can also use that to search for packages using apt-file.

For example, let’s say, you want to use the route command on your Ubuntu 20.04 LTS which is not installed by default.

You know that route is a command. So, it will most likely be inside a bin/ directory.

You can search for the package name which provides the route command as follows:

$ apt-file search 'bin/route'

As you can see, the route command (/sbin/route) is in provided by the net-tools package.

By default, case sensitive search is enabled. So, uppercase and lowercase characters are different when you search for files using apt-file. In case insensitive search, uppercase and lowercase letters are the same.

For example, in case sensitive search libpcre.so and LibPcre.so are not the same. But in case insensitive search, libpcre.so and LibPcre.so are the same and will return the same result.

As you can see, searching for LibPcre.so file returns no result.

You can do case insensitive search using the -i option as follows:

$ apt-file search -i 'LibPcre.so'

As you can see, the same result as before (libpcre.so) is returned.

You can also use Regular Expression to search for package names using the file/directory path.

NOTE: Regular Expression is a topic on its own. It is out of the scope of this article. Check for other articles on LinuxHint if you want to learn more about Regular Expression.

For example, let’s say, you want to search for the package which provides the file where the path ends in bin/route.

You can do a Regular Expression search using the -x option as follows:

$ apt-file search -x '.*bin/route$'

Here, the $ means the end of the file and .* means match anything. So, .*bin/route$ means match any path that ends with bin/route, does not matter what comes before it.

As you can see, the exact package name is listed.

Listing Package Contents using apt-file:

Let’s say, you know a package name (i.e. net-tools). Now, you want to find out what files and directories this package provides before installing it on your computer. You can do that using apt-file.

For example, to list the files and directories of the package net-tools, run the following command:

$ apt-file list net-tools

As you can see, all the files and directories of the net-tools package are listed.

You can also pass (pipe) the output of the apt-file command to grep or egrep to filter the output as you like.

For example, to find out what binary/executable file the net-tools package provides, run the apt-file and grep commands as follows:

$ apt-file list net-tools | grep bin/

As you can see, all the binary/executable files provided by the net-tools package are listed.

The same way, you can check for what configuration files a package (i.e. apache2) provides.

$ apt-file list apache2 | grep etc/

Installing Packages:

Once you have the package name which provides the file(s) you need, you can install it with the following command:

$ sudo apt install <packageName>

Here, replace <packageName> with the package name you wish to install.

For example, to install the libpcre3-dev package, run the following command:

$ sudo apt install libpcre3-dev

To confirm the installation, press Y and then press <Enter>.

libpcre3-dev should be installed and you should have access to your desired file(s).

So, that’s how you find which packages provides the file you need and install it on Ubuntu 20.04 LTS. Thanks for reading this article.

About the author

Shahriar Shovon

Shahriar Shovon

Freelancer & Linux System Administrator. Also loves Web API development with Node.js and JavaScript. I was born in Bangladesh. I am currently studying Electronics and Communication Engineering at Khulna University of Engineering & Technology (KUET), one of the demanding public engineering universities of Bangladesh.