keepcalculator

06 Numbers and Powers

Apt Cache Command: What It Does & How to Use It

Card No. 06·66931 · Updated · Standard formula, shown below

The apt cache command is a tool for querying the local package metadata that APT (Advanced Package Tool) maintains on Debian-based Linux systems. You use it to search for packages, view details about a specific package, check dependency information, and see what versions are available, all without actually installing anything. It reads from the package lists that are downloaded during apt update, so it works offline and is fast.

Why Use apt-cache Instead of apt or dpkg?

On modern systems, apt itself has many of the same query functions. For example, apt search and apt show work similarly. However, apt-cache is a dedicated tool that only queries the cache; it doesn’t modify anything. This makes it safe to use for scripting and for users who just want to look up information without accidentally triggering an install. Also, apt-cache is available even on minimal systems where apt might be a symlink to a different frontend. It’s also faster in some cases because it doesn’t load the full APT configuration.

Compared to dpkg, which queries the installed package database, apt-cache queries the available package lists. So dpkg -l shows what’s installed, while apt-cache search shows what you could install.

Common Use Cases and Examples

  • Searching for a package: apt-cache search "pdf editor" – returns packages whose name or description mentions pdf editor.
  • Viewing package details: apt-cache show firefox – shows version, size, dependencies, and description.
  • Checking if a package is installed and what version is available: apt-cache policy firefox – shows installed and candidate versions.
  • Listing dependencies: apt-cache depends firefox – shows what packages firefox needs.
  • Finding reverse dependencies: apt-cache rdepends firefox – shows which packages depend on firefox.
  • Seeing all available versions: apt-cache madison firefox – lists versions from each repository.

These commands are read-only, so they never change the system state.

How to Read the Output

The output of apt-cache show is a series of fields like Package:, Version:, Architecture:, Maintainer:, Installed-Size:, Depends:, and Description:. The Depends: field lists other packages that must be installed for this one to work. If a dependency has a version requirement like libc6 (>= 2.34), it means the installed version of libc6 must be at least 2.34.

In apt-cache policy, the Candidate is the version that would be installed if you ran apt install. The priority numbers (e.g., 500) come from the APT preferences in /etc/apt/preferences and the release files. A higher priority wins. If the candidate is different from what you expect, check the priority values and the sources in /etc/apt/sources.list.

Common Mistakes and Troubleshooting

  • Forgetting to run apt update: If the cache is stale, apt-cache search won’t find new packages. Always run sudo apt update first.
  • Using regex without escaping: apt-cache search supports regular expressions. If you search for a dot (e.g., apt-cache search lib.6), it matches any character. Use apt-cache search '^libc6$' for exact matches.
  • Confusing apt-cache with apt-get: apt-get installs and removes packages; apt-cache only queries.
  • Running without sudo: apt-cache doesn’t require root because it only reads files, but some systems might restrict access to the lists. If you get permission errors, try with sudo.
  • Expecting installed package info: apt-cache show shows the available version, not necessarily the installed one. Use dpkg -s for installed package details.

Alternatives and Related Tools

Besides apt-cache, there are other ways to query package information:

  • apt search and apt show – modern equivalents that are part of the apt command.
  • aptitude – a text-based interface that also has search functions.
  • dpkg-query – queries the installed package database.
  • grep on the files in /var/lib/apt/lists/ – the raw data is there, but parsing it manually is error-prone.

For scripting, apt-cache is often preferred because its output is stable and doesn’t require a terminal UI.

Frequently Asked Questions

What is the difference between apt-cache search and apt search?

They do the same thing. apt search is a newer wrapper that also works with the same cache. The main difference is that apt is a single command that includes both query and install functions, while apt-cache is specifically for queries. Some users prefer apt-cache because it’s less likely to accidentally prompt for installation.

Why does apt-cache search return nothing?

If you haven’t run sudo apt update recently, the package lists might be empty or outdated. Also, check that your sources in /etc/apt/sources.list are correct and that you have network access. If you’re searching for a package that doesn’t exist in the configured repositories, it won’t appear.

Can I use apt-cache to see what’s installed?

No, apt-cache only queries the available package lists. To see installed packages, use dpkg -l or apt list --installed.

How do I see all available versions of a package?

Use apt-cache madison <package>. It shows each version and which repository it comes from. The version with the highest priority (usually the highest version number) will be the candidate.

Is apt-cache safe to run as root?

Yes, it’s read-only. It doesn’t modify any files. However, it’s not necessary to run it with sudo because it only reads from world-readable directories.

What does the priority number in apt-cache policy mean?

It’s a numeric value used by APT to decide which version of a package to install. Higher priority wins. The default is 500 for packages from the main repositories, 100 for installed packages, and -1 for packages that are pinned. You can adjust priorities in /etc/apt/preferences.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →