6.2. aptitude
, apt-get
, and apt
Commands
APT is a vast project, whose original plans included a graphical interface. It is based on a library which contains the core application, and apt-get
is the first front end — command-line based — which was developed within the project. apt
is a second command-line based front end provided by APT which overcomes some design mistakes of apt-get
.
Numerous other graphical interfaces then appeared as external projects: synaptic
, aptitude
(which includes both a text mode interface and a graphical one — even if not complete yet), wajig
, etc. The most recommended interface, apt
, is the one that we will use in the examples given in this section. Note however that apt-get
and aptitude
have a very similar command line syntax. When there are major differences between apt
, apt-get
and aptitude
, these differences will be detailed.
For any work with APT, the list of available packages needs to be updated; this can be done simply through apt update
. Depending on the speed of your connection, the operation can take a while since it involves downloading a certain number of Packages
/Sources
/Translation-language-code
files, which have gradually become bigger and bigger as Debian has developed (at least 10 MB of data for the main
section). Of course, installing from a CD-ROM set does not require any downloading — in this case, the operation is very fast.
6.2.2. Installazione e rimozione
With APT, packages can be added or removed from the system, respectively with apt install package
and apt remove package
. In both cases, APT will automatically install the necessary dependencies or delete the packages which depend on the package that is being removed. The apt purge package
command involves a complete uninstallation — the configuration files are also deleted.
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
apt install package=version
, but indicating its distribution of origin (
Stable,
Testing or
Unstable) — with
apt 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
APPROFONDIMENTO Le vecchie versioni dei pacchetti: snapshot.debian.org
).
Esempio 6.3. Installazione della versione unstable di spamassassin
#
apt install spamassassin/unstable
6.2.3. Aggiornamento del sistema
Regular upgrades are recommended, because they include the latest security updates. To upgrade, use apt upgrade
, apt-get upgrade
or aptitude safe-upgrade
(of course after apt 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
or apt
because it will refuse to install packages which were not installed beforehand.
apt
will generally select the most recent version number (except for packages from Experimental and stable-backports, which are ignored by default whatever their version number). If you specified Testing or Unstable in your sources.list
, apt upgrade
will switch most of your Stable system to Testing or Unstable, which might not be what you intended.
To tell apt
to use a specific distribution when searching for upgraded packages, you need to use the -t
or --target-release
option, followed by the name of the distribution you want (for example: apt -t stable upgrade
). To avoid specifying this option every time you use apt
, 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 version to the next, you need to use apt full-upgrade
. With this instruction, apt
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.
Unlike apt
and aptitude
, apt-get
doesn't know the full-upgrade
command. Instead, you should use apt-get dist-upgrade
(”distribution upgrade”), the historical and well-known command that apt
and aptitude
also accept for the convenience of users who got used to it.
6.2.4. Opzioni di configurazione
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"; }
.
Se si può accedere al Web solo attraverso proxy, bisogna aggiungere una riga come
Acquire::http::proxy "http://proprioproxy:3128"
. Per un proxy FTP, scrivere
Acquire::ftp::proxy "ftp://proprioproxy"
. Per scoprire più opzioni di configurazione, leggere la pagina di manuale
apt.conf(5) con il comando
man apt.conf
(per i dettagli sulle pagine del manuale, vedere la
Sezione 7.1.1, «Pagine di manuale»).
6.2.5. Gestire le priorità dei pacchetti
Uno degli aspetti più importanti nella configurazione di APT è la gestione delle priorità assegnate ad ogni fonte di pacchetti. Per esempio, si potrebbe volere estendere una distribuzione con uno o due pacchetti più nuovi da Testing, Unstable o Experimental. È possibile assegnare una priorità a ciascun pacchetto disponibile (lo stesso pacchetto può avere diverse priorità a seconda della sua versione o della distribuzione che lo fornisce). Queste priorità influenzeranno il comportamento di APT: per ogni pacchetto, selezionerà sempre la versione con la priorità più alta (tranne se questa è più vecchia di quella installata e se la sua priorità è inferiore a 1000).
APT definisce diverse priorità predefinite. Ogni versione installata di un pacchetto ha priorità 100. Una versione non installata ha priorità 500 per impostazione predefinita, ma può arrivare a 990 se è parte del rilascio di destinazione prescelto (definito con l'opzione a riga di comando -t
o con la direttiva di configurazione APT::Default-Release
).
È possibile modificare le priorità con l'aggiunta di voci nel file /etc/apt/preferences
con i nomi dei pacchetti interessati, la loro versione, la loro origine e la loro nuova priorità.
APT non installerà mai una versione più vecchia di un pacchetto (cioè un pacchetto il cui numero di versione è più basso di quello del pacchetto attualmente installato) tranne se la sua priorità è superiore a 1000. APT installerà sempre il pacchetto con priorità più alta che soddisfa questa regola. Se due pacchetti hanno la stessa priorità, APT installerà il più recente (quello con numero di versione più alto). Se due pacchetti hanno le stesse versione e priorità ma diverso contenuto, APT installerà la versione non installata (questa regola è stata creata per coprire il caso di un aggiornamento di pacchetto senza incremento del numero di revisione, che normalmente è richiesto).
Più in concreto, non sarà mai installato un pacchetto la cui priorità è minore di 0. Un pacchetto con priorità compresa tra 0 e 100 verrà installato solo se nessun'altra versione del pacchetto è già installata. Con una priorità da 100 a 500, il pacchetto sarà installato solo se non c'è un'altra versione più recente installata o disponibile in un'altra distribuzione. Un pacchetto con priorità fra 501 e 990 verrà installato solo se non ci sono nuove versioni installate o disponibili nella distribuzione di riferimento. Con una priorità da 990 a 1000, il pacchetto verrà installato a meno che la versione installata non è più recente. Una priorità superiore a 1000 porterà sempre all'installazione del pacchetto anche se costringe APT a retrocedere ad una versione precedente.
When APT checks /etc/apt/preferences
, it first takes into account the most specific entries (often those specifying the concerned package), then the more generic ones (including for example all the packages of a distribution). If several generic entries exist, the first match is used. The available selection criteria include the package's name and the source providing it. Every package source is identified by the information contained in a Release
file that APT downloads together with the Packages
files. It specifies the origin (usually “Debian” for the packages of official mirrors, but it can also be a person's or an organization's name for third-party repositories). It also gives the name of the distribution (usually Stable, Testing, Unstable or Experimental for the standard distributions provided by Debian) together with its version (for example 8 for Debian Jessie). Let's have a look at its syntax through some realistic case studies of this mechanism.
Supponiamo che si vogliano usare solamente i pacchetti della versione stabile di Debian. Quelli forniti in altre versioni non devono essere installati tranne se esplicitamente richiesto. Si dovrebbero scrivere le seguenti voci nel file /etc/apt/preferences
:
a=stable
definisce il nome della distribuzione selezionata. o=Debian
limita l'impostazione ai pacchetti la cui origine è "Debian".
Let's now assume that you have a server with several local programs depending on the version 5.14 of Perl and that you want to ensure that upgrades will not install another version of it. You could use this entry:
La documentazione di riferimento per questo file di configurazione è disponibile nella pagina di manuale apt_preferences(5), che è possibile visualizzare con man apt_preferences
.
6.2.6. Lavorare con più distribuzioni
apt
being such a marvelous tool, it is tempting to pick packages coming from other distributions. For example, after having installed a Stable system, you might want to try out a software package available in Testing or Unstable without diverging too much from the system's initial state.
Even if you will occasionally encounter problems while mixing packages from different distributions,
apt
manages such coexistence very well and limits risks very effectively. The best way to proceed is to list all distributions used in
/etc/apt/sources.list
(some people always put the three distributions, but remember that
Unstable is reserved for experienced users) and to define your reference distribution with the
APT::Default-Release
parameter (see
Sezione 6.2.3, «Aggiornamento del sistema»).
Let's suppose that Stable is your reference distribution but that Testing and Unstable are also listed in your sources.list
file. In this case, you can use apt install package/testing
to install a package from Testing. If the installation fails due to some unsatisfiable dependencies, let it solve those dependencies within Testing by adding the -t testing
parameter. The same obviously applies to Unstable.
In this situation, upgrades (
upgrade
and
full-upgrade
) are done within
Stable except for packages already upgraded to another distribution: those will follow updates available in the other distributions. We will explain this behavior with the help of the default priorities set by APT below. Do not hesitate to use
apt-cache policy
(see sidebar
SUGGERIMENTO apt-cache policy
) to verify the given priorities.
Tutto ruota intorno al fatto che APT considera solo i pacchetti con versione più alta o uguale a quella installata (assumendo che non è stato usato /etc/apt/preferences
per forzare priorità più alte di 1000 per alcuni pacchetti).
Supponiamo di avere installato la versione 1 di un primo pacchetto da Stable e che le versioni 2 e 3 siano disponibili rispettivamente in Testing e Unstable. La versione installata ha una priorità di 100, ma la versione disponibile in Stable (la stessa) ha priorità 990 (perché fa parte della versione di riferimento). I pacchetti in Testing e Unstable hanno priorità 500 (la priorità predefinita per una versione non installata). Il vincitore è dunque la versione 1 con una priorità di 990. Il pacchetto «rimane in Stable».
Prendiamo ora l'esempio di un altro pacchetto la cui versione 2 è stata installata da Testing. La versione 1 è disponibile in Stable e la versione 3 in Unstable. La versione 1 (di priorità 990, quindi minore di 1000) è scartata perché è più bassa della versione installata. Questo lascia in gioco solo le versioni 2 e 3, entrambe con priorità 500. Di fronte a questa alternativa, APT sceglie la versione più recente, quella da Unstable. Se non si desidera che un pacchetto installato da Testing venga migrato a Unstable, è necessario assegnare una priorità minore di 500 (490 ad esempio) ai pacchetti provenienti da Unstable. Si può modificare /etc/apt/preferences
con queste righe:
Package: *
Pin: release a=unstable
Pin-Priority: 490
6.2.7. Tenere traccia dei pacchetti installati automaticamente
One of the essential functionalities of apt
is the tracking of packages installed only through dependencies. These packages are called “automatic”, and often include libraries for instance.
With this information, when packages are removed, the package managers can compute a list of automatic packages that are no longer needed (because there is no “manually installed” packages depending on them). apt-get autoremove
will get rid of those packages. aptitude
and apt
do not have this command: the former because it removes them automatically as soon as they are identified, and the latter probably because the user should not have to manually run such a command. In all cases, the tools display a clear message listing the affected packages.
È buona abitudine marcare come automatico ogni pacchetto di cui non si ha direttamente bisogno, in modo che venga automaticamente rimosso quando non è più necessario.
apt-mark auto pacchetto
marca il pacchetto specificato come automatico, mentre
apt-mark manual pacchetto
fa l'opposto.
aptitude markauto
e
aptitude unmarkauto
funzionano nello stesso modo, anche se offrono più funzionalità per marcare molti pacchetti contemporaneamente (vedere la
Sezione 6.4.1, «aptitude
»). L'interfaccia interattiva basata su console di
aptitude
rende anche facile revisionare il «contrassegno automatico» per molti pacchetti.
People might want to know why an automatically installed package is present on the system. To get this information from the command line, you can use aptitude why package
(apt
and apt-get
have no similar feature):
$
aptitude why python-debian
i aptitude Recommends apt-xapian-index
i A apt-xapian-index Depends python-debian (>= 0.1.15)