Loading...

Lab 117: Debian Package Management with dpkg

Manage Debian packages directly with dpkg: query status, list installed packages, enumerate files owned by a package, and identify file ownership. Install a local .deb, recover cleanly with APT when dependencies break, then remove and purge to understand the full lifecycle.

packages debian core

Scenario

You are working on a Debian-based system where package work needs to be done at the dpkg level. You must verify package state, determine which package owns a given file, and install a local .deb artifact. When the install leaves the package unconfigured due to dependency issues, you will use APT to correct the system, then remove and fully purge the package.

Operator context

dpkg installs packages but does not resolve dependencies. In real ops, you often pair dpkg with APT to complete the transaction and return the system to a consistent state.

Objective

  • Query package status using dpkg -s.
  • List installed packages using dpkg -l.
  • List files installed by a package using dpkg -L.
  • Identify which package owns a path using dpkg -S.
  • Install a local .deb using dpkg -i.
  • Repair dependency issues using apt-get -f install.
  • Remove a package while keeping config using dpkg -r.
  • Purge a package including config using dpkg -P.

Concepts

  • dpkg is the low-level package tool for the local package database and .deb files.
  • dpkg does not fetch dependencies. APT is the dependency resolver and installer frontend.
  • A “broken” install typically means packages are unpacked but not configured. You restore consistency with a “fix” install.
  • Remove vs purge matters. Remove keeps configuration remnants and purge deletes them.

Walkthrough

Step 1 : Show the status of the bash package.
Command
dpkg -s bash

dpkg -s queries the dpkg database for a package and reports its status. This is how you confirm whether a package is installed and in a healthy configured state.

Package: bash
Status: install ok installed
Priority: required
Version: 5.1-2ubuntu3
Step 2 : List the first 10 installed packages.
Command
dpkg -l | head -n 10

dpkg -l lists packages known to dpkg and their desired/actual state. This is useful for quick audits and confirming whether a package is installed, removed, or purged.

Desired=Unknown/Install/Remove/Purge/Hold
||/ Name           Version       Architecture Description
ii  adduser        3.118         all          add and remove users and groups
ii  apt            2.4.5         amd64        commandline package manager
ii  base-files     12ubuntu4     amd64        Debian base system miscellaneous files
ii  bash           5.1-2ubuntu3  amd64        GNU Bourne Again SHell
Step 3 : List a few files installed by the bash package.
Command
dpkg -L bash | head -n 20

dpkg -L enumerates paths installed by a package. Use this to locate binaries, docs, and man pages and to confirm exactly what a package put on disk.

/.
 /bin/bash
 /usr/share/doc/bash/README.gz
 /usr/share/man/man1/bash.1.gz
Step 4 : Identify which package owns /bin/bash.
Command
dpkg -S /bin/bash

dpkg -S maps a file path back to the owning package. This is foundational for troubleshooting and for answering “where did this file come from?”

bash: /bin/bash
Step 5 : Install a local .deb file with dpkg.
Note

This step demonstrates a common dpkg outcome: the package is unpacked, but configuration fails because dependencies are missing. That is expected when you bypass APT.

Command
sudo dpkg -i htop_3.0.5-1_amd64.deb

dpkg -i installs a local package file but does not fetch dependencies. If dependencies are missing, dpkg leaves the package unconfigured until the system is fixed.

Selecting previously unselected package htop.
(Reading database ... 123456 files and directories currently installed.)
Preparing to unpack htop_3.0.5-1_amd64.deb ...
Unpacking htop (3.0.5-1) ...
dpkg: dependency problems prevent configuration of htop:
 htop depends on libc6 (>= 2.34); however:
  Package libc6 is not configured yet.
dpkg: error processing package htop (--install):
 dependency problems - leaving unconfigured
Step 6 : Fix missing dependencies after a dpkg install.
Command
sudo apt-get -f install

This repairs broken installs by resolving dependencies and completing configuration. This is the standard recovery path when dpkg leaves packages unconfigured.

Reading package lists... Done
Building dependency tree... Done
Correcting dependencies... Done
The following additional packages will be installed:
  libc6
Setting up libc6 ...
Setting up htop (3.0.5-1) ...
Step 7 : Confirm htop is now configured (or removed if APT chose that path).
Command
dpkg -s htop | egrep '^(Package|Status|Version)'

After a fix install, verify the package status. You want to see an “install ok installed” style state for a clean install.

Package: htop
Status: install ok installed
Version: 3.0.5-1
Step 8 : Remove the package but keep configuration files.
Command
sudo dpkg -r htop

dpkg -r removes the package but preserves configuration files. This is useful when you might reinstall later and want prior settings retained.

(Reading database ... 123460 files and directories currently installed.)
Removing htop (3.0.5-1) ...
Processing triggers for man-db ...
Step 9 : Purge the package including configuration files.
Command
sudo dpkg -P htop

dpkg -P purges configuration files and removes package traces. Use this for a clean removal when config should not persist.

(Reading database ... 123450 files and directories currently installed.)
Purging configuration files for htop (3.0.5-1) ...

Common breakpoints

Your system is not Debian-based

If dpkg is not found, you are likely on an RPM-based distro. Use the equivalent tooling (rpm, dnf) or run this lab inside a Debian/Ubuntu VM/container.

dpkg install fails due to missing file

This lab assumes the htop_3.0.5-1_amd64.deb file is already present in your working directory. In real ops, you confirm the artifact path before running dpkg -i.

apt-get -f install removes the package

APT may choose removal if it cannot resolve dependencies cleanly. If that happens, treat it as the outcome and verify the dpkg status, then proceed with remove/purge steps as needed.

Cleanup checklist

Your goal is to leave the system in a clean state with no leftover dpkg locks and no partially installed packages.

Commands
dpkg -l | grep -E '^(iU|iF|iH|rc)' || true
dpkg -s htop 2>/dev/null | egrep '^(Package|Status|Version)' || true
Success signal

No “half-installed/unconfigured” packages remain, and the example package is either fully installed and configured or fully removed/purged as intended.

Reference

  • dpkg -s <pkg> : Show package status and metadata.
  • dpkg -l : List packages and their state (installed/removed/purged).
  • dpkg -L <pkg> : List files installed by a package.
  • dpkg -S <path> : Identify which installed package owns a path.
  • dpkg -i <file.deb> : Install a local .deb (no dependency resolution).
  • apt-get -f install : Fix broken installs by resolving dependencies and completing configuration.
  • dpkg -r <pkg> : Remove a package but keep configuration files.
  • dpkg -P <pkg> : Purge a package including configuration files.