How to Use apt-cache to Search for Packages in Ubuntu

When working in an Ubuntu terminal, you will frequently need to install new software. However, you often won’t know the exact name of the package you need. For example, you might want to install the Apache web server, but is the package called apache, apache2, or httpd?

To find out, you can query the local package cache using the apt-cache command. This utility allows you to search for software, read package descriptions, and check dependencies without needing to install anything or browse the web for answers.

Updating the Cache First

Before searching, it is crucial to ensure your local list of available software is up to date. The apt-cache command searches your local database, not the live internet servers. If your local database is old, you won’t find the newest software.

Update your package lists by running:

sudo apt update

Searching for a Package

The most common use of apt-cache is the search function. You can search using a keyword, and it will return a list of all packages where that keyword appears in either the package name or its description.

For example, to find packages related to the Nginx web server, type:

apt-cache search nginx

You will likely receive a long list of results, formatted with the package name on the left and a brief description on the right:

nginx - small, powerful, scalable web/proxy server
nginx-common - small, powerful, scalable web/proxy server - common files
libnginx-mod-http-image-filter - HTTP image filter module for Nginx

Refining Your Search

If your search returns too many results, you can pipe the output into the grep command to filter it further. For example, if you are looking for an Nginx module related to security, you could run:

apt-cache search nginx | grep security

Getting Detailed Package Information

Once you have identified the exact package name you want (e.g., nginx), you should read its details before installing it to ensure it is exactly what you need.

Use the show command followed by the package name:

apt-cache show nginx

This command outputs a wealth of information, including:

  • Version: The exact version number that will be installed.
  • Maintainer: Who is responsible for keeping the package updated.
  • Architecture: Whether it is built for amd64, arm64, etc.
  • Description: A detailed, multi-paragraph explanation of what the software does.
  • Homepage: The official website of the software project.

Checking Dependencies

Linux software relies heavily on shared libraries (dependencies). If you want to know what other packages will be automatically installed alongside your target software, use the depends command:

apt-cache depends nginx

This will list all the required libraries (e.g., libc6, libpcre3). Conversely, if you want to know what other software on your system relies on a specific package, you can use the rdepends (reverse dependencies) command.

Checking Package Policies

Sometimes, multiple repositories (like the official Ubuntu repo and a third-party PPA) offer different versions of the same package. To see which version your system will prioritise if you run apt install, use the policy command:

apt-cache policy nginx

This output clearly shows the “Installed” version (if any) and the “Candidate” version (the one that will be installed if you issue the install command). It also lists the URL of the repository that is providing the candidate version.

By mastering apt-cache, you can navigate the vast Ubuntu software ecosystem entirely from the command line, ensuring you always install the correct and most up-to-date tools for your server.

Leave a Reply

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

Get the best tech tips delivered straight to your inbox.

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