DPKG basics
How to use the Debian Package Manager
-
dpkgis 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.
-
dpkgships with the following programs :program usage dpkgInstall, remove, build and manage packages dpkg-debPack, unpack and provide information on individual *.debfilesdpkg-queryQuery the dpkgdatabase, provide informations on packages -
As opposed to
apt,dpkgdoes not download packages.
- Debian packages are archived in
*.debfiles 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
*.debarchive 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
conffilesof the old version.
- Run
- Extract the new package
conffiles. - Run
postinst(post-install) script of the new package.
-
The
dpkgsystem database is located in the/var/lib/dpkg/infodirectory. -
When a package is installed, its control archive is extracted to the directory :
control archive file usage package.listList of absolute paths for all package files package.md5sumsList of md5 hashes for all package files package.conffilesList of declared configuration files package.preinstPre-installation script package.postinstPost-installation script package.prermPre-removal script package.postrmPost-removal script -
As a result, the package is now known to the system and "published" to the
dpkgdatabase. -
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 -loutput represents a package state using 3 letters. -
selection state : the package is selected for one of the following actions :
pending action iSelected for installation hOn hold: ignored by dpkg to preserve a specific version rSelected for removal pSelected for purge: removal and config files deletion uUnknown: state is invalid and the package may disappear from the database -
package state : the current state of the package in the system :
Package state nNot installed cOnly configuration files are present HInstallation started but not completed UPackage extracted but not configured FPackage configuration started but not completed WPackage awaits trigger processing by another package tPackage has been triggered iPackage is successfully extracted and configured -
error state : package error state in the system :
error state RPackage 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"