# Use Debian package management

> Install, upgrade, remove and inspect .deb packages with dpkg, apt-get, apt-cache and apt, and find which package holds a file.

Source: https://aminbytes.dev/bytes/installation-and-packages/debian-packages/


On Debian, Ubuntu, Linux Mint and their relatives, almost every piece of software arrives as a **.deb package**. Packages bring tidy installs, clean removals, automatic security updates, and a database that knows which file belongs to what. You use the tools every day, and the exam goes deep: commands, options and status codes.

There are two layers:

- **`dpkg`** is the low-level tool. It installs, removes and queries `.deb` files that are already on your disk. It **doesn't download anything and doesn't resolve dependencies**.
- **APT** (`apt-get`, `apt-cache`, `apt`) sits on top. It downloads packages from repositories, works out dependencies and then calls `dpkg` to do the actual install.

Follow along in a container: `docker run -it --rm debian` (you're root there, so drop the `sudo`). Run `apt-get update` first: container images ship without a package list.

## What a .deb is

A package file name tells you a lot:

```text
nginx_1.22.1-9_amd64.deb
  │      │    │   └ architecture (amd64, arm64, i386, or all)
  │      │    └ Debian revision of the packaging
  │      └ upstream version
  └ package name
```

Inside is a compressed archive of files to install, plus control data: the description, dependencies (`Depends`, `Recommends`, `Conflicts`), and scripts that run before and after install or removal. `dpkg` records everything it installs in its database under **`/var/lib/dpkg/`**: `status` holds the state of every package and `info/<package>.list` the files each one owns.

## dpkg: working with .deb files

| Command | What it does |
| --- | --- |
| `dpkg -i file.deb` | Install (or upgrade) a package file |
| `dpkg -r package` | Remove a package, **keep** its config files |
| `dpkg -P package` | Purge: remove it **and** its config files |
| `dpkg -l [pattern]` | List packages with their status |
| `dpkg -s package` | Status and details of an installed package |
| `dpkg -L package` | List the files an installed package owns |
| `dpkg -S /path/or/pattern` | Find which installed package owns a file |
| `dpkg -I file.deb` | Show the control info of a package file (not installed) |
| `dpkg -c file.deb` | List the contents of a package file |
| `dpkg -V [package]` | Verify installed files against their checksums |
| `dpkg --configure -a` | Finish configuring packages left half-done |
| `dpkg --get-selections` | List all packages and their selection state |

Install a downloaded package, then see its status:

```bash
apt-get download tree        # saves tree_2.1.0-1_amd64.deb in the current folder
dpkg -I tree_*.deb
sudo dpkg -i tree_*.deb
dpkg -l tree
```

```text
Selecting previously unselected package tree.
(Reading database ... 6090 files and directories currently installed.)
Preparing to unpack tree_2.1.0-1_amd64.deb ...
Unpacking tree (2.1.0-1) ...
Setting up tree (2.1.0-1) ...

Desired=Unknown/Install/Remove/Purge/Hold
| Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend
|/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)
||/ Name           Version      Architecture Description
+++-==============-============-============-=====================================
ii  tree           2.1.0-1      amd64        displays an indented directory tree, in color
```

The first two letters matter. The first is what you **want** (`i` install, `r` remove, `p` purge, `h` hold, `u` unknown), the second what the package **is** (`i` installed, `c` only config files left, `n` not installed, `U` unpacked, `F` half-configured, `H` half-installed). So:

- `ii`: installed and fine.
- `rc`: removed, config files still there. `dpkg -P` or `apt-get purge` cleans it up.
- `iU` or `iF`: an install broke halfway. `dpkg --configure -a` usually finishes it.

`dpkg -s bash` shows the same state in words (`Status: install ok installed`), plus version, size, dependencies and description.

When `dpkg -i` fails because a dependency is missing, the package is left unconfigured. Let APT fetch what's missing:

```bash
sudo apt-get install -f
```

## dpkg-reconfigure

Many packages ask questions while they install, like your timezone, keyboard layout or locale. A system called **debconf** asks them and stores the answers. **`dpkg-reconfigure`** asks them again for a package that's already installed and applies the new answers:

```bash
sudo dpkg-reconfigure tzdata           # pick a new timezone
sudo dpkg-reconfigure locales          # choose and generate locales
sudo dpkg-reconfigure -plow keyboard-configuration   # show every question, even the minor ones
```

## apt-get: install from repositories

| Command | What it does |
| --- | --- |
| `apt-get update` | Download fresh package lists from the repositories. Do this first. |
| `apt-get upgrade` | Upgrade installed packages, never adding or removing any |
| `apt-get dist-upgrade` | Upgrade and allow adding or removing packages (needed for new kernels, release upgrades) |
| `apt-get install pkg` | Install a package and its dependencies. Also upgrades a single package. |
| `apt-get install ./file.deb` | Install a local file and fetch its dependencies |
| `apt-get install --reinstall pkg` | Reinstall the same version |
| `apt-get remove pkg` | Remove, keep config files |
| `apt-get purge pkg` | Remove including config files |
| `apt-get autoremove` | Remove dependencies nothing needs any more |
| `apt-get clean` / `autoclean` | Empty the download cache `/var/cache/apt/archives/` (`autoclean`: only files that can't be downloaded again) |
| `apt-get download pkg` | Download the `.deb` to the current folder without installing |
| `apt-get source pkg` | Download the source package (needs `deb-src` lines) |

Useful options: `-y` (answer yes), `-s` (simulate: show what would happen), `-d` (download only), `--no-install-recommends`.

```bash
sudo apt-get update
sudo apt-get install curl
```

```text
Reading package lists... Done
Building dependency tree... Done
The following additional packages will be installed:
  ca-certificates libcurl4 libssl3 ...
The following NEW packages will be installed:
  ca-certificates curl libcurl4 libssl3 ...
0 upgraded, 21 newly installed, 0 to remove and 0 not upgraded.
Need to get 4,912 kB of archives.
Do you want to continue? [Y/n]
```

To stop a package from being upgraded, put it on **hold** with `sudo apt-mark hold nginx` (and `apt-mark unhold` to release it).

## apt-cache: ask the package lists

`apt-cache` reads the local copy of the repository lists, so it needs no root and works for packages you haven't installed.

| Command | What it shows |
| --- | --- |
| `apt-cache search word` | Packages whose name or description matches |
| `apt-cache show pkg` | Full details: version, size, dependencies, description |
| `apt-cache showpkg pkg` | Versions, dependencies and reverse dependencies, in raw form |
| `apt-cache depends pkg` | What the package needs |
| `apt-cache rdepends pkg` | What needs the package |
| `apt-cache policy pkg` | Installed version, candidate version, and which repository each comes from |
| `apt-cache stats` | Numbers about the package cache |

```bash
apt-cache policy curl
```

```text
curl:
  Installed: (none)
  Candidate: 7.88.1-10+deb12u8
  Version table:
     7.88.1-10+deb12u8 500
        500 http://deb.debian.org/debian bookworm/main amd64 Packages
        500 http://deb.debian.org/debian-security bookworm-security/main amd64 Packages
```

## apt: the friendly front end

**`apt`** combines the most used parts of `apt-get` and `apt-cache` with a progress bar and color. It's what you'll type day to day; scripts should keep using `apt-get`, whose output stays stable.

| apt | Older equivalent |
| --- | --- |
| `apt update`, `apt upgrade`, `apt install`, `apt remove`, `apt purge`, `apt autoremove` | `apt-get` with the same word |
| `apt full-upgrade` | `apt-get dist-upgrade` |
| `apt search`, `apt show` | `apt-cache search`, `apt-cache show` |
| `apt list --installed`, `apt list --upgradable` | `dpkg -l`, `apt-get -s upgrade` |

One difference: `apt upgrade` may install new dependencies, while `apt-get upgrade` never does. **aptitude** is another, older front end with a text-mode menu; you may see it mentioned.

## Repositories: /etc/apt/sources.list

APT downloads from the repositories listed in **`/etc/apt/sources.list`** and in files under **`/etc/apt/sources.list.d/`**. The classic one-line format:

```text {title="/etc/apt/sources.list"}
deb     http://deb.debian.org/debian bookworm main contrib non-free non-free-firmware
deb     http://deb.debian.org/debian-security bookworm-security main
deb-src http://deb.debian.org/debian bookworm main
```

Each line is: type (`deb` for binary packages, `deb-src` for source), the mirror URL, the **suite** (release name like `bookworm` or `noble`, or `stable`), then one or more **components**. Debian has `main` (free software), `contrib`, `non-free` and `non-free-firmware`. Ubuntu uses `main`, `restricted`, `universe` and `multiverse`.

Newer systems use the multi-line **deb822** format in `.sources` files instead. Ubuntu 24.04 ships `/etc/apt/sources.list.d/ubuntu.sources`, and the Debian container has `debian.sources`:

```text {title="/etc/apt/sources.list.d/ubuntu.sources"}
Types: deb
URIs: http://archive.ubuntu.com/ubuntu/
Suites: noble noble-updates noble-backports
Components: main restricted universe multiverse
Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg
```

After changing either, run `apt-get update`. APT checks the repository's GPG signature on every update and refuses lists it can't verify. `Signed-By` (or `[signed-by=...]` in one-line format) names the key that's allowed to sign that repository. Extra settings for APT itself go in `/etc/apt/apt.conf.d/`.

## Find which package has a file

For **installed** packages, `dpkg` knows every file:

```bash
dpkg -S libz.so.1
dpkg -L zlib1g
```

```text
zlib1g:amd64: /lib/x86_64-linux-gnu/libz.so.1.2.13
zlib1g:amd64: /lib/x86_64-linux-gnu/libz.so.1
/.
/lib
/lib/x86_64-linux-gnu
/lib/x86_64-linux-gnu/libz.so.1.2.13
/usr
/usr/share
/usr/share/doc
...
```

> [!TIP]
> On "merged /usr" systems, `/bin` is a link to `/usr/bin`, and older packages still record the `/bin` path. If `dpkg -S /usr/bin/ls` says `no path found`, try `dpkg -S /bin/ls`, or a pattern that matches both: `dpkg -S '*bin/ls'`.

For packages that **aren't installed**, use **`apt-file`**, which downloads the file lists of the whole repository:

```bash
sudo apt-get install apt-file
sudo apt-file update
apt-file search libssl.so.3
```

```text
libssl3: /usr/lib/x86_64-linux-gnu/libssl.so.3
```

You can also search on [packages.debian.org](https://packages.debian.org) or [packages.ubuntu.com](https://packages.ubuntu.com).

## Check a package's integrity

`dpkg -V` (or `--verify`) compares installed files with the checksums recorded at install time. No output means nothing changed:

```bash
sudo dpkg -V openssh-server
```

```text
??5?????? c /etc/ssh/sshd_config
```

The `5` means the file's checksum differs and `c` marks a config file, so here someone simply edited `sshd_config`. Changes to files that aren't config files deserve a closer look. The `debsums` package offers the same check with friendlier output. (Side note: `dpkg -V` borrowed its output format from `rpm -V`, which you'll meet in the next byte. The `?` marks are checks dpkg doesn't do, like file owner and permissions.)

## Exam checklist

- `dpkg` works on local `.deb` files with no dependency resolution. APT downloads and resolves dependencies.
- `dpkg -i` install, `-r` remove (keeps config), `-P` purge, `-l` list, `-s` status, `-L` files of a package, `-S` package of a file, `-I` and `-c` on a `.deb` file, `-V` verify, `--configure -a` finish.
- `dpkg -l` status: `ii` installed, `rc` removed with config left.
- `dpkg-reconfigure pkg` asks the debconf questions again (`tzdata`, `locales`).
- `apt-get update` refreshes lists; `upgrade` never adds or removes packages, `dist-upgrade` may. `install -f` fixes broken dependencies. `remove` vs `purge`, `autoremove`, `clean`.
- `apt-cache search`, `show`, `showpkg`, `depends`, `rdepends`, `policy`.
- `apt` is the interactive front end: `apt full-upgrade` = `apt-get dist-upgrade`, plus `apt list --installed`.
- Repositories: `/etc/apt/sources.list` and `/etc/apt/sources.list.d/`, lines like `deb URL suite components`. Run `apt-get update` after a change.
- Package of a file: `dpkg -S` (installed), `apt-file search` (not installed).


## Byte check

1. You ran `dpkg -i app.deb` and it failed with unmet dependencies. What's the quickest fix?
   - **`sudo apt-get install -f`** (correct)
   - `sudo dpkg-reconfigure app`
   - `sudo apt-cache depends app`
   `dpkg` doesn't fetch dependencies. `apt-get install -f` (fix broken) downloads what's missing and finishes the half-done install.

2. Which command tells you which **installed** package owns `/usr/bin/curl`?
   - `dpkg -L curl`
   - **`dpkg -S /usr/bin/curl`** (correct)
   - `apt-cache search curl`
   `dpkg -S` searches the installed files for a path. `dpkg -L` goes the other way, listing the files of a package you name. For packages that aren't installed, use `apt-file search`.

3. `dpkg -l nginx` shows `rc` at the start of the line. What does that mean?
   - Installed and running
   - **Removed, but its config files are still there** (correct)
   - Waiting to be reconfigured
   `r` = desired state remove, `c` = only config files left. That's what `dpkg -r` or `apt-get remove` leaves behind; `purge` removes the config files too.


