How to Find Which Ubuntu Package Contains a Missing Command or File Using apt-file

When compiling software from source, running third-party scripts, or configuring server environments on Ubuntu, you will inevitably encounter error messages stating command not found or complaining that a critical header file or shared library (such as fatal error: curl/curl.h: No such file or directory) is missing. While the standard apt search tool can look through package names and descriptions, it cannot inspect the actual files contained inside uninstalled packages.

This is where apt-file becomes an essential utility for system administrators and developers. The apt-file tool searches the comprehensive index of contents across all official Ubuntu repositories, allowing you to discover the exact package required to supply any specific binary, configuration file, or development library. In this guide, you will learn how to install, configure, and use apt-file effectively on Ubuntu.

Why Standard APT Search Is Insufficient

Standard package management tools operate on package-level metadata rather than file contents:

  • apt search only matches terms against the package name and short synopsis text. It will not identify that a file named libssl.so.3 belongs to the libssl3 package unless that specific file is named in the package summary.
  • dpkg -S can search for file paths, but it only checks packages that are already installed on your local machine. If a command is missing, dpkg -S cannot tell you which remote package to download.
  • apt-file downloads a lightweight file-list database from your configured APT repositories, allowing instant global search across hundreds of thousands of available packages.

Step 1: Installing apt-file on Ubuntu

The apt-file utility is available directly from the standard Ubuntu universe repository. Open your terminal and install it using the following commands:

sudo apt update
sudo apt install apt-file -y

Step 2: Building the apt-file Contents Database

Before you can perform searches, apt-file must download the Contents indices from your active software sources. Execute the following update command:

sudo apt-file update

This process retrieves compressed file-list indexes for all active repositories defined in /etc/apt/sources.list and /etc/apt/sources.list.d/. Depending on your internet connection, this initial download will take between thirty seconds and two minutes.

Step 3: Searching for a Missing Command or Binary

If a script fails because a specific command is missing from your system, search for its binary path in standard execution directories:

apt-file search /usr/bin/dig

The output lists the matching package name on the left, followed by the full file path on the right:

bind9-dnsutils: /usr/bin/dig

With the exact package name identified, you can immediately install it with sudo apt install bind9-dnsutils.

Step 4: Locating Missing C/C++ Header Files and Development Libraries

When compiling software, missing header files are a common cause of compilation failure. To locate the package that provides a specific C header:

apt-file search curl/curl.h

The query returns the corresponding development package:

libcurl4-openssl-dev: /usr/include/curl/curl.h

Install the development library using sudo apt install libcurl4-openssl-dev to resolve your compiler error.

Step 5: Useful apt-file Flags and Advanced Techniques

You can fine-tune your searches using powerful built-in flags:

  • Fixed String Matching: By default, apt-file treats search patterns as substrings. Use the -F (fixed string) flag to avoid partial matches:
    apt-file search -F /bin/zsh
  • Listing All Files in an Uninstalled Package: To inspect the complete file hierarchy of a package before installing it:
    apt-file list nginx-common
  • Regular Expression Searches: Enable regular expression pattern matching with -x to filter complex file patterns:
    apt-file search -x '/usr/lib/.*.so.1$'

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.