Aptitude vs. apt-get: Which is the recommended (aka the "right") tool to use?

Some time ago I read that aptitude is the preferred tool for installation on Debian-based systems. But when you search around on how to administer a Debian-based system, then aptitude is rarely mentioned. Most people seem to prefer apt-get - and that's even true for the Debian wiki pages!

Thus I am wondering if I have missed something. Which is the right tool to use?


Solution 1:

aptitude and apt-get work the same for many tasks, but for the most tricky cases, such as distribution upgrades (apt-get dist-upgrade vs. aptitude full-upgrade), they have different rules, and aptitude's rules are nearly always better in practice where they disagree.

The reason you see more documentation for apt-get over aptitude is mostly inertia: aptitude has not been the recommended front end to APT for all that long, so much of the existing documentation hasn't been updated, and there are plenty of people who recognise the advantages of aptitude over apt-get but use apt-get reflexively.

I've recently got to the point where I usually follow apt with a iand not a - when I type.

Postscript Note that the rules used in apt-get and aptitude are moving targets - as Hubert notes in comments, the upgrade path recommended from Debian Lenny now uses apt-get, not aptitude. This reflects the fact that apt-get keeps track of less state about the current package than aptitude, and so does not need to worry about APT state not being "clean", and because apt-get rules are smarter than they used to be. I still use and recommend aptitude over apt-get, but it is a more nuanced recommendation

Solution 2:

aptitude makes it convenient to show what programs in a search you already have installed on the system (with the help of grep):

aptitude search flash | grep ^i

Actually, aptitude's search is far more powerful than what you get piping through grep, as it supports contextual searching:

e.g., this finds all packages with 'flash' in the package name that are installed:

aptitude search '~iflash'

An equivalent "long form" of the "short form" ~i:

aptitude search '?installed(flash)'

Note that search patterns are free (unanchored) by default. To anchor them, you need to use anchor patterns '^' (beginning of string) and/or '$' (end-of-string).

To find all packages whose names start with either 'ttf' or 'font':

aptitude search '(^ttf|^font)'

(Note: this is a workaround for a bug in aptitude, as the correct regex of '^(ttf|font)' does not work properly -- it finds packages whose names start with 'ttf' or contain 'font'.)

Other nifty aptitude features:

Show all packages with 'firmware' in their name that ALSO have 'wireless' in their description:

aptitude search 'firmware ~dwireless'

or long form:

aptitude search '?and(?name(firmware),?description(wireless))'

(Note: in the short form, space-delimited arguments are ANDed within quotes; if passed as separate argv[] commandline arguments they are ORed.)

p   atmel-firmware       - Firmware for Atmel at76c50x wireless networking chips.
p   firmware-atheros     - Binary firmware for Atheros wireless cards
...
p   libertas-firmware    - Firmware for Marvell's libertas wireless chip series
p   zd1211-firmware      - Firmware images for the zd1211rw wireless driver

~U shows all packages that are Upgradeable from their current versions with new versions:

# aptitude update ; aptitude versions '~U'
Package virtualbox-4.1:            
i   4.1.18-78361~Debian~squeeze                       100
p   4.1.20-80170~Debian~squeeze     <NULL>            500

Show packages that Recommend 'gcc-multilib'

$ aptitude search '~DRecommends:gcc-multilib'
i   libc6-dev-i386   - Embedded GNU C Library: 32-bit development libraries for AMD64

Explain why 'fuse-utils' might need to be installed

$ aptitude why fuse-utils
i   xorg           Depends    xterm | x-terminal-emulator
pi  gnome-terminal Provides   x-terminal-emulator
pi  gnome-terminal Recommends gvfs
pi  gvfs           Depends    libgdu0 (>= 2.29.90)
pi  libgdu0        Depends    udisks (< 1.1.0)
pi  udisks         Recommends ntfsprogs
pi  ntfsprogs      Depends    fuse-utils (> 2.5.0)

(This example shows some of the craziness resulting from the default since Squeeze(?) of installing all "Recommends" packages. Installing gnome-terminal ends up installing ntfsprogs and fuse-utils, egad! I think most folks just want the terminal perspective and not the builtin NTFS integration, which is optional, unless they specified it.)

Find all packages that provide the service "mail-transport-agent":

$ aptitude search '?provides(mail-transport-agent)'
p   citadel-mta          - complete and feature-rich groupware server (mail transport agent)
...
p   nullmailer           - simple relay-only mail transport agent
p   postfix              - High-performance mail transport agent
i   sendmail-bin         - powerful, efficient, and scalable Mail Transport Agent
p   ssmtp                - extremely simple MTA to get mail off the system to a mail hub
p   xmail                - advanced, fast and reliable ESMTP/POP3 mail server

Show all package names that are installed, that are not either Essential or Automatically installed by dependencies:

$ aptitude search '~i!(~E|~M)' -F '%p'

Unfortunately, this stuff is rather poorly documented and hard to find, but here's the best reference (from the 'aptitude' maintainer).