As an experienced Linux system administrator, I frequently need to search for and install software packages on Ubuntu and Debian systems. The apt package manager makes this easy through powerful searching and querying functionality. In this comprehensive guide, I‘ll demonstrate the ins and outs of searching for packages with apt-cache, apt, and aptitude.

Whether you need to hunt down a specific package, investigate package details, or simply explore what‘s available, read on for pro tips and best practices.

A Primer on APT and dpkg

Before diving into package searching, let‘s briefly overview the APT and dpkg system that powers Ubuntu and Debian.

APT stands for Advanced Package Tool. It‘s comprises a collection of tools that manage packages on these distributions. This includes:

  • apt-get – the original command line tool for installing, upgrading, and removing packages
  • apt-cache – queries and displays available package information
  • apt – the modern successor to apt-get with an improved interface

Under the hood, APT handles tasks like:

  • Resolving package dependencies
  • Retrieving packages from remote repositories
  • Installing and removing packages
  • Upgrading the system

The lower level component that APT builds upon is dpkg. The dpkg tools interface directly with .deb packages and the local package database. APT handles higher level management while leveraging dpkg to perform raw package manipulation.

Now that we‘ve seen how apt, dpkg, and .deb packages relate, let‘s focus on searching for packages efficiently.

Searching Packages with apt-cache

The apt-cache program enables searching package metadata and viewing package details. It‘s one of the most flexible and versatile tools for this purpose.

apt-cache searches available package listings. This includes both installed packages and available packages in the configured APT repositories.

Some key advantages of apt-cache search include:

  • Lightning fast searches of available package listing
  • Flexible search syntax
  • Detailed package information listings
  • No dependencies on external repositories

Let‘s go over the syntax basics then look at examples of using apt-cache search in action.

apt-cache search basics

The main command for searching is:

apt-cache search search_terms

Where "search_terms" is what you want to search for. Some key points on the search syntax:

  • Terms can match full or partial package names
  • Descriptions are also searched, so related keywords work
  • Multiple search terms can be chained together

Additional helpful options include:

  • apt-cache --names-only search – Show only package names, omit descriptions
  • apt-cache search --installed search – Match only installed packages
  • apt-cache show package – Show package details

Now let‘s walk through some illustrative examples.

Finding packages by name

The most straightforward search is looking for a package by full or partial name.

For example, to search for available packages related to Apache:

apt-cache search apache

This returns all packages with "apache" in their name or description:

apache2-doc - Apache2 web server documentation
apachetop - displays top CPU consumers among Apache2 processes
httperf - tool for measuring webserver performance 
libapache2-mod-encoding - Apache text encoding conversion module
...

You can then scan the results for relevant packages.

Searching on partial names is also effective. Searching the term "http" reveals web server packages:

apt-cache search http

apache2 - Apache HTTP Server
apache2-doc - Apache HTTP Server documentation
libhttp-cookies-perl - HTTP cookie jars
libwww-perl - simple and consistent interface to the world-wide web
lighttpd - fast webserver with minimal memory footprint
nginx - small, powerful, scalable web/proxy server

And searching for "python" displays all available python packages:

apt-cache search python | less

Using the pipe to less or grep helps shorten results.

Searching package contents with keywords

One of apt-cache‘s strengths is searching package descriptions. You can hunt down packages related to a specific function or technology.

For example, to find docker containers tools:

apt-cache search docker

docker-compose - Punctual, lightweight development environments using Docker
docker.io - Linux container runtime
...

Need a web browser? Search browsers:

apt-cache search browser

w3m - WWW browsable pager with excellent tables/frames support
w3m-img - images display using w3m commands 
lynx - Text-based Web Browser 
elinks - advanced and well-established text-mode web browser
firefox - Safe and easy web browser from Mozilla
google-chrome-stable - The web browser from Google 
...

And networking utilities can be found with:

sudo apt-cache search network

netkit-rsh-client - client for remote shell service provided by rshd
netkit-rsh-server - server for remote shell service
netkit-ftp - An implementation of the FTP protocol as defined in RFC
net-tools - NET-3 networking toolkit
dnsutils - clients provided with BIND
whois - intelligent WHOIS client
mtr - network diagnostic tool
netstat - network connections, routing tables, interface statistics
nmap - Network exploration tool and security / port scanner

Chaining multiple search terms together with operators also works for narrowing down results:

apt-cache search mail smtp server

Install candidate packages after identifying them via apt install package_name.

Displaying package information with apt-cache show

Beyond searching available listings, you can drill into package details with apt-cache show:

apt-cache show package_name

For example:

apt-cache show nginx

Package: nginx
Version: 1.18.0-0ubuntu1
Priority: optional
Section: web
Maintainer: Ubuntu Developers <ubuntu-devel-discuss@lists.ubuntu.com>
Original-Maintainer: Debian Nginx Maintainers <pkg-nginx-maintainers@lists.alioth.debian.org>
Architecture: all
Provides: httpd, webserver
Depends: libc6 (>= 2.17), libpcre3
Recommends: nginx-doc, nginx-common
Suggests: openssl, fcgiwrap, nginx-extras
Homepage: https://nginx.org/
Tag: devel::lang:c, devel::library, httpd::server, implemented-in::c,
 implemented-in::lua, interface::daemon, network::services, network::server,
 protocol::http, protocol::tcp, role::program, scope::utility, web, web::cgi,
 web::http::proxy, web::http::server, web::server, web::static
Download-Size: 144 kB
APT-Sources: http://us.archive.ubuntu.com/ubuntu focal-updates/main amd64 Packages
Description: small, powerful, scalable web/proxy server
 Nginx ("engine X") is a high-performance web and reverse proxy server
 created by Igor Sysoev. It can be used both as a standalone web server and as
 a proxy to reduce the load on back-end HTTP or mail servers.
 .
 This package provides a daemon which can configure itself at boot using the
 /etc/nginx/nginx.conf configuration file and a sample configuration file.

This includes crucial details like:

  • Package dependencies
  • Supported architectures
  • Installed size
  • Included documentation
  • Short description

Similar details are shown for all valid packages.

Now that we‘ve mastered apt-cache, let‘s look at alternate methods available.

Searching with the apt tool

The apt program modernizes many apt-get functions with an improved interface. This includes package searching via the apt search command.

apt search has a few advantages over apt-cache search:

  • Simplified, cleaner output
  • Colorized output highlighting matches
  • Space-delimited results
  • Version number visibility
  • Marks installed packages

The syntax mirrors apt-cache:

apt search search_term

For example:

apt search python

Sorting... Done
Full Text Search... Done

python3/focal,now 3.8.2-0ubuntu2 amd64 [installed]
  interactive high-level object-oriented language (default python3 version)

python3-all/focal-updates 3.8.5-3 amd64
  A set of userspace python tools and python3-dbg debugger

python3-all-dev/focal,now 3.8.2-0ubuntu2 amd64
  Header files and a static library for Python (default)

python3-apport/focal-updates 3.8.5-3 all
  Python 3 library for Apport crash reporting

python3-apport-hooks/focal-updates 3.8.5-3 all
  Python 3 hooks for Apport crash reporting

python3-apt/focal 1.9.4 all 
  Python 3 interface to libapt-pkg
...

The output contains:

  • Clean formatting with spacing between packages
  • Highlighted package names
  • Available version numbers
  • Special marking for installed packages

This improved output readability is the main perk of apt search over apt-cache. Functionally they enable the same searches.

For simplicity, try apt search first then fallback to apt-cache for advanced queries.

Leveraging aptitude searches

The aptitude tool provides another option for package management. Underneath it utilizes APT and dpkg, but it has its own unique features.

Key highlights relevant to searching include:

  • Ncurses terminal interface
  • Advanced conflict resolution
  • Powerful search expressions
  • Manual/automatic package marking
  • Logical dependency solver

Install aptitude if needed:

sudo apt install aptitude

Searching syntax mirrors the above:

aptitude search search_term

This produces verbose results with extensive package metadata:

aptitude search apache

p   apache2-doc                    - Apache2 web server documentation
p   apache2-mpm-event              - Apache2 traditional (process-based) mpm mod
p   apache2-mpm-prefork            - traditional Apache HTTP Server process man
p   apache2-mpm-worker             - Apache2 "worker" Multi-Processing Module 
p   apache2-utils                  - utility programs for webservers
p   apachetop                      - displays top CPU consumers among Apache2 pr
p   h2database                     - embedded Java SQL database, Jetty web serve
p   jetty                          - Java servlet engine and webserver
v   libapache-mod-encoding         - Apache text encoding conversion module
v   libapache-mod-fastcgi          - Apache FastCGI module
v   libapache2-mod-auth-mellon     - Apache authentication module for MELLON
v   libapache2-mod-brotli          - Apache HTTP module providing Brotli compress
p   libapache2-mod-evasive         - Module to protect webservers from DoS/brute-
v   libapache2-mod-fastcgi         - Apache 2 module for FastCGI
p   libapache2-mod-fcgid           - Apache FastCGI module
p   libapache2-mod-lisp2           - Apache2 Common Lisp module
p   libapache2-mod-log-slow        - logging of slow requests for Apache 2
p   libapache2-mod-proxy-html      - Apache module for rewriting links in HTML
p   libapache2-mod-security2       - Apache2 HTTP Server mod_security module 
p   libapache2-mod-shib2           - Apache2 module for shibboleth 2 integration
p   libapache2-mod-uwsgi           - Apache HTTP Server mod_uwsgi module
v   shibboleth-sp2-schemas         - Shibboleth 2 XML schemas
i A httperf                        - tool for measuring webserver performance

This includes front markers indicating:

  • p – Purgeable package
  • v – Virtual package provided by another
  • i – Installable package
  • Installed packages (no marker)

The richness of these results suits more advanced queries. But the verbosity can be overkill for casual package lookups.

With aptitude installed however, it adds a versatile tool to complement APT. And the built in interactive interface allows powerful package management beyond the scope here.

Essential accompanying commands

While the above covers searching packages specifically, a few additional handy commands deserve mention:

apt update – Update package metadata from repositories
apt list – List installed packages
apt-mark – Mark packages manually for later actions
apt-file – Search contents of packages

Consult the man pages for apt, apt-get, apt-cache, and aptitude for further usage details.

Putting it all together

Now that we‘ve covered the key methods and tools – here is a quick decision checklist:

  • apt search – for new users and readable output
  • apt-cache search – versatile queries and info access
  • aptitude search – advanced interactions and customization
  • apt show – detailed package information
  • apt update && apt upgrade – regular system updates

Follow best practices like updating repositories frequently and leveraging multiple search tools. This gives a complete package searching toolkit.

The apt ecosystem offers immense flexibility – integrate these techniques into your administrative workflow.

Conclusion

With an understanding of the apt tools at your disposal, finding and investigating packages becomes second nature.

The apt-cache command line tool in particular provides powerful search capabilities. Packages can be discovered by name, keywords, metadata like descriptions, and using advanced operators. Consuming available package info enables wise software choices.

Complementary utilities like apt, aptitude and accompanying helpers fill out a distro admin‘s toolkit further.

Managing software is easier thanks to the depth of Debian and Ubuntu‘s packaging ecosystem. With practice, hunting down packages speeds up deployments and reduces headaches. Master these search techniques to enhance your server, desktop and DevOps apt prowess even further.

Similar Posts