DPKG basics

How to use the Debian Package Manager

view on github

dpkg

Table of contents

  1. Overview
  2. The *.deb archive format
  3. The dpkg database
  4. Check package state
  5. Remove a package

Overview

  • 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 files
    dpkg-query Query the dpkg database, provide informations on packages
  • As opposed to apt, dpkg does not download packages.


The *.deb archive format

  • Debian packages are archived in *.deb files that contain a control archive and a data archive :
    1. The control archive stores metadata about the package.
    2. The data archive stores actual program binaries and configuration files.
  • Information about a package can be extracted from a *.deb archive using dpkg-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 :
    1. Extract the new package control archive.
    2. 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.
    3. Run preinst (pre-install) script of the new package.
    4. Extract the new package files.
    5. 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.
    6. Extract the new package conffiles.
    7. Run postinst (post-install) script of the new package.

The dpkg database

  • 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"

Check package state

  • 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) or un (if removed) requires attention.


Remove a package

  • 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"