6.5. Controlling Package Selection

Left alone, apt always moves every package toward the highest version at the highest priority. Most of the time that is what an administrator wants; the exceptions - a database that must not jump major versions during a busy week, a machine that takes one backported package without adopting the rest, a 32-bit runtime installed alongside the 64-bit one - are what holds, pins, and explicit version selection exist for. The three subsections cover the mechanism in rising order of permanence: a hold freezes one package, a preference shapes candidates continuously, and a backport or a locally built package asks for a specific source.

6.5.1. Holds, Version Selection, and APT Preferences

At this point the fixture ch06-demo sits at version 1.0 with version 2.0 as its candidate - the state Section 6.4.3 left. A hold freezes exactly that situation:

$ sudo apt-mark hold ch06-demo
ch06-demo set on hold.

The confirmation is one line. List what is held, then plan an upgrade to see the effect:

$ apt-mark showhold
ch06-demo
$ apt-get -s upgrade
NOTE: This is only a simulation!
      apt-get needs root privileges for real execution.
      Keep also in mind that locking is deactivated,
      so don't depend on the relevance to the real current situation!
Reading package lists...
Building dependency tree...
Reading state information...
Calculating upgrade...
The following packages have been kept back:
  ch06-demo
The following packages will be upgraded:
  fonts-opensymbol libreoffice-base-core libreoffice-calc libreoffice-common
  libreoffice-core libreoffice-draw libreoffice-gtk3 libreoffice-help-common
  libreoffice-help-en-us libreoffice-impress libreoffice-math
  libreoffice-style-colibre libreoffice-uiconfig-calc
  libreoffice-uiconfig-common libreoffice-uiconfig-draw
  libreoffice-uiconfig-impress libreoffice-uiconfig-math
  libreoffice-uiconfig-writer libreoffice-writer libuno-cppu3t64
  libuno-cppuhelpergcc3-3t64 libuno-purpenvhelpergcc3-3t64 libuno-sal3t64
  libuno-salhelpergcc3-3t64 libvisio-0.1-1 python3-uno uno-libs-private ure
28 upgraded, 0 newly installed, 0 to remove and 1 not upgraded.
[...]

Selected output; the per-package Inst/Conf lines are omitted. The simulation is the same one as Section 6.4.1 with one difference: the held package has moved from the upgrade list to kept back, and the "not upgraded" count has grown by one.

The held package is excluded from upgrade planning entirely - upgrade and full-upgrade both skip it, and unattended-upgrades respects holds too. A hold is a dpkg selection state, so apt-cache policy still shows the candidate honestly:

$ apt-cache policy ch06-demo
ch06-demo:
  Installed: 1.0
  Candidate: 2.0
  Version table:
     2.0 500
        500 file:/srv/ch06-repo trixie/main amd64 Packages
 *** 1.0 500
        500 file:/srv/ch06-repo trixie/main amd64 Packages
        100 /var/lib/dpkg/status

The candidate has not changed; the planner simply will not act on it. Holds are meant to be temporary - release them the moment the reason passes, because a forgotten hold silently freezes security updates too:

$ sudo apt-mark unhold ch06-demo
Canceled hold on ch06-demo.

For a persistent version policy, APT preferences express it as priorities. The strongest form pins one version of one package above everything, including the installed one. Create /etc/apt/preferences.d/ch06-demo.pref:

Package: ch06-demo
Pin: version 1.0
Pin-Priority: 1001

The three lines read: for ch06-demo, version 1.0 receives priority 1001. That number is above 1000, the threshold from the table in Section 6.2.4 at which apt will follow a pin even downhill - the only range that makes apt prefer an older version than the one installed. Check the outcome:

$ apt-cache policy ch06-demo
ch06-demo:
  Installed: 1.0
  Candidate: 1.0
  Version table:
     2.0 500
        500 file:/srv/ch06-repo trixie/main amd64 Packages
 *** 1.0 1001
        500 file:/srv/ch06-repo trixie/main amd64 Packages
        100 /var/lib/dpkg/status

The candidate is 1.0 now, chosen by the pin's 1001 over 2.0's 500. Version pins like this are the tool for "this machine runs exactly this version" - vendor-tested configurations, regulated environments - with the caveat that they also freeze security fixes unless the pin is maintained. Remove the practice pin with sudo rm /etc/apt/preferences.d/ch06-demo.pref, and the candidate returns to 2.0, which apt-cache policy ch06-demo will confirm line for line: version 1.0 back at 500 with the *** installed marker, 2.0 the candidate again.

The Pin: line itself has several forms, each matching a different notion of "where this version came from":

Pin: form Matches
version 1.0 One version string, glob patterns allowed.
release o=… Versions from repositories with that Origin:.
release a=…, n=…, c=…, l=… By archive (suite), codename, component, or label.
origin "hostname" Versions fetched from that host.
* Every version of the named packages.

The third-party containment of Section 6.2.4 used release o=, and the backports section below uses -t, which applies a temporary release-style pin on the command line. Holds and pins compared, since they solve adjacent problems:

Hold Preference pin
Layer dpkg selection state. APT priority rule.
Scope One package, all versions. Any packages the Package: line matches.
Effect Excluded from upgrade planning. Candidate re-ranked; planning follows the ranking.
Can force older than installed No. Yes, at priority ≥ 1000.
Inspect with apt-mark showhold. apt-cache policy (priority visible).

6.5.2. Using Backports Without Replacing the Base Distribution

Backports exist so a stable release can take individual newer packages without becoming something else: maintainers rebuild software from Debian's testing distribution against stable's libraries, so one backported package runs on an otherwise stable machine. The archive design encodes the caution: backports sits at priority 100 (Section 6.2.2 showed tmux's version table with 3.7c-1~bpo13+1 waiting at 100), so backports never upgrades anything until a package is requested from it explicitly. The request takes one of two forms. The per-command form is -t:

$ sudo apt-get install -t trixie-backports tmux
Reading package lists...
Building dependency tree...
Reading state information...
The following additional packages will be installed:
  libevent-core-2.1-7t64
The following NEW packages will be installed:
  libevent-core-2.1-7t64 tmux
0 upgraded, 2 newly installed, 0 to remove and 106 not upgraded.
Need to get 682 kB of archives.
After this operation, 1740 kB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 https://security.debian.org/debian-security trixie-security/main amd64 libevent-core-2.1-7t64 amd64 2.1.13-stable-1~deb13u1 [133 kB]
Get:2 https://deb.debian.org/debian trixie-backports/main amd64 tmux amd64 3.7c-1~bpo13+1 [549 kB]
Fetched 682 kB in 1s (457 kB/s)
Selecting previously unselected package libevent-core-2.1-7t64:amd64.
(Reading database ... 111159 files and directories currently installed.)
Preparing to unpack .../libevent-core-2.1-7t64_2.1.13-stable-1~deb13u1_amd64.deb ...
Unpacking libevent-core-2.1-7t64:amd64 (2.1.13-stable-1~deb13u1) ...
Selecting previously unselected package tmux.
Preparing to unpack .../tmux_3.7c-1~bpo13+1_amd64.deb ...
Unpacking tmux (3.7c-1~bpo13+1) ...
Setting up libevent-core-2.1-7t64:amd64 (2.1.13-stable-1~deb13u1) ...
Setting up tmux (3.7c-1~bpo13+1) ...
Processing triggers for man-db (2.13.1-1) ...

Selected output; progress lines and the needrestart report omitted.

Two details in the summary reward attention. The tmux itself came from trixie-backports, but its dependency libevent-core-2.1-7t64 was fetched from the security archive: -t trixie-backports raises that suite's priority to 990 for this one command, but each dependency still takes its own newest eligible candidate, which for the library was the security update. And the "106 not upgraded" figure is not an accident: with backports momentarily at 990, every backported package's newest version becomes a visible candidate, and the count reports them all as upgradable-but-not-requested. The flag changed the ranking for one transaction; nothing else moved.

Confirm what was installed, then use it:

$ dpkg-query -W -f='${Version}\n' tmux
3.7c-1~bpo13+1
$ tmux -V
tmux 3.7c

The version string carries the whole story: upstream 3.7c, Debian revision 1, and the ~bpo13+1 marker that sorts below the stable revision scheme, so when stable itself ships a 3.7c-based tmux someday, it will supersede the backport cleanly.

The persistent form suits a package that should stay on backports as new backports are published: a preference entry raising that one package from the backports suite above stable's copy - for tmux, a file /etc/apt/preferences.d/tmux-backports.pref with a Package: tmux, Pin: release n=trixie-backports, Pin-Priority: 500 block would keep the backport as the standing candidate without letting any other backported package win. That file is a documented pattern rather than a demonstration; the pin syntax it uses is exactly the one Section 6.5.1 tabulated. What the chapter did demonstrate is the cleanup, because a backport should leave the machine as cleanly as it came:

$ sudo apt-get purge tmux
Reading package lists...
Building dependency tree...
Reading state information...
The following package was automatically installed and is no longer required:
  libevent-core-2.1-7t64
Use 'sudo apt autoremove' to remove it.
The following packages will be REMOVED:
  tmux*
0 upgraded, 0 newly installed, 1 to remove and 29 not upgraded.
After this operation, 1426 kB disk space will be freed.
Do you want to continue? [Y/n] y
(Reading database ... 111176 files and directories currently installed.)
Removing tmux (3.7c-1~bpo13+1) ...
Processing triggers for debianutils (5.23.2) ...
Processing triggers for man-db (2.13.1-1) ...

Selected output; progress lines and needrestart report omitted. The "29 not upgraded" is back to the familiar count - with the transaction over, backports is at 100 again, and the figure is the security batch plus the unpinned fixture.

$ sudo apt-get autoremove --purge
Reading package lists...
Building dependency tree...
Reading state information...
The following packages will be REMOVED:
  libevent-core-2.1-7t64*
0 upgraded, 0 newly installed, 1 to remove and 29 not upgraded.
After this operation, 313 kB disk space will be freed.
Do you want to continue? [Y/n] y
(Reading database ... 111164 files and directories currently installed.)
Removing libevent-core-2.1-7t64:amd64 (2.1.13-stable-1~deb13u1) ...
Processing triggers for libc-bin (2.41-12+deb13u4) ...

Selected output; progress lines omitted.

Backports' own support model completes the picture: the backports suite receives security fixes as backported updates, but a backported package is not covered by the stable security team's guarantees - the reason the suite is opt-in per package, and the reason the base distribution should always remain the source of everything not deliberately taken from backports. Release upgrades treat backports specially too: Chapter 29 removes backport sources before a distribution upgrade for exactly the conflict this section's version scheme exists to prevent.

6.5.3. Multiarch and Locally Built Packages

Two more forms of "software from outside the default candidate" round out the selection toolkit. Multiarch lets one machine install packages of another architecture - overwhelmingly i386 libraries on amd64, to run 32-bit binaries - with dpkg tracking each architecture's packages separately. Locally built packages are one-off archives installed directly by file, with the caveats that path implies.

Multiarch begins by enabling the foreign architecture, a dpkg-level registration that every later apt-get update respects:

# dpkg --add-architecture i386
$ dpkg --print-foreign-architectures
i386

The first command prints nothing on success; the second confirms the registration. The next refresh downloads the foreign indexes - with no change to any source file, because the configured sources already offer every architecture the machine now asks about:

# apt-get update
Get:1 file:/srv/ch06-repo trixie InRelease [1874 B]
Get:1 file:/srv/ch06-repo trixie InRelease [1874 B]
Hit:2 https://security.debian.org/debian-security trixie-security InRelease
Hit:3 https://deb.debian.org/debian trixie InRelease
Get:4 https://security.debian.org/debian-security trixie-security/main i386 Packages [273 kB]
Hit:5 https://deb.debian.org/debian trixie-updates InRelease
Hit:6 https://deb.debian.org/debian trixie-backports InRelease
Get:7 https://deb.debian.org/debian trixie/main i386 Packages [9437 kB]
Get:8 https://deb.debian.org/debian trixie-updates/main i386 Packages [1108 B]
Get:9 https://deb.debian.org/debian trixie-backports/main i386 Packages [280 kB]
Fetched 9992 kB in 7s (1440 kB/s)
Reading package lists...

The four Get lines for i386 Packages indexes are the cost of the registration; the Hit lines show the amd64 indexes untouched. A foreign package is addressed by name with an :architecture suffix, and the policy report shows the foreign candidate like any other:

$ apt-cache policy libc6:i386
libc6:i386:
  Installed: (none)
  Candidate: 2.41-12+deb13u4
  Version table:
     2.41-12+deb13u4 500
        500 https://deb.debian.org/debian trixie/main i386 Packages

Installing it pulls the small closure of i386 support libraries:

$ sudo apt-get install libc6:i386
Reading package lists...
Building dependency tree...
Reading state information...
The following additional packages will be installed:
  gcc-14-base:i386 libgcc-s1:i386 libidn2-0:i386 libunistring5:i386
Suggested packages:
  glibc-doc:i386 locales:i386 libnss-nis:i386 libnss-nisplus:i386
The following NEW packages will be installed:
  gcc-14-base:i386 libc6:i386 libgcc-s1:i386 libidn2-0:i386 libunistring5:i386
0 upgraded, 5 newly installed, 0 to remove and 29 not upgraded.
Need to get 3441 kB of archives.
After this operation, 15.7 MB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 https://deb.debian.org/debian trixie/main i386 gcc-14-base i386 14.2.0-19 [49.4 kB]
Fetched 3441 kB in 10s (332 kB/s)
Selecting previously unselected package gcc-14-base:i386.
(Reading database ... 111159 files and directories currently installed.)
[...]
Setting up libc6:i386 (2.41-12+deb13u4) ...
[...]
Processing triggers for libc-bin (2.41-12+deb13u4) ...
Processing triggers for systemd (257.13-1~deb13u1) ...

Selected output; the remaining four Get lines, the other unpack and setup steps, and the needrestart report are omitted. Every package in the transaction is i386 - the suffix is per package name, so libc6:i386 and the already-installed libc6:amd64 coexist as independent database entries.

The database records them separately, which is how coexistence works:

$ dpkg-query -W -f='${Package} ${Architecture}\n' libc6:i386
libc6 i386

Removing the foreign runtime cleanly requires one deliberate override, and it deserves a clear warning before the command: purging a foreign-architecture C library is safe only when no other package of that architecture remains on the machine; anything i386 still installed stops working at once. apt insists because the Essential: yes flag on libc6 does not distinguish architectures - the flag belongs to the package, and apt warns about removing an essential package even when it is the foreign copy. The command that removes all five i386 packages must state the override explicitly:

# apt-get purge --allow-remove-essential -y libc6:i386 libgcc-s1:i386 gcc-14-base:i386 libidn2-0:i386 libunistring5:i386
Reading package lists...
Building dependency tree...
Reading state information...
The following packages will be REMOVED:
  gcc-14-base:i386* libc6:i386* libgcc-s1:i386* libidn2-0:i386*
  libunistring5:i386*
WARNING: The following essential packages will be removed.
This should NOT be done unless you know exactly what you are doing!
  libgcc-s1:i386 gcc-14-base:i386 (due to libgcc-s1:i386)
  libc6:i386 (due to libgcc-s1:i386)
0 upgraded, 0 newly installed, 5 to remove and 29 not upgraded.
After this operation, 15.7 MB disk space will be freed.
(Reading database ... 111445 files and directories currently installed.)
Removing libidn2-0:i386 (2.3.8-2) ...
Removing libunistring5:i386 (1.3-2) ...
dpkg: warning: overriding problem because --force enabled:
dpkg: warning: this is a protected package; it should not be removed
[...]
Removing libc6:i386 (2.41-12+deb13u4) ...
[...]
Removing libgcc-s1:i386 (14.2.0-19) ...
Removing gcc-14-base:i386 (14.2.0-19) ...
[...]
Purging configuration files for libc6:i386 (2.41-12+deb13u4) ...

Selected output; repeated dpkg protected-package warnings and the needrestart report are omitted. The -y answers the confirmation so the essential-package warning, which cannot be answered per package, is the transaction's one deliberate decision. On a real machine, verify first with dpkg -l '*:i386' that nothing you need remains.

A final apt-get autoremove --purge reports nothing further to remove, and the architecture itself can then be unregistered - the mirror opposite of the enabling sequence:

# dpkg --remove-architecture i386
$ dpkg --print-foreign-architectures

The removal prints nothing on success; the second command prints nothing because the foreign list is now empty - the honest answer, and the one to verify before the next refresh, which will drop the i386 indexes again (the earlier capture in reverse: four Hit lines for amd64, no i386 Get lines).

dpkg architecture option Description
--print-architecture The machine's native architecture.
--print-foreign-architectures Enabled foreign architectures, one per line.
--add-architecture arch Enable a foreign architecture.
--remove-architecture arch Disable one, refused while packages of it remain.

Locally built packages - one .deb built on this machine, not published in any repository - are the last selection form. The fixture script of Section 6.2.3 left one tree unbuilt on purpose: ch06-locally-built, whose entire content is a DEBIAN/control file and one script in usr/bin. Build it by hand, from the workspace root, with the tool whose reading form Section 6.3.4 already introduced:

$ cd ~/debian-handbook/package-management
$ dpkg-deb --root-owner-group --build local-packages/ch06-locally-built-1.0 local-packages/ch06-locally-built_1.0_all.deb
dpkg-deb: building package 'ch06-locally-built' in 'local-packages/ch06-locally-built_1.0_all.deb'.
$ dpkg-deb -I local-packages/ch06-locally-built_1.0_all.deb | sed -n "1,12p"
 new Debian package, version 2.0.
 size 912 bytes: control archive=376 bytes.
     259 bytes,     8 lines      control
 Package: ch06-locally-built
 Version: 1.0
 Section: misc
 Priority: optional
 Architecture: all
 Maintainer: Debian Handbook Lab <lab@example.org>
 Description: Local test package ch06-locally-built version 1.0
  Built by the Debian handbook package-management lab.

The cd prints nothing, the build prints its one confirmation line, and the dpkg-deb -I pipeline shows the archive's metadata - including the new Debian package, version 2.0 header, which is the archive format version, not the package's. Installing a local file is dpkg's oldest interface, and its limitation is the teaching point:

# dpkg -i /home/john/debian-handbook/package-management/local-packages/ch06-locally-built_1.0_all.deb
Selecting previously unselected package ch06-locally-built.
(Reading database ... 111159 files and directories currently installed.)
Preparing to unpack .../ch06-locally-built_1.0_all.deb ...
Unpacking ch06-locally-built (1.0) ...
Setting up ch06-locally-built (1.0) ...

dpkg -i unpacks and configures the named archive, and it does exactly that and nothing more: no repository lookup, no dependency resolution, no candidate selection. If the archive's dependencies are unsatisfied, the install fails half-done - the ch06-broken-deps fixture of Section 6.7.1 demonstrates that failure and its repair. For an archive with dependencies, apt-get install ./path.deb (apt resolves and fetches what the local archive needs) or publication into a real repository (Section 6.6) is the sound path. Either way, dpkg now owns the package as completely as any other:

$ ch06-locally-built
locally built package works
$ dpkg -S /usr/bin/ch06-locally-built
ch06-locally-built: /usr/bin/ch06-locally-built

The program runs, and the file-owner query answers with the locally built package's name. Removal needs no special form - the package is in the database, so sudo apt-get purge ch06-locally-built removes it after the usual confirmation.