The next example shows two different ways to install package1 and to remove package2.
# aptitude install package1 package2-
[...]
# aptitude remove package1+ package2
[...]
TIP apt-get --reinstall and aptitude reinstall
The system can sometimes be damaged after the removal or modification of files in a package. The easiest way to retrieve these files is to reinstall the affected package. Unfortunately, the packaging system finds that the latter is already installed and politely refuses to reinstall it; to avoid this, use the --reinstall option of the apt-get command. The following command reinstalls postfix even if it is already present:
# apt-get --reinstall install postfix
The aptitude command line is slightly different, but achieves the same result with aptitude reinstall postfix.
The problem does not arise with dpkg, but the administrator rarely uses it directly.
Be careful, using apt-get --reinstall to restore packages modified during an attack certainly cannot recover the system as it was. Section 14.6, “Dealing with a Compromised Machine” details the necessary steps to take with a hacked system.
If the file sources.list mentions several distributions, it is possible to give the version of the package to install. A specific version number can be requested with aptitude install package=version, but indicating its distribution of origin (Stable, Testing or Unstable) — with aptitude install package/distribution — is usually preferred. With this command, it is possible to go back to an older version of a package (if for instance you know that it works well), provided that it is still available in one of the sources referenced by the sources.list file. Otherwise the snapshot.debian.org archive can come to the rescue (see sidebar GOING FURTHER Old package versions: snapshot.debian.org).
Example 6.2. Installation of the unstable version of spamassassin
# aptitude install spamassassin/unstable
GOING FURTHER The cache of .deb files
APT keeps a copy of each downloaded .deb file in the directory /var/cache/apt/archives/. In case of frequent updates, this directory can quickly take a lot of disk space with several versions of each package; you should regularly sort through them. Two commands can be used: aptitude clean entirely empties the directory; aptitude autoclean only removes packages which cannot be downloaded (because they have disappeared from the Debian mirror) and are therefore clearly useless (the configuration parameter APT::Clean-Installed can prevent the removal of .deb files that are currently installed).
6.2.3. System Upgrade
Regular upgrades are recommended, because they include the latest security updates. To upgrade, use aptitude safe-upgrade or apt-get upgrade (of course after aptitude update). This command looks for installed packages which can be upgraded without removing any packages. In other words, the goal is to ensure the least intrusive upgrade possible. apt-get is slightly more demanding than aptitude because it will refuse to install packages which were not installed beforehand.
TIP Incremental upgrade
As we explained earlier, the aim of the aptitude update command is to download for each package source the corresponding Packages (or Sources) file. However, even after a bzip2 compression, these files can remain rather large (the Packages.bz2 for the main section of Squeeze takes more than 8 MB). If you wish to upgrade regularly, these downloads can take up a lot of time.
A “new feature” (available since Etch) is that APT can now download the changes since the previous update, as opposed to the entire file. To achieve this, official Debian mirrors distribute different files which list the differences between one version of the Packages file and the following version. They are generated at each update of the archives and a history of one week is kept. Each of these “diff” files only takes a few dozen kilobytes for Unstable, so that the amount of data downloaded by a weekly aptitude update is often divided by 10. For distributions like Stable and Testing, which change less, the gain is even more noticeable.
However, it can sometimes be of interest to force the download of the entire Packages.bz2 file, especially when the last upgrade is very old and when the mechanism of incremental differences would not contribute much. This can also be interesting when network access is very fast but when the processor of the machine to upgrade is rather slow, since the time saved on the download is more than lost when the computer calculates the new versions of these files (starting with the older versions and applying the downloaded differences). To do that, you can use the configuration parameter Acquire::Pdiffs and set it to false.
aptitude will generally select the most recent version number (except for Experimental packages, which are ignored by default whatever their version number). If you specified Testing or Unstable in your sources.list, aptitude safe-upgrade will switch most of your Stable system to Testing or Unstable, which might not be what you intended.
To tell aptitude to use a specific distribution when searching for upgraded packages, you need to use the option -t or --target-release, followed by the name of the distribution you want (for example: aptitude -t stable safe-upgrade). To avoid specifying this option every time you use aptitude, you can add APT::Default-Release "stable"; in the file /etc/apt/apt.conf.d/local.
For more important upgrades, such as the change from one major Debian versionto the next, you need to use aptitude full-upgrade (the option used to be named dist-upgrade, for “distribution upgrade”). With this instruction, aptitude will complete the upgrade even if it has to remove some obsolete packages or install new dependencies. This is also the command used by users who work daily with the Debian Unstable release and follow its evolution day by day. It is so simple that it hardly needs explanation: APT's reputation is based on this great functionality.
aptitude dist-upgrade is still available as a synonym for aptitude full-upgrade; apt-get only recognizes the former.
6.2.4. Configuration Options
Besides the configuration elements already mentioned, it is possible to configure certain aspects of APT by adding directives in a file of the /etc/apt/apt.conf.d/ directory. Remember for instance that it is possible for APT to tell dpkg to ignore file conflict errors by specifying DPkg::Options { "--force-overwrite"; }.