Skip to content
Tutorials Center
My learning

Topics

Linux Command Reference ยท Packages and Services

Linux dpkg command

Reference for the Linux dpkg command: install, remove and inspect .deb files, list installed packages and their files, find the owner of a file and build a package.

dpkg is the low-level package tool of Debian and its derivatives. It installs, removes and inspects individual .deb files and keeps a database of what is installed in /var/lib/dpkg/. Unlike apt, it does not download anything and does not resolve missing dependencies: it only works with the files you give it.

A .deb file is an ar archive holding a control file (name, version, dependencies, description), the files to install, and optional scripts that run before and after install or removal. Related tools are dpkg-deb (work with the archive), dpkg-query (ask the database) and dpkg-reconfigure.

Terminal
sudo dpkg -i package.deb
dpkg -l | grep PATTERN
dpkg -S /path/to/file

Try it

This block builds a tiny package, inspects it, and installs it into a private database and root directory, so nothing system-wide changes. (Real installs use sudo dpkg -i without the extra options.)

Bash Build, inspect, install and remove a package

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Bash Version arithmetic and architecture

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Syntax

Terminal
dpkg [OPTIONS] ACTION
Action Description
-i FILE.deb, --install Install or upgrade from a file.
-r PKG, --remove Remove a package, keeping its configuration.
-P PKG, --purge Remove a package and its configuration.
-l [PATTERN] List packages (state, name, version, description).
-L PKG List files installed by a package.
-S PATH Find which package owns a file.
-s PKG Show the status record of a package.
-c FILE.deb, -I FILE.deb List the contents, or show the control information, of a .deb file.
--configure -a Finish configuring packages that were left half-installed.
--compare-versions A OP B Exit 0 when version A is lt, le, eq, ne, ge or gt than B.
--print-architecture Print the native architecture, for example amd64.
--get-selections Print packages marked for installation.

Returns: 0 on success, 1 on failure of the action (and for --compare-versions, 1 when the relation is false), 2 on serious trouble such as a corrupt archive.

In dpkg -l, the first two columns are the wanted state and the actual state: ii means installed, rc removed with config left, un unknown and not installed.

Usage notes

  • Dependencies are your job. dpkg -i fails with "dependency problems" if something is missing. Run sudo apt-get install -f afterwards to fetch what is needed, or install local files with sudo apt install ./file.deb, which resolves dependencies for you.
  • Who owns this file? dpkg -S /usr/bin/ls is the quickest answer; it only finds files from installed packages and does not see files created by scripts.
  • Debian version order. Versions are [epoch:]upstream[-revision]. Numbers compare numerically (1.10 > 1.9), ~ sorts before everything (2.0~rc1 < 2.0), and a higher epoch always wins. Do not compare versions with sort or string tests.
  • Interrupted installs. If apt reports "dpkg was interrupted", run sudo dpkg --configure -a.
  • Forcing is dangerous. Options such as --force-depends or --force-overwrite bypass safety checks and can leave the system inconsistent. Prefer fixing the cause.
  • Lock file. Only one dpkg/apt runs at a time (/var/lib/dpkg/lock-frontend).
  • Building packages. dpkg-deb --build DIR is fine for a quick package; for real software use debhelper and dpkg-buildpackage. All directories under the package root need permissions 0755 or less restrictive but not setgid.
  • Logs. Actions are logged in /var/log/dpkg.log; apt keeps its own in /var/log/apt/history.log.