Using apt-get and aptitude
Filtering and Search Patterns
Aptitude provides extensive options for searching for packages. Both the interactive interface and the command line include powerful filters and search patterning. The curses interface provides the search input with the Shift+/. In the CLI, the command is search followed by the search string.
In both cases, aptitude lets you input not only parts of the package name and regular expressions, but also special search patterns. These include ~i for all installed packages, ~M for all those automatically installed through dependencies, and ?description(<description>) in searching for package descriptions (Figure 2).
Most of the search patterns have both long and short forms. The long forms start with the question mark, and the short forms begin with the tilde character. A selection of search patterns is shown in Table 4; a full list is available online  and in the aptitude-doc-en package.
Table 4: Aptitude Search Patterns
|?not(<pattern>)||!<pattern>||Packages that do not match the pattern|
|?and(<pattern1>, <pattern2>)||<pattern1> <pattern2>||Packages that match both patterns|
|?or(<pattern1>, <pattern2>)||<pattern1> | <pattern2>||Packages that match either or both patterns|
|?automatic||~M||Automatically installed packages|
|?broken||~b||Packages with a broken dependency|
|?config-files||~c||Removed but not purged packages|
|?description(<Text>)||~d<Text>||Packages whose description matches|
|?essential||~E||Packages marked as essential in control files|
|?garbage||~g||Packages not required by any manually installed package|
|?obsolete||~o||nstalled packages that cannot be downloaded|
|?provides(<pattern>)||~P<pattern>||Packages that provide a virtual package for searching patterns|
|?section(<section>)||~s<section>||Packages in the given section|
|?upgradable||~U||Installed packages that can be upgraded|
These patterns also help filter the lists in the text interface and commands in the CLI. Use L to filter the text lists, where you get a search input box for entering a pattern, as in a search. The program displays only those software packages that match the pattern (in Figure 3, o[kc]ular). You can accurately find each package, even if the spelling isn't exactly right.
Patterns also prove useful at the command line. The following command marks all packages that begin with the name lib as automatically installed:
# aptitude markauto '~n^lib'
You should normally only install software libraries that you actually use. Our example can have an undesirable side effect: It targets LibreOffice but picks up packages with header files (whose names typically are lib<name>-dev) and removes them, which was not what we wanted.
A more precise filtering is using the section name instead of the package name as the pattern. The following command marks all packages in the libs and oldlibs sections as "automatically installed":
# aptitude markauto '~slibs'
An explicit ~soldlibs option is not necessary because aptitude recognizes the pattern as part of a section name. Specific exceptions (e.g., libraries that end up in a system because of a dependency) can be dealt with through corrections to actions (see Table 3). After the previous command, aptitude would remove all libraries upon which no other packages depend – with the exception of the libtclcl1 package, marked as "not automatically installed," which remains installed.
aptitude markauto '~slibs' 'libtclcl1&m'
Another typical example is purging previously removed configuration and application data from already configured packages:
# aptitude purge '~c'
The same warning as for --purge-unused applies, however. When not used correctly, the action can lead to losing possibly required data. Thus, a setting for packages for the Debian MySQL server can determine whether the system should delete databases during a purge. In any case, a second look before executing the command is advised.
Aptitude proves to be much more complex and provides quite a few more options for managing packages. Of course, this also has its downside. The program uses considerably more time to start and end than apt-get. At startup, it reads its additional database to determine status and, when it closes, it saves it back to the hard disk. This happens even in cases when you would not think it necessary , making it a slower than apt-get.
If you want fine-grained yet efficient control of the packages you use, the mighty aptitude provides far and away more options than apt-get. However, it requires a bit more knowledge and a steeping learning curve, whereas apt-get gets a start right out of the gate. Apt-get might thus provide a better choice if you want to avoid "side effects" in prepared actions, such as accidentally deleting packages.
Once you have learned to appreciate aptitude's text mode interface with all its options for previewing and filtering, must always remember to call aptitude keep-all before each run, to remove all reservations.
If you're writing installation instructions for an Ubuntu package or article, you should make sure you always use apt-get in all your examples. Apt-get provides the basic functionalities and it's best to let the users decide whether to use apt-get or aptitude. As an author, you can't possibly anticipate what side effects could occur with aptitude.
The authors thank Lars Lingner for his encouragement and suggestions in preparing this article.
- "Apt and Aptitude, Part 1: Dynamic Duo," by Axel Beckert and Frank Hofmann, Ubuntu User, 18, pg. 44.
- Configuration in /etc/apt/apt.conf: http://linux.die.net/man/5/apt.conf
- Modular configuration in /etc/apt/apt.conf.d/: http://wiki.debian.org/AptConf
- Manpage for run-parts: http://manpages.debian.net/cgi-bin/man.cgi?query=run-parts
- Aptitude configuration file reference: http://people.debian.org/~abe/aptitude/en/ch02s05s05.html
- Commands for aptitude and apt-get: http://debian-handbook.info/browse/stable/sect.apt-get.html
- Managing packages behind a proxy: http://wiki.itadmins.net/doku.php?id=debian:apt-get_proxy
- Apt from behind a proxy: http://www.unixmen.com/apt-get-behind-a-proxy/
- Ubuntu forum: http://askubuntu.com/questions/196059/how-to-modify-settings-in-apt-conf-file-that-no-longer-exists-in-12-04
- Stump Window Manager: http://stumpwm.nongnu.org/
- Aptitude search pattern reference: http://people.debian.org/~abe/aptitude/en/ch02s04s05.html
- Aptitude user's manual: http://aptitude.alioth.debian.org/doc/en/
- Daniel Hartwig's response to Debian bug #693144: http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=693144#32