DPKG basics

How to use the Debian Package Manager

view on github

DPKG

✔️ DPKG stands for Debian Package Manager

✔️ It is a command-line frontend to a system database maintaining the list and status of packages known to the system.

✔️ It can be used to install, remove and check the current state of a package.

✔️ A package contains program binaries and configuration files.

✔️ DPKG ships with the fllowing programs :

  1. dpkg : install, remove, build and manage packages
  2. dpkg-deb : pack, unpack and provide information on individual .deb files
  3. dpkg-query : query the dpkg system database, provide informations on packages states

✔️ DPKG CAN NOT DOWNLOAD PACKAGES...

The DPKG database

✔️ The dpkg system database is located in /var/lib/dpkg/info/.

✔️ Each package is characterized (at most) by the following files :

file usage
/var/lib/dpkg/info/package.list list of package files path
/var/lib/dpkg/info/package.md5sums list of md5 hashes for each package file
/var/lib/dpkg/info/package.postinst* post-install script
/var/lib/dpkg/info/package.postrm* post-removal script
/var/lib/dpkg/info/package.preinst* pre-install script
/var/lib/dpkg/info/package.prerm* pre-removal script

✔️ Use dpkg-query to query the database :

dpkg-query -l <package>             # prints current package(s) state in the dpkg database 
dpkg-query -s <package>             # prints installed package headers (equals to dpkg-deb -e plus installation status) 
dpkg-query -L <package>             # prints the list of installed package files (equals to dpkg-deb -c)
dpkg-query -S <path/pattern>        # retrieves the origin packages from the file(s) - use shell wildcards

Retrieve package information

✔️ dpkg-deb will only work with local .deb files :

dpkg-deb -c archive.deb             # prints the list of package files
dpkg-deb -e archive.deb /to/path    # extracts the package control files to a directory
dpkg-deb -f archive.deb             # prints the package control files
dpkg-deb -I archive.deb             # prints the package control files + archive size

Install a package

dpkg --install <package.deb>        # installs package from local .deb file

✔️ Installation consists of the following steps:

  1. Extract the control files of the new package.
  2. If a previous version of the same package is present, execute prerm (pre-remove) script of the old package.
  3. Run preinst (pre-install) script, if provided by the package.
  4. Unpack the new files, and at the same time back up the old files so that they can be restored if something goes wrong.
  5. If a previous version of the same package is present, execute the postrm (post-remove) script of the old package.
  6. Unpack the conffiles, and at the same time back up the old conffilesso that they can be restored if something goes wrong.
  7. Run postinst (post-install) script, if provided by the package.

Check a package's state

dpkg-query --list <package>         # query the system database for the status of <package> (works with multiple packages)

✔️ The dpkg information of a given package is represented by 3 letters :

  1. selection state - indicates that the package has been selected for one of the following actions :

    • i : selected for installation
    • h : ignored by dpkg (preserve a specific version)
    • d : selected for deinstallation
    • p : selected for purge (deinstallation + config files removal)
    • u : package selection is unknown and package itself may disappear from the dpkg database
  2. package state - indicates the current state of the package in the system :

    • n: not installed
    • c: only config files are present
    • H: installation was started but not completed
    • U: package is unpacked but not configured
    • F: package configuration was started but not completed
    • W: package awaits trigger processing y another package
    • t: package has been triggered
    • i: package is successfully installed (unpacking + configuration)
  3. Error state - indicates the package error state in the system :

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

Remove a package

✔️ 2 options are available :

#  removes package(s) program binaries
#  package status then becomes un or rc (if configuration files are present)
dpkg --remove <package>

#  removes package binaries as well as configuration files
#  package status then becomes un
dpkg --purge <package>