# dpkg — The Low-Level Debian Package Tool

> Practical guide to dpkg: install local .deb packages, query package status and contents, and repair installations — the foundation beneath apt.

Source: https://www.jpkc.com/db/en/cheatsheets/package-managers/dpkg/

<!-- PROSE:intro -->
dpkg is Debian's low-level package tool and the foundation that `apt` and `apt-get` are built on. You use it to install local `.deb` files directly, query the status of installed packages, list their contents, or find out which package a given file belongs to. Unlike `apt`, however, dpkg does **not** resolve dependencies – it processes exactly the packages you hand it. This guide walks you through the essential dpkg commands for installing, querying and repairing packages.
<!-- PROSE:intro:end -->

## Install & Remove

`dpkg -i <file>.deb` — Install a .deb package.

```bash
sudo dpkg -i package.deb
```

`dpkg -r <package>` — Remove a package (keep config files).

```bash
sudo dpkg -r nginx
```

`dpkg -P <package>` — Purge a package (remove including config files).

```bash
sudo dpkg -P nginx
```

`dpkg --configure -a` — Configure all unpacked but unconfigured packages.

```bash
sudo dpkg --configure -a
```

`dpkg --force-depends -i <file>.deb` — Force install ignoring dependency errors.

```bash
sudo dpkg --force-depends -i package.deb
```

## Query Installed Packages

`dpkg -l` — List all installed packages.

```bash
dpkg -l
```

`dpkg -l '<pattern>'` — List packages matching a pattern.

```bash
dpkg -l 'php*'
```

`dpkg -s <package>` — Show detailed status and info of a package.

```bash
dpkg -s nginx
```

`dpkg -L <package>` — List all files installed by a package.

```bash
dpkg -L nginx
```

`dpkg -S <file>` — Find which package owns a file.

```bash
dpkg -S /usr/sbin/nginx
```

`dpkg -S '<pattern>'` — Search for packages by file path pattern.

```bash
dpkg -S '*/bin/python*'
```

## Query .deb Files

`dpkg -I <file>.deb` — Show info about a .deb file (before installing).

```bash
dpkg -I package.deb
```

`dpkg -c <file>.deb` — List files inside a .deb file.

```bash
dpkg -c package.deb
```

`dpkg -x <file>.deb <dir>` — Extract files from a .deb to a directory.

```bash
dpkg -x package.deb /tmp/extract/
```

`dpkg -e <file>.deb <dir>` — Extract control info from a .deb file.

```bash
dpkg -e package.deb /tmp/control/
```

## Reconfigure & Repair

`dpkg-reconfigure <package>` — Reconfigure an installed package (re-run setup).

```bash
sudo dpkg-reconfigure tzdata
```

`dpkg-reconfigure -plow <package>` — Reconfigure showing all questions (even low priority).

```bash
sudo dpkg-reconfigure -plow locales
```

`dpkg --configure -a` — Fix partially installed packages.

```bash
sudo dpkg --configure -a
```

`apt-get install -f` — Fix broken dependencies after dpkg install.

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

## Common Patterns

`dpkg -l | grep '^ii'` — List only properly installed packages.

```bash
dpkg -l | grep '^ii'
```

`dpkg -l | grep '^rc'` — List removed packages with leftover config files.

```bash
dpkg -l | grep '^rc' | awk '{print $2}' | xargs sudo dpkg -P
```

`dpkg --get-selections` — Export package selection state (for cloning).

```bash
dpkg --get-selections > packages.txt
```

`dpkg --set-selections < packages.txt` — Import package selections (then run apt-get dselect-upgrade).

```bash
sudo dpkg --set-selections < packages.txt && sudo apt-get dselect-upgrade
```

`dpkg --compare-versions <v1> <op> <v2>` — Compare package version numbers.

```bash
dpkg --compare-versions 2.0 gt 1.5 && echo 'newer'
```

<!-- PROSE:outro -->
## Conclusion

dpkg is the bedrock of Debian package management: precise, fast and indispensable when you need to install a single `.deb` file or find out exactly which package ships a given file. The crucial catch: dpkg resolves no dependencies – a `dpkg -i` can leave your system in an inconsistent state that you then repair with `sudo apt-get -f install`. The `-r`, `-P` and `--purge` options remove packages (and, with `-P`/`--purge`, their configuration too) without asking, so double-check the package name. For everyday work – installing and upgrading packages from repositories – reach for `apt` instead; dpkg is the tool for local files, queries and emergency repairs.

## Further Reading

- [dpkg(1) – Debian manpage](https://manpages.debian.org/dpkg) – official reference for all options and actions
- [dpkg – Debian Wiki](https://wiki.debian.org/dpkg) – background on dpkg within the Debian package system
<!-- PROSE:outro:end -->

## Related Commands

- [apk](https://www.jpkc.com/db/en/cheatsheets/package-managers/apk/) – package manager for Alpine Linux
- [apt](https://www.jpkc.com/db/en/cheatsheets/package-managers/apt/) – convenient high-level front-end for Debian package management
- [apt-get](https://www.jpkc.com/db/en/cheatsheets/package-managers/apt-get/) – classic, script-friendly APT tool

