DPKG basics
How to use the Debian Package Manager

-
dpkg
is the default package manager for Linux Debian distributions. -
It is a CLI frontend to a database containing the list and statuses of packages known to the system.
-
It can be used to install, remove and check the current state of a package.
-
dpkg
ships with the following programs :program usage dpkg
Install, remove, build and manage packages dpkg-deb
Pack, unpack and provide information on individual *.deb
filesdpkg-query
Query the dpkg
database, provide informations on packages -
As opposed to
apt
,dpkg
does not download packages.
- Debian packages are archived in
*.deb
files that contain a control archive and a data archive :- The control archive stores metadata about the package.
- The data archive stores actual program binaries and configuration files.
- Information about a package can be extracted from a
*.deb
archive usingdpkg-deb
:
ARCHIVE="cowsay_3.03+dfsg2-8_all.deb"
# prints package files list
dpkg-deb -c "$ARCHIVE"
# extracts the package control files to a directory
dpkg-deb -e "$ARCHIVE" ./control.files
# lists the package control files
dpkg-deb -f "$ARCHIVE"
# lists the package control files and archive size
dpkg-deb -I "$ARCHIVE"
# installs the package on the current system (see below)
dpkg --install "$ARCHIVE"
- The sequence of actions during package installation is as follows :
- Extract the new package control archive.
- If it is a newer version of an installed package :
- Run
prerm
(pre-remove) script of the old version. - Back up the files of the old version.
- Run
- Run
preinst
(pre-install) script of the new package. - Extract the new package files.
- If it is a newer version an installed package :
- Run
postrm
(post-remove) script of the old version. - Back up the
conffiles
of the old version.
- Run
- Extract the new package
conffiles
. - Run
postinst
(post-install) script of the new package.
-
The
dpkg
system database is located in the/var/lib/dpkg/info
directory. -
When a package is installed, its control archive is extracted to the directory :
control archive file usage package.list
List of absolute paths for all package files package.md5sums
List of md5 hashes for all package files package.conffiles
List of declared configuration files package.preinst
Pre-installation script package.postinst
Post-installation script package.prerm
Pre-removal script package.postrm
Post-removal script -
As a result, the package is now known to the system and "published" to the
dpkg
database. -
Information about the package can now be retrieved using
dpkg-query
:
PACKAGE="openssh-client"
# prints package headers and description (if installed)
dpkg-query -s "$PACKAGE"
# prints package files list (if installed)
dpkg-query -L "$PACKAGE"
# lists installed packages containing file paths that match a pattern
dpkg-query -S "/usr/bin/ssh*"
# prints current package state (see below)
dpkg-query -l "$PACKAGE"
-
dpkg-query -l
output represents a package state using 3 letters. -
selection state : the package is selected for one of the following actions :
pending action i
Selected for installation h
On hold: ignored by dpkg to preserve a specific version r
Selected for removal p
Selected for purge: removal and config files deletion u
Unknown: state is invalid and the package may disappear from the database -
package state : the current state of the package in the system :
Package state n
Not installed c
Only configuration files are present H
Installation started but not completed U
Package extracted but not configured F
Package configuration started but not completed W
Package awaits trigger processing by another package t
Package has been triggered i
Package is successfully extracted and configured -
error state : package error state in the system :
error state R
Package is broken and must be reinstalled -
The current state represents the package status in the system after update from the previous desired state.
-
As a rule of thumb, any package state that is not
ii
(if installed) orun
(if removed) requires attention.
- 2 options are available for uninstalling packages:
PACKAGE="openssh-client"
# removes files, state changes to "un" or "rc" if configuration files are present
dpkg --remove "$PACKAGE"
# remove files and configuration, state changes to "un"
dpkg --purge "$PACKAGE"