Overview
The apt-cache command is used in Debian and Ubuntu-based systems to search and interact with APT cache data (metadata) stored locally.
This tool allows you to search for packages, display detailed information, and investigate dependencies quickly without an internet connection. While the interactive apt command (e.g., apt search) is now recommended for general use, apt-cache remains useful for script processing and detailed dependency analysis.
Specifications (Arguments and Options)
Syntax
apt-cache [options] [subcommand] [package_name/pattern]
Main Subcommands (Switches)
| Subcommand | Description |
| search | Searches package names and descriptions for keywords. |
| show | Displays detailed information (version, description, maintainer, etc.). |
| showsrc | Displays source package records. |
| depends | Lists libraries and packages that the specified package depends on. |
| rdepends | Lists packages that depend on (require) the specified package (Reverse Dependencies). |
| showpkg | Displays low-level information regarding package dependency resolution. |
| stats | Displays statistics about the package cache (total count, size, etc.). |
| pkgnames | Lists all package names known to the system (can specify a prefix). |
| policy | Displays installation status and priority (Pinning). |
| dump | Outputs a concise list of all packages in the cache. |
| dumpavail | Outputs available header lists to standard output. |
| gencaches | Regenerates the package and source caches (usually done by apt update). |
Main Options
| Option | Description |
| -n / –names-only | When using search, targets only the package names and ignores the descriptions. |
| –recurse | When using depends or rdepends, follows dependencies recursively. |
| -i / –installed | Limits the scope to only installed packages (affects commands like depends). |
| -c [file] | Reads the specified configuration file. |
| -p [file] | Specifies a file to use as the package cache. |
| -s [file] | Specifies a file to use as the source cache. |
Basic Usage
This flow shows how to find packages related to a keyword (in this case, json-processor) and then view details for a specific tool.
# Search by keyword
apt-cache search json-processor
# Display detailed information
apt-cache show jq
Example Output:
# Results of 'search'
jq - command-line JSON processor
python3-jsonpointer - resolve JSON pointers - Python 3.x
# Results of 'show'
Package: jq
Version: 1.6-1ubuntu0.20.04.1
Architecture: amd64
Maintainer: Ubuntu Developers <ubuntu-devel-discuss@lists.ubuntu.com>
Depends: libjq1 (= 1.6-1ubuntu0.20.04.1), libc6 (>= 2.14)
Description: command-line JSON processor
jq is like sed for JSON data – you can use it to slice and filter and map...
Homepage: https://stedolan.github.io/jq/
Practical Commands
Checking Cache Statistics
Verify the total number of packages the system currently recognizes.
# Display overall statistics and extract the total number of package names
LANG=C apt-cache stats | grep "Total package names"
Extracting Package Official Sites and Repository Information
This is useful when you want to find the developer’s website or the source code management (VCS) location.
# Extract Homepage information
apt-cache show git | grep Homepage
# Extract VCS (Version Control System) URLs from source package info
apt-cache showsrc git | grep -i vcs
Investigating Dependencies
Check what else is required to install a package (dependency) or what will break if you remove a package (reverse dependency).
# List of packages that 'nginx' depends on
apt-cache depends nginx
# List of packages that depend on (require) 'nginx'
apt-cache rdepends nginx
Example output for depends:
nginx
Depends: nginx-core
Depends: nginx-full
Depends: nginx-light
Depends: nginx-extras
Customization Points
Searching by Package Name Only (–names-only)
By default, search also looks through descriptions, which can result in many unrelated matches. Use this option to limit the search to package names.
apt-cache search --names-only python3-django
Displaying Recursive Dependencies (–recurse)
Use this when you want to see the entire chain of dependencies (packages that your target depends on, which in turn depend on others). Note that this can produce a very large output.
apt-cache depends --recurse redis-server
Important Notes
Freshness of Information
apt-cache refers to local cache files. To get the most recent information, you must run sudo apt update to refresh the cache first.
Root Privileges are Not Required
Unlike installing packages (apt install), simply viewing information does not require sudo. General users can run these commands.
Comparison with the apt Command
The modern apt search and apt show commands provide colorized and more readable output. Use apt-cache when you need plain text output for scripts or when you need specific subcommands like depends that are not available in the standard apt command.
Advanced Applications
Listing All Installable Package Names
Use pkgnames to get a list of packages starting with a specific string. This is often used for implementing auto-completion or creating package lists.
# Display all package names starting with 'docker'
apt-cache pkgnames docker
Example Output:
docker-compose
docker.io
docker-registry
docker-doc
Summary
apt-cache is a tool for efficiently searching the “metadata” behind package management.
It allows you to safely investigate packages, troubleshoot dependency issues, and find developer URLs without making any changes to the system. A common practice for Linux administrators is to use apt for daily tasks and apt-cache for detailed investigations.
