How to Use apt-cache Command in Debian, Ubuntu and Other Linux Distributions

With apt-cache command, you can search for package details in the local APT cache. Learn to use apt-cache command in this tutorial.

What is apt-cache command used for?

The apt package manager works on a local cache of package metadata. The metadata usually consists of information like the package name, version, description, dependencies, repository and developers. With the apt-cache command, you can query this local APT cache and get relevant information.

You can search for the availability of a package, its version number, its dependencies among other things. I’ll show you how to use the apt-cache command with examples.

The location of APT cache is /var/lib/apt/lists/ directory. Which repository metadata to cache depends on the repositories added in your source list in the /etc/apt/sources.list file and additional repository files located in ls /etc/apt/sources.list.d directory.

Surprisingly, apt-cache doesn’t clear the APT cache. For that you’ll have to use the apt-get clean command.

Needless to say, the APT packaging system is used on Debian and Debian-based Linux distributions like Ubuntu, Linux Mint, elementary OS etc. You cannot use it on Arch or Fedora.

Using apt-cache command

Apt Cache Command

Like any other Linux command, there are several options available with apt-cache and you can always refer to its man page to read about them.

However, you probably won’t need to use all of them. This is why I will show only the most common and useful examples of the apt-cache command in this tutorial.

💡
It is always a good idea to update the local APT cache to sync it with the remote repositories. How do you do that? You use the command: sudo apt update

Search for packages

The most common use of apt-cache command is for finding packages. You can use a regex pattern to search for a package in the local APT cache.

apt-cache search package_name

By default, it looks for the search term in both the name and description of the package. It shows the matching package along with its short description in alphabetical order.

Apt Cache Search

You can narrow down your search to look for the search term in package names only.

apt-cache search --names-only package_name
Apt Cache Search Names Only

If you want complete details of all the matched packages, you may use the --full flag. It can also be used with --names-only flag.

Apt Cache Show Full

Get detailed package information

If you know the exact package name (or if you have managed to find it with the search), you can get the detailed metadata information on the package.

apt-cache show package_name
Apt Cache Show Pkgname

You can see all kinds of details in the package metadata like name, version, developer, maintainer, repository, short and long description, package size, and even checksum.

There is another option showpkg that displays information about the package name, version and its forward and reverse dependencies.

apt-cache showpkg package_name

apt-cache policy

This is one of the rarely used options of apt-cache command. The policy option helps you debug the issue related to the preference file.

Specifying the package name will show whether the package is installed, which version is available from which repository and its priority.

Apt Cache Policy

By default, each installed package version has a priority of 100 and a non-installed package has a priority of 500. The same package may have more than one version with a different priority. APT installs the version with higher priority unless the installed version is newer.

If this doesn’t make sense, it’s okay. It will be extremely rare for a regular Linux user to dwell this deep into package management.

Check dependencies and reverse dependencies of a package

You can check the dependencies of a package before (or even after) installing it. It also shows all the possible packages that can fulfill the dependency.

apt-cache depends package
Apt-Cache Dependency Check

You may also check which packages are dependent on a certain package by checking the reverse dependencies with apt-cache.

Apt Cache Reverse Dependency

Frankly, I was also surprised to see that a DevOps tool like Ansible has a dependency on a funny Linux command like Cowsay. I think it’s perhaps because after installing Ansible, it displays some message on the nodes.

Check unmet dependencies

You may get troubled with unmet dependencies issue in Ubuntu or other Linux. The apt-cache command provides the option to check all the unmet dependencies of various available packages on your system.

apt-cache unmet
Apt Cache Unmet

Conclusion

You can list all available packages with the apt-cache command. The output would be huge, so I suggest combining it with wc command to get a total number of available packages like this:

apt-cache pkgnames | wc -l

Did you notice that you don’t need to be root user for using apt-cache command?

The newer apt command has a few options available to match the features of apt-cache command. Since apt is new, apt-get and its associated commands like apt-cache are still preferred to be used in scripts. That’s one of the major differences between apt and apt-get commands.

Difference Between apt and apt-get Explained
Brief: This article explains the difference between apt and apt-get commands of Linux. It also lists some of the most commonly used apt commands that replace the older apt-get commands. One of the noticeable new features of Ubuntu 16.04 was the ‘introduction’ of apt command. The reality is that

I hope you find this tutorial helpful. If you have questions about any point discussed above or suggestions to improve it, please let me know in the comments.

Become a Better Linux User

With the FOSS Weekly Newsletter, you learn useful Linux tips, discover applications, explore new distros and stay updated with the latest from Linux world

It's FOSS

Great! You’ve successfully signed up.

Welcome back! You've successfully signed in.

You've successfully subscribed to It's FOSS.

Success! Check your email for magic link to sign-in.

Success! Your billing info has been updated.

Your billing was not updated.