Common Commands:
The apt-cache tool provides several commands for querying the APT package database:
search
: Search for packages by name and description
show
: Display detailed information about specific packages
showpkg
: Display information about a package in a machine-readable format
depends
: Show dependencies of a package
rdepends
: Show reverse dependencies (packages that depend on the specified package)
policy
: Show installation candidate version and priority information
madison
: Display available versions in a tabular format (similar to apt-cache policy)
pkgnames
: List all package names in the system
stats
: Display overall statistics about the cache
unmet
: Show unmet dependencies in the cache
dump
: Show all package records in the cache
dotty
: Generate package dependency graphs in DOT format
Package Cache:
The apt-cache command works with the APT package cache, which:
- Contains information about available packages from repositories configured in
/etc/apt/sources.list
- Is updated when you run
apt update
or apt-get update
- Is stored in
/var/lib/apt/lists/
- Can be used without internet connectivity once updated
- Includes package metadata like dependencies, versions, descriptions, and sizes
Searching Tips:
- Use
apt-cache search --names-only
to search only in package names for faster, more focused results
- Search terms are automatically joined with logical AND; use regular expressions for more complex searches
- Combine with grep for additional filtering:
apt-cache search editor | grep 'text'
- For case-sensitive searches:
apt-cache search --names-only python | grep -i 'Django'
- Search for multiple related packages:
apt-cache search --names-only 'php[0-9]+-'
Understanding Dependencies:
When using apt-cache depends
or apt-cache rdepends
, you'll see different dependency types:
- Depends: Required for the package to function properly
- Recommends: Highly recommended but not strictly required
- Suggests: Optional packages that enhance functionality
- Conflicts: Packages that cannot be installed simultaneously
- Replaces: Packages that this package replaces
- Provides: Virtual packages that this package provides
Integration with Other Tools:
- Use
apt-cache dotty
with Graphviz to visualize package dependencies: apt-cache dotty package | dot -Tpng > package_deps.png
- Combine with apt or apt-get for package management workflows:
apt-cache search package && sudo apt install package
- Use in scripts to check package availability before installation
- Parse output with tools like awk or grep for specific information
Performance Considerations:
- The apt-cache command is generally fast as it reads from local files and doesn't require internet access
- For very large package lists, some operations like
apt-cache dump
may be slow
- The
--recurse
option with depends/rdepends can be resource-intensive for packages with many dependencies
- Use
apt-cache policy
instead of apt-cache show
when you only need version information
Important Notes:
- Unlike
apt
commands, apt-cache
typically doesn't require root privileges
- If the cache is out of date, run
sudo apt update
to refresh it
- The
apt-cache
command is more scriptable than apt search
, with more predictable output format
- For most interactive use cases, the newer
apt
command provides similar functionality with a more user-friendly interface
- The output format of apt-cache commands may change between APT versions