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 information like package name, version, description, dependencies, its 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.

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 am going to show you only the most common and useful examples of the apt-cache command in this tutorial.

Always update

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 package. 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 manged 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 kind 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 option of apt-cache command. The policy options helps you debug the issue related to the preference file.

If you specify the package name, it 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-cahce.

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 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.

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



Source link

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.