Skip to content

102.4 Use Debian package management

Concept of the package management system

GNU/Linux distributions maintain large archives of pre-compiled software called repositories, managed by package manager tools that handle searching, installing, resolving dependencies, resolving conflicts, and updating. Debian-based distros use .deb files as their packages and tools like dpkg, apt-get, apt-cache, and apt to manage them.

Debian packages are named like NAME-VERSION-RELEASE_ARCHITECTURE.deb, for example tmux_3.2a-4build1_amd64.deb.

Package management layers:

  apt-get / apt-cache / apt       (high-level: repositories, dependencies, updates)
        |
        v
  dpkg                            (low-level: install/remove individual .deb files)
        |
        v
  .deb files on disk

dpkg works on individual .deb files but cannot resolve dependencies. apt-get/apt works with repositories and handles dependencies automatically. Think of dpkg as the engine and apt as the driver.

apt vs apt-get

apt is a newer, "friendlier" command that combines the most used options of apt-get and apt-cache into one tool. Most commands are interchangeable:

Task apt-get / apt-cache apt
Update package list apt-get update apt update
Install a package apt-get install tmux apt install tmux
Remove a package apt-get remove tmux apt remove tmux
Upgrade all packages apt-get upgrade apt upgrade
Search for a package apt-cache search tmux apt search tmux
Show package info apt-cache show tmux apt show tmux
Fix broken packages apt-get install -f apt install -f

Why learn apt-get if apt exists? The PDF states: since apt may not be installed on every system, it is recommended to learn apt-get and apt-cache. On minimal or older systems, apt-get is guaranteed to be there; apt might not be. Also, apt-get is more stable for use in scripts, while apt adds features like a progress bar that are designed for interactive (human) use.


Repositories

Repositories are where packages come from - a network server, local disk, or even a DVD. The configuration lives at:

  • /etc/apt/sources.list - the main sources file
  • /etc/apt/sources.list.d/ - additional .list files (one per extra repo)

A typical sources.list line:

deb http://us.archive.ubuntu.com/ubuntu/ jammy main restricted universe multiverse

The format is: archive-type URL distribution component(s)

Field Meaning
deb or deb-src Binary packages or source code
URL Repository address
Distribution Release codename (e.g. jammy for Ubuntu 22.04)
Components main (official open-source), restricted (official closed-source), universe (community open-source), multiverse (unsupported/patent-encumbered)

Updating repository information (does NOT upgrade any software, just refreshes the list of what's available):

apt-get update

Installing packages

apt-get install tmux                  # install a package (resolves dependencies automatically)
apt-get install -s tmux               # dry-run/simulation only, shows what would happen
apt-get install --download-only tmux  # download .deb files to cache without installing
apt-get download tmux                 # download the .deb to current directory

Downloaded packages are cached in /var/cache/apt/archives/.


Removing debian packages

apt-get remove tmux            # remove the package, keep config files
apt-get autoremove tmux        # remove the package AND its unused dependencies
apt-get autoremove             # remove any dependencies no longer needed by anything

Notes: - remove alone does NOT remove dependencies that were pulled in automatically - If you remove a package that other packages depend on, apt will warn you and list what else will be removed

Note on "removing 100%": dpkg -P tmux (purge) removes tmux and its config files, but leaves its dependencies behind. apt autoremove tmux removes tmux and its unused dependencies, but leaves the config files behind. Neither one alone does both jobs.

sudo apt autoremove --purge tmux    # one command: configs gone, unused deps gone

This one line does both. But it only checks tmux's own dependencies at that moment. For a full clean-up (including old leftovers from other past removals), two separate commands work better:

sudo apt purge tmux        # remove tmux + its config files
sudo apt autoremove        # clean up ANY unused dependency on the whole system

Searching for packages

apt-cache search "tiny window"     # search package names and descriptions
apt search grub2                   # same thing using the newer apt command

Upgrading

apt-get install tzdata       # upgrade a single package (install and upgrade use the same command)
apt-get upgrade              # upgrade all installed packages to latest versions
apt-get dist-upgrade         # upgrade to a new distribution release (may add/remove packages)

The default apt configuration is at /etc/apt/apt.conf, manageable via apt-config.


Reconfiguring packages

Debian packages can run configuration actions after installation (via debconf). To re-run that configuration on an already installed package:

dpkg-reconfigure tzdata      # re-asks the timezone questions as if you just installed it

Useful when a config file is corrupted or you want to change answers you gave during initial setup.


Package information with dpkg

dpkg is the low-level tool for working directly with .deb files. Format: dpkg [OPTIONS] ACTION PACKAGE

Switch Long form Description
-c --contents Show the contents of a .deb file (what files it would install)
-C --audit Search for broken installed packages and propose solutions
--configure Reconfigure an installed package
-i --install Install or upgrade a .deb file. Does NOT resolve dependencies
-I --info Show detailed info about a .deb file (version, dependencies, description)
-l --list List all installed packages matching a pattern
-L --listfiles List all files installed by a specific package
-P --purge Remove the package AND its configuration files (stronger than -r)
-r --remove Remove the package but keep its configuration files
-s --status Display the current status of an installed package
-S --search Find which installed package owns a given file

Examples:

dpkg --contents tmux_3.2a-4build1_amd64.deb   # peek inside a .deb without installing
dpkg -i tmux_3.2a-4build1_amd64.deb           # install from a local .deb file
dpkg -s tmux                                   # is tmux installed? what version?
dpkg -L tmux                                   # what files did tmux install?
dpkg -S /usr/bin/tmux                          # which package owns /usr/bin/tmux?
dpkg -P tmux                                   # remove tmux AND its config files

Key difference: -r vs -P - -r (remove): deletes the program but leaves config files behind. If you reinstall later, your old settings are still there - -P (purge): deletes everything, program and configs. Clean slate

Key difference: -i vs apt-get install - dpkg -i installs one .deb file, does NOT resolve dependencies (fails if they're missing) - apt-get install fetches from repositories AND resolves all dependencies automatically

If a dpkg -i leaves broken dependencies, fix them with:

apt-get install -f     # -f = fix broken

Common apt-get options

Command Usage
autoclean Remove cached .deb files for packages that are no longer available
check Check the package database for consistency issues
clean Delete all cached .deb files from /var/cache/apt/archives/ to free disk space
dist-upgrade Major upgrade - can add/remove packages to satisfy new dependencies
install Install or upgrade packages
remove Remove a package (keep configs)
source Download the source code of a package
update Refresh the package list from repositories (NOT upgrade software)
upgrade Upgrade all installed packages to latest versions (won't remove anything)

Common apt-cache options

Command Usage
depends Show what a package depends on
pkgnames List all known package names
search Search package names and descriptions
showpkg Show detailed info about a package
stats Show repository statistics
unmet Show unmet dependencies

Other tools

Other tools include aptitude (text-based interactive package manager) and graphical tools used in KDE and GNOME desktop environments.


Summary

I have a Debian-based Linux system where software is distributed as .deb packages. There are two layers of tools: dpkg at the bottom works directly with individual .deb files (install with -i, remove with -r, purge with -P, query with -s/-S/-L), but it cannot resolve dependencies on its own. Above it, apt-get and apt-cache work with online repositories defined in /etc/apt/sources.list, automatically downloading packages and their dependencies.

The workflow I'll use most often is apt-get update to refresh the package list, then apt-get install to install, apt-get upgrade to upgrade all packages, and apt-get remove or autoremove to clean up. If a package has post-install configuration prompts I need to change later, dpkg-reconfigure re-runs them. If a dpkg -i leaves a package in a broken state with missing dependencies, apt-get install -f fixes it.

The one naming trap to watch for is update vs upgrade: apt-get update only refreshes the list of available packages (like checking what's new at the store), while apt-get upgrade actually installs the newer versions (like buying and bringing them home). Running upgrade without update first means you're upgrading based on stale information.