28.3 apt-cache search and apt-cache show: Finding Packages
Right, so you’ve decided you want to install something. Good for you. But you don’t just throw apt install at the wall and hope something sticks. That’s how you end up with a system cluttered with half a dozen text editors you’ll never use and a package named cowsay that, well, says things with a cow. First, you need to find the right package. That’s where apt-cache comes in. Think of it as the card catalog for the vast, sometimes bewildering library of software in your repositories.
apt-get is for doing things (installing, upgrading), while apt-cache is for asking things. It queries the local database APT maintains, which is a cached copy of the lists from your configured repositories. This is key: it works entirely offline. You’re searching what you already know is available, which is why it’s lightning fast.
The Kitchen Sink Search: apt-cache search
The search command is your blunt instrument. You give it a string, and it returns every package whose name or description contains that string. I’m not kidding. It’s a full-text search on the metadata, and it can be… overwhelming.
apt-cache search web server
Go on, try it. You’ll get a firehose of output: apache2, nginx, lighttpd, but also a bunch of utilities, documentation packages, and things that happen to have the words “web” and “server” somewhere in their mile-long description. This is its greatest strength and its most glaring weakness. It’s fantastic for casting a wide net when you only have a vague idea of what you’re looking for, but you have to be prepared to wade through the results.
Pro tip: since it’s a regex pattern, you can use a caret ^ to anchor the search to the beginning of the package name. This is how you narrow things down drastically. Want to find all packages that start with “nginx”?
apt-cache search ^nginx
This will return nginx-full, nginx-light, nginx-extras, and so on, but it won’t return python3-nginx because that doesn’t start with “nginx”. This is how you avoid the noise.
Actually Finding What You Need
The sheer volume of results from apt-cache search is the number one pitfall for newcomers. You’ll see twenty different packages and have no idea which one is the actual, canonical one you want. Here’s the insider knowledge: look for the ones without a hyphenated suffix first. The output usually lists the package name followed by a short description.
From our ^nginx search, you’d see:
nginx - small, powerful, scalable web/proxy server
nginx-common - common files for nginx
nginx-core - nginx core files
...
nginx is the main, meta-package that will pull in the standard version. The others are components or variants. Start with the short, sweet name. When in doubt, the one with the most concise description is usually the primary package.
The Package Deep Dive: apt-cache show
Once you’ve isolated a potential candidate from your search, say nginx, you need to know what you’re actually getting into. This is where apt-cache show is your best friend. It dumps the entire metadata record for a package. This isn’t just a description; it’s the whole spec sheet.
apt-cache show nginx
The output is a wall of text, but here’s what to look for, because frankly, most of it is boilerplate:
- Description: This is the long-form explanation. Read this. It will often tell you what the package is for and how it relates to other packages.
- Depends: This is the dealbreaker list. These packages must be installed alongside it. If you see a massive list here, you know this thing pulls in a whole universe of dependencies. Install with eyes wide open.
- Conflicts and Breaks: This is APT’s way of saying “if you have this other thing, we can’t be in the same room together.” Heed this warning.
- Provides: Sometimes a package provides a virtual name. This is mostly for resolving dependencies behind the scenes, but it’s good to know.
- Version and Origin: Tells you what version you’ll get and which repository it’s coming from. Crucial for debugging why you’re not getting the version you expected.
Why showpkg is Your Debugging Ally
There’s a related, more technical command: apt-cache showpkg. It gives you the information from show but in a more structured, parseable format. It’s less for humans to read and more for scripts or for when you need a clearer view of the dependency chains.
apt-cache showpkg nginx
The key section is “Reverse Provides:” at the bottom. This tells you what other packages provide the same functionality. If you’re dealing with a conflict or a virtual package (like mail-transport-agent), this is where you go to see your options. It’s the “choose your own adventure” page for package dependencies. You won’t use it every day, but when you need it, you’ll be glad it exists.