6.3. Installing and Removing Software

Installing software on Debian is a loop, not a single command: refresh the indexes, inspect what would change and where each version comes from, apply the change, and verify the result. The subsections follow that loop with real transactions - a package from Debian, packages from the practice archive, and one package whose recommendations must not come along - then cover removal, purge, and the cleanup of automatically installed dependencies, and finally the tools that answer "which package owns this file?".

6.3.1. Refreshing Indexes and Inspecting Candidate Versions

The refresh itself ran in Section 6.2.3 and needs no repetition here; what deserves attention is what a refresh changes and how to read the result. apt-get update rewrites /var/lib/apt/lists/ from each source's signed indexes, and every later decision - candidates, apt-cache show, simulations - reads those lists. Between refreshes, apt may still use the previous indexes (that is the point of the freshness fields: they bound how long an index may serve), but a version that appeared upstream today is invisible until the next refresh. The example machine's pending security updates - visible in the captures below as the packages apt counts as "not upgraded" - are exactly such a case: they became visible after a refresh and remain pending while this chapter's fixture demonstrations run.

Reading candidates is apt-cache policy's job, on packages from any source. For a core tool, the report usually shows installed and candidate in agreement:

$ apt-cache policy apt
apt:
  Installed: 3.0.4
  Candidate: 3.0.4
  Version table:
 *** 3.0.4 500
        500 https://security.debian.org/debian-security trixie-security/main amd64 Packages
        100 /var/lib/dpkg/status
     3.0.3 500
        500 https://deb.debian.org/debian trixie/main amd64 Packages

The *** marks the installed version, 3.0.4, which arrived from the security archive - stable's mirror still lists the older 3.0.3, and the /var/lib/dpkg/status line at priority 100 is dpkg's own database appearing in the ranking, the record of what is installed rather than an offer. For a package offered by the practice archive, the same report shows both versions and their source:

$ apt-cache policy ch06-demo
ch06-demo:
  Installed: (none)
  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

Both versions come from file:/srv/ch06-repo at priority 500, and the newer one is the candidate - the fixture of Section 6.4.3, which installs 1.0 deliberately and then upgrades it. Whenever a version question arises, this report is the answer: it shows every source offering the package and the priority that picked the winner, including pins and holds once Sections 6.5.1 and 6.5.2 add them. The same reading applies to whole-system changes, and the plan for one of those belongs to Section 6.4.1, where the machine's real pending updates make the example.

The apt-get subcommands this chapter uses, gathered in one place:

apt-get usage Description
update Refresh signed indexes from every source.
upgrade Upgrade installed packages without removing anything or installing new dependencies.
full-upgrade Upgrade, allowing new packages and removals where dependencies demand.
install pkg[=ver] Install, or install a chosen version explicitly.
remove pkg… Remove files, keep configuration.
purge pkg… Remove files and configuration.
autoremove [--purge] Remove automatically installed packages nothing needs.
download pkg… Fetch archives into the current directory without installing.
-s Simulate; print the plan, change nothing.
-t suite Raise a suite's priority for this one command.
-f install Repair broken dependencies first (Section 6.7.1).
--no-install-recommends Skip recommended packages for this transaction.
--download-only / --print-uris Fetch archives without installing (Section 6.6.1).

6.3.2. Installing Packages and Understanding Recommendations

The plan's candidate from Section 6.2.4 - Debian's real cowsay - is now installed for real:

$ sudo apt-get install cowsay
Reading package lists...
Building dependency tree...
Reading state information...
Suggested packages:
  filters cowsay-off
The following NEW packages will be installed:
  cowsay
0 upgraded, 1 newly installed, 0 to remove and 28 not upgraded.
Need to get 21.4 kB of archives.
After this operation, 94.2 kB of additional disk space will be used.
Get:1 https://deb.debian.org/debian trixie/main amd64 cowsay all 3.03+dfsg2-8 [21.4 kB]
Fetched 21.4 kB in 0s (59.1 kB/s)
Selecting previously unselected package cowsay.
(Reading database ... 111156 files and directories currently installed.)
Preparing to unpack .../cowsay_3.03+dfsg2-8_all.deb ...
Unpacking cowsay (3.03+dfsg2-8) ...
Setting up cowsay (3.03+dfsg2-8) ...
Processing triggers for man-db (2.13.1-1) ...

Running kernel seems to be up-to-date.

No services need to be restarted.

No containers need to be restarted.

No user sessions are running outdated binaries.

No VM guests are running outdated hypervisor (qemu) binaries on this host.

Selected output; the (Reading database ... progress percentages, which redraw a single line on a terminal, are kept only in their final form. The closing five-line report is needrestart, a package installed on the example system that runs as a dpkg hook after every transaction and assesses what the change means for running processes - here: the running kernel matches the newest installed one, and no service, container, session, or virtual-machine guest is executing outdated code. Section 6.4.4 uses the same tool's batch output; the report is trimmed from later install captures in this chapter, since it repeats this assessment verbatim while the fixtures change nothing that runs. No confirmation was asked, and that is apt-get's rule rather than an omission: a transaction that only installs the packages named on the command line, with all dependencies already satisfied and nothing removed or upgraded beyond them, proceeds without a prompt; as soon as a plan pulls in additional packages, removes anything, or otherwise exceeds what was asked, apt-get shows the plan and asks Do you want to continue? [Y/n] - the examples below hit both cases. The summary block carries the plan's substance: one new package, 21.4 kB to fetch, 94.2 kB of disk once installed, and the pin from Section 6.2.4 quietly doing its work - the fetched version is Debian's 3.03+dfsg2-8, not the archive's fake. The stages after the download are dpkg's, exactly as Figure 6.1 drew them: Selecting registers a previously unknown package, Unpacking places the files, Setting up runs the postinst and finishes configuration, and the man-db trigger regenerates the manual-page index because a new man page arrived. On a desktop, packages that ask configuration questions open a debconf dialog at this point; cowsay asks nothing. Verify what dpkg now records, then simply run it:

$ dpkg-query -W -f='${Status} ${Version}\n' cowsay
install ok installed 3.03+dfsg2-8
$ cowsay Packages are signed
 _____________________
< Packages are signed >
 ---------------------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

The second half of this subsection is about the packages apt adds to a request. The fixture pair from the practice archive is built to demonstrate: ch06-suite declares Recommends: ch06-extra and nothing else. Look at the relationship first:

$ apt-cache depends ch06-suite
ch06-suite
  Recommends: ch06-extra

Now install it, and watch what the recommendation does:

$ sudo apt-get install ch06-suite
Reading package lists...
Building dependency tree...
Reading state information...
The following additional packages will be installed:
  ch06-extra
The following NEW packages will be installed:
  ch06-extra ch06-suite
0 upgraded, 2 newly installed, 0 to remove and 28 not upgraded.
Need to get 0 B/1676 B of archives.
After this operation, 0 B of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 file:/srv/ch06-repo trixie/main amd64 ch06-extra all 1.0 [832 B]
Get:2 file:/srv/ch06-repo trixie/main amd64 ch06-suite all 1.0 [844 B]
Selecting previously unselected package ch06-extra.
(Reading database ... 111156 files and directories currently installed.)
Preparing to unpack .../main/ch06-extra_1.0_all.deb ...
Unpacking ch06-extra (1.0) ...
Selecting previously unselected package ch06-suite.
Preparing to unpack .../main/ch06-suite_1.0_all.deb ...
Unpacking ch06-suite (1.0) ...
Setting up ch06-extra (1.0) ...
Setting up ch06-suite (1.0) ...

Selected output; progress lines and the needrestart report - shown in full at the first install above - are omitted.

The prompt appeared this time because the transaction installs two packages while one was named, and the y on the prompt line is the answer typed at it. ch06-extra was pulled in by the recommendation, and both are now recorded as installed:

$ dpkg-query -W -f='${Package} ${Status}\n' ch06-suite ch06-extra
ch06-extra install ok installed
ch06-suite install ok installed

Recommendations are the Debian compromise between "install only what is required" and "install what the package actually works well with": they are on by default, but apt marks every recommended package it pulls in as automatically installed - installed as a consequence, not as a choice. That marking is what lets autoremove (Section 6.3.3) clean them up later. When a recommendation is unwanted, one flag narrows the transaction:

$ sudo apt-get install --no-install-recommends ch06-suite
Reading package lists...
Building dependency tree...
Reading state information...
Recommended packages:
  ch06-extra
The following NEW packages will be installed:
  ch06-suite
0 upgraded, 1 newly installed, 0 to remove and 28 not upgraded.
Need to get 0 B/844 B of archives.
After this operation, 0 B of additional disk space will be used.
Get:1 file:/srv/ch06-repo trixie/main amd64 ch06-suite all 1.0 [844 B]
Selecting previously unselected package ch06-suite.
(Reading database ... 111156 files and directories currently installed.)
Preparing to unpack .../main/ch06-suite_1.0_all.deb ...
Unpacking ch06-suite (1.0) ...
Setting up ch06-suite (1.0) ...

Selected output; progress lines and needrestart report omitted. This run follows a reset of the pair - sudo apt-get purge ch06-suite ch06-extra removed both, asking the usual confirmation.

This time the recommendation appears in the Recommended packages: list - shown, considered, and skipped - and only the named package is installed. The state of the skipped recommendation is worth checking, because it exercises a row of the table in Section 6.1.3:

$ dpkg-query -W -f='${Status}\n' ch06-extra
unknown ok not-installed

ch06-extra has no entry of its own in the installed database, but the name is known through ch06-suite's recommendation of it, and dpkg-query reports that state as unknown ok not-installed. A name the database has never seen at all - some misspelling - fails the query with dpkg-query: no packages found matching and a nonzero status instead. Finish the demonstration by removing the suite package as before (sudo apt-get purge ch06-suite); the pair is gone, and nothing in the rest of the chapter depends on them.

6.3.3. Removing Packages, Purging Configuration, and Autoremove

Three related verbs remove software, and the difference between them is what stays behind. remove deletes the package's files but keeps its conffiles and the database record that describes them; purge deletes those too; autoremove applies the same logic to packages that were only ever pulled in as dependencies. The fixture ch06-config ships one conffile, /etc/ch06-config.conf, which makes the whole sequence visible. Install it:

$ sudo apt-get install ch06-config
Reading package lists...
Building dependency tree...
Reading state information...
The following NEW packages will be installed:
  ch06-config
0 upgraded, 1 newly installed, 0 to remove and 28 not upgraded.
Need to get 0 B/904 B of archives.
After this operation, 0 B of additional disk space will be used.
Get:1 file:/srv/ch06-repo trixie/main amd64 ch06-config all 1.0 [904 B]
Selecting previously unselected package ch06-config.
(Reading database ... 111156 files and directories currently installed.)
Preparing to unpack .../main/ch06-config_1.0_all.deb ...
Unpacking ch06-config (1.0) ...
Setting up ch06-config (1.0) ...

Selected output; progress lines and needrestart report omitted. A single named package with no dependencies, so no confirmation prompt appears.

The conffile is on disk:

$ cat /etc/ch06-config.conf
enabled=true

Remove the package:

$ sudo apt-get remove ch06-config
Reading package lists...
Building dependency tree...
Reading state information...
The following packages will be REMOVED:
  ch06-config
0 upgraded, 0 newly installed, 1 to remove and 28 not upgraded.
After this operation, 0 B of additional disk space will be used.
Do you want to continue? [Y/n] y
(Reading database ... 111157 files and directories currently installed.)
Removing ch06-config (1.0) ...

Selected output; progress lines omitted. Removals always ask for confirmation; the zero-size disk line simply reflects how small the fixture is. Now compare what the two databases say:

$ dpkg-query -W -f='${Status}\n' ch06-config
deinstall ok config-files
$ cat /etc/ch06-config.conf
enabled=true

The package is gone from the filesystem, but dpkg keeps its record in the deinstall ok config-files state and the conffile survives - the deliberate design choice that makes remove safe when a reinstall or an upgrade is coming. Purge the record and the file:

$ sudo apt-get purge ch06-config
Reading package lists...
Building dependency tree...
Reading state information...
The following packages will be REMOVED:
  ch06-config*
0 upgraded, 0 newly installed, 1 to remove and 28 not upgraded.
After this operation, 0 B of additional disk space will be used.
Do you want to continue? [Y/n] y
(Reading database ... 111155 files and directories currently installed.)
Purging configuration files for ch06-config (1.0) ...

Selected output; progress lines omitted. The * on the package name marks a package being purged rather than merely removed.

After the purge, both checks agree that nothing is left:

$ dpkg-query -W ch06-config
dpkg-query: no packages found matching ch06-config

The query now fails - the record itself is gone, which is the visible difference between the two states. The conffile check is a plain file test:

$ test ! -e /etc/ch06-config.conf

test produces no output; it answers through its exit status, and the ! negation makes success mean "the file does not exist" - a command that prints nothing when everything is right.

The third verb works on the automatic marking from Section 6.3.2. The ch06-app fixture depends on ch06-lib; installing it pulls the library in, and removing the application orphans the library, which autoremove then offers to clean. Install the pair:

$ sudo apt-get install ch06-app
Reading package lists...
Building dependency tree...
Reading state information...
The following additional packages will be installed:
  ch06-lib
The following NEW packages will be installed:
  ch06-app ch06-lib
0 upgraded, 2 newly installed, 0 to remove and 28 not upgraded.
Need to get 0 B/1676 B of archives.
After this operation, 0 B of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 file:/srv/ch06-repo trixie/main amd64 ch06-lib all 1.0 [828 B]
Get:2 file:/srv/ch06-repo trixie/main amd64 ch06-app all 1.0 [848 B]
Selecting previously unselected package ch06-lib.
(Reading database ... 111156 files and directories currently installed.)
Preparing to unpack .../pool/main/ch06-lib_1.0_all.deb ...
Unpacking ch06-lib (1.0) ...
Selecting previously unselected package ch06-app.
Preparing to unpack .../pool/main/ch06-app_1.0_all.deb ...
Unpacking ch06-app (1.0) ...
Setting up ch06-lib (1.0) ...
Setting up ch06-app (1.0) ...

Selected output; progress lines and needrestart report omitted. The pulled-in dependency is marked automatically at install time - check the marking directly:

$ apt-mark showauto ch06-lib
ch06-lib

apt-mark showauto lists automatic packages among those named (without arguments it lists them all), and ch06-lib is in that set because it was installed to satisfy a dependency, not by request. Remove the application and watch apt-get notice:

$ sudo apt-get remove ch06-app
Reading package lists...
Building dependency tree...
Reading state information...
The following package was automatically installed and is no longer required:
  ch06-lib
Use 'sudo apt autoremove' to remove it.
The following packages will be REMOVED:
  ch06-app
0 upgraded, 0 newly installed, 1 to remove and 28 not upgraded.
After this operation, 0 B of additional disk space will be used.
Do you want to continue? [Y/n] y
(Reading database ... 111158 files and directories currently installed.)
Removing ch06-app (1.0) ...

Selected output; progress lines omitted. The notice names ch06-lib and suggests the cleanup; the library is still installed at this point, because removing the application does not cascade:

$ dpkg-query -W ch06-lib
ch06-lib    1.0

Complete the cleanup with autoremove, which is just remove driven by the automatic marking:

$ sudo apt-get autoremove --purge
Reading package lists...
Building dependency tree...
Reading state information...
The following packages will be REMOVED:
  ch06-lib*
0 upgraded, 0 newly installed, 1 to remove and 28 not upgraded.
After this operation, 0 B of additional disk space will be used.
Do you want to continue? [Y/n] y
(Reading database ... 111156 files and directories currently installed.)
Removing ch06-lib (1.0) ...

Selected output; progress lines omitted. --purge extends the cleanup to conffiles as well; plain apt-get autoremove keeps them.

Afterward, dpkg-query -W ch06-lib fails with no packages found matching ch06-lib - the same end state as the purge above, reached through the dependency graph instead of an explicit name.

apt-mark usage Description
showauto [pkg…] List automatically installed packages.
showmanual [pkg…] List manually installed packages.
auto pkg… Mark packages automatic.
manual pkg… Mark packages manual, excluding them from autoremove.
hold pkg… / unhold pkg… Freeze or release versions (Section 6.5.1).
showhold List held packages.

The marking is editable, which matters in practice: a package pulled in once as a dependency but now wanted in its own right should be apt-mark manual, or a future autoremove will take it; conversely, apt-mark auto enrolls a package in the cleanup. The state of the whole system after these verbs, at a glance:

Operation Files Conffiles Database record
remove Deleted Kept deinstall ok config-files
purge Deleted Deleted Gone
autoremove --purge Deleted, for auto packages nothing needs Deleted Gone

6.3.4. Finding Package Files and Their Owners

Two questions cover most day-to-day file ownership: "which package put this file here?" and "what did that package install?". dpkg answers both from its database - cowsay from Section 6.3.2 is still installed, so its files serve as the example:

$ dpkg -S /usr/games/cowsay
cowsay: /usr/games/cowsay

dpkg -S (search) takes a path - or a name fragment, which it matches against the tail of installed paths - and names the owning package. The reverse listing, with the grep narrowing a long inventory to the program's own files:

$ dpkg -L cowsay | grep -E "cowsay|cowthink"
/usr/games/cowsay
/usr/share/cowsay
/usr/share/cowsay/cows
/usr/share/cowsay/cows/apt.cow
[...]
/usr/share/cowsay/cows/vader.cow
/usr/share/cowsay/cows/www.cow
/usr/share/doc/cowsay
/usr/share/doc/cowsay/NEWS.Debian.gz
/usr/share/doc/cowsay/README
/usr/share/doc/cowsay/changelog.Debian.gz
/usr/share/doc/cowsay/changelog.gz
/usr/share/doc/cowsay/copyright
/usr/share/doc/cowsay/examples
/usr/share/doc/cowsay/examples/cowsay_random
/usr/share/man/man6/cowsay.6.gz
/usr/games/cowthink
/usr/share/man/man6/cowthink.6.gz

Selected output; the forty-four .cow character files between apt.cow and vader.cow are omitted. Note where the program lives: Debian puts games under /usr/games, not /usr/bin.

Both commands work only for installed packages, because dpkg's database records what was unpacked. For a package not installed - one being evaluated, or one seen on a mirror - the search goes through the repository indexes instead:

$ apt-cache search "^hello$"
hello - example package based on GNU hello

apt-cache search matches regular expressions against package names and descriptions in every configured index; the anchored ^hello$ matches the name exactly rather than every description containing the word. To inspect a package's archive without installing it, fetch it first - as john, into the downloads workspace:

$ cd ~/debian-handbook/package-management/downloads
$ apt-get download hello
Get:1 https://deb.debian.org/debian trixie/main amd64 hello amd64 2.10-5 [53.2 kB]
Fetched 53.2 kB in 1s (66.8 kB/s)

cd prints nothing; the download writes hello_2.10-5_amd64.deb into the current directory and installs nothing. Confirm the file, then read its control fields and its contents with dpkg-deb, the tool that reads .deb archives directly - the same tool the fixture script used to build them:

$ ls -1 hello_*.deb
hello_2.10-5_amd64.deb
$ dpkg-deb -f hello_*.deb Package Version Architecture Depends
Package: hello
Version: 2.10-5
Architecture: amd64
Depends: libc6 (>= 2.38)
$ dpkg-deb -c hello_*.deb | grep "usr/bin/hello"
-rwxr-xr-x root/root     35544 2025-04-16 09:50 ./usr/bin/hello
dpkg-deb usage Description
-I archive Show the control fields of an archive.
-f archive field… Print selected control fields of an archive.
-c archive List an archive's files with their modes and owners.
--root-owner-group --build dir out.deb Build an archive from a directory tree.

For searching files across all packages in the archive, Debian ships apt-file, which downloads the Contents indexes - a map from every file of every package to its owning package - and answers "which package would provide this path?" without downloading the packages themselves. It is the right tool when the file's owner is not installed anywhere; the procedures in this chapter always know the package involved, so the tool is described here rather than demonstrated.