Linux dpkg command

Updated: 08/16/2021 by Computer Hope
dpkg command

On Linux operating systems that use Debian package management, the dpkg command queries, installs, removes, and maintains Debian software packages and their dependencies.

Description

The primary and more user-friendly front-end for dpkg is aptitude. dpkg itself is controlled entirely via command line parameters, which consist of exactly one action and zero or more options. The action parameter tells dpkg what to do and options control the behavior of the action in some way.

dpkg can also be used as a front-end to dpkg-deb and dpkg-query. The list of supported actions is below (in the "Actions" section). If any such action is encountered, dpkg runs dpkg-deb or dpkg-query with the parameters given to it, but no specific options are currently passed to them. To use any such option, the back-ends need to be called directly.

Syntax

dpkg [option...] action

Actions

-i,
--install package-file...
Install the package. If --recursive or -R option is specified, package-file must refer to a directory instead.

Installation consists of the following steps:

1. Extract the control files of the new package.

2. If another version of the same package was installed before the new installation, execute prerm script of the old package.

3. Run preinst script, if provided by the package.

4. Unpack the new files, and at the same time back up the old files, so that if something goes wrong, they can be restored.

5. If another version of the same package was installed before the new installation, execute the postrm script of the old package. Note that this script is executed after the preinst script of the new package, because new files are written at the same time old files are removed.

6. Configure the package. See --configure for detailed information about how this is done.
--unpack package-file... Unpack the package, but don't configure it. If --recursive or -R option is specified, package-file must refer to a directory instead.
--configure package... | -a | --pending Configure a package that was unpacked but not yet configured. If -a or --pending is given instead of package, all unpacked but unconfigured packages are configured.

To reconfigure a package which has already been configured, try the dpkg-reconfigure command instead.

Configuring consists of the following steps:

1. Unpack the conffiles, and at the same time back up the old conffiles, so that they can be restored if something goes wrong.

2. Run postinst script, if provided by the package.
--triggers-only package... | -a | --pending Processes only triggers. All pending triggers are processed. If package names are supplied, only those packages' triggers are processed, exactly once each where necessary. Use of this option may leave packages in the improper triggers-awaited and triggers-pending states. This can be fixed later by running: dpkg --configure --pending.
-r, --remove, -P, --purge package... | -a | --pending Remove an installed package. -r or --remove removes everything except conffiles. This may avoid having to reconfigure the package if it's reinstalled later. (Conffiles are configuration files that are listed in the DEBIAN/conffiles control file). -P or --purge removes everything, including conffiles. If -a or --pending is given instead of a package name, then all packages unpacked, but marked to be removed or purged in file /var/lib/dpkg/status, are removed or purged, respectively. Note: some configuration files might be unknown to dpkg because they are created and handled separately through the configuration scripts. In that case, dpkg won't remove them by itself, but the package's postrm script (which is called by dpkg), has to take care of their removal during purge. Of course, this only applies to files in system directories, not configuration files written to individual users' home directories.

Removal of a package consists of the following steps:

1. Run prerm script.

2. Remove the installed files.

3. Run postrm script.
--update-avail,
--merge-avail Packages-file
Update dpkg's and dselect's information regarding which packages are available. With action --merge-avail, old information is combined with information from Packages-file. With action --update-avail, old information is replaced with the information in the Packages-file.

The Packages-file distributed with Debian are named Packages. dpkg keeps its record of available packages in /var/lib/dpkg/available.

A simpler command to retrieve and update the available file is dselect update. Note that this file is mostly useless if you don't use dselect but an APT-based frontend: APT has its own system to keep track of available packages.
-A,
--record-avail package-file...
Update dpkg and dselect's information that packages are available with information from the package package-file. If --recursive or -R option is specified, package-file must refer to a directory instead.
--clear-avail Erase the existing information about what packages are available.
-C, --audit Searches for packages that were installed only partially on your system. dpkg suggests what to do with them to get them working.
--get-selections [package-name-pattern...] Get list of package selections, and write it to stdout. Without a pattern, packages not installed (i.e., those that were previously purged) are not shown.

--set-selections

Set package selections using file read from stdin. This file should be in the format 'package state', where state is one of install, hold, deinstall or purge. Blank lines and comment lines beginning with '#' are also permitted.
--clear-selections Set the requested state of every non-essential package to deinstall. This is intended to be used immediately before --set-selections, to deinstall any packages not in list given to --set-selections.
--yet-to-unpack Searches for packages selected for installation, but which for some reason still haven't been installed.
--add-architecture architecture Add architecture to the list of architectures for which packages can be installed without using --force-architecture. The architecture dpkg is built for (i.e., the output of --print-architecture) is always part of that list.
--remove-architecture architecture Remove architecture from the list of architectures for which packages can be installed without using --force-architecture. If the architecture is currently in use in the database, then the operation is refused, except if --force-architecture is specified. The architecture dpkg is built for (i.e., the output of --print-architecture) can never be removed from that list.
--print-architecture Print architecture of packages dpkg installs (for example, "i386").
--print-foreign-architectures Print a newline-separated list of the extra architectures dpkg is configured to allow packages to be installed for.
--compare-versions ver1 op ver2 Compare version numbers, where op is a binary operator. dpkg returns success (zero result) if the specified condition is satisfied, and failure (nonzero result) otherwise. There are two groups of operators, which differ in how they treat an empty ver1 or ver2. These treat an empty version as earlier than any version: lt le eq ne ge gt. These treat an empty version as later than any version: lt-nl le-nl ge-nl gt-nl. These are provided only for compatibility with control file syntax: <, <<, <=, =, >=, >>, >.
-?, --help Display a brief help message.
--force-help Give help about the --force-something options.
-Dh, --debug=help Give help about debugging options.
--version Display dpkg version information.

Options

All options can be specified both on the command line and in the dpkg configuration file /etc/dpkg/dpkg.cfg or fragment files (with names matching this shell pattern '[0-9a-zA-Z_-]*') on the configuration directory /etc/dpkg/dpkg.cfg.d/. Each line in the configuration file is either an option (exactly the same as the command line option but without leading dashes) or a comment (if it starts with a #).

--abort-after=number Change after how many errors dpkg aborts. The default is 50.
--abort-after=number Change after how many errors dpkg aborts. The default is 50.
-B,
--auto-deconfigure
When a package is removed, there is a possibility that another installed package depended on the removed package. Specifying this option causes automatic deconfiguration of the package which depended on the removed package.
-Doctal,
--debug=octal
Switch debugging on. The octal is formed by bitwise-OR desired values together from the list below (note that these values may change in future releases). -Dh or --debug=help display these debugging values.
Number Description
1 Generally helpful progress information
2 Invocation and status of maintainer scripts
10 Output for each file processed
100 Lots of output for each file processed
20 Output for each configuration file
200 Lots of output for each configuration file
40 Dependencies and conflicts
400 Lots of dependencies/conflicts output
10000 Trigger activation and processing
20000 Lots of output regarding triggers
40000 Silly amounts of output regarding triggers
1000 Lots of info about e.g., the dpkg/info dir
2000 Insane amounts of info
--force-things,
--no-force-things,
--refuse-things
Force or refuse (no-force and refuse mean the same thing) to do some things. The things is a comma separated list of things specified below. --force-help displays a message describing them. Things marked with (*) are forced by default.

Warning: These options are mostly intended to be used by experts only. Using them without fully understanding their effects may break your whole system.

all: Turns on (or off) all force options.

downgrade: Install a package, even if newer version of it is already installed.

Warning: At present, dpkg does not do any dependency checking on downgrades and therefore does not warn you if the downgrade breaks the dependency of some other package. This can have serious side effects, downgrading essential system components can even make your whole system unusable. Use with care.

configure-any: Configure also any unpacked but unconfigured packages on which the current package depends.

hold: Process packages even when marked "hold".

remove-reinstreq: Remove a package, even if it's broken and marked to require reinstallation. This may, for example, cause parts of the package to remain on the system, which are then forgotten by dpkg.

remove-essential: Remove, even if the package is considered essential. Essential packages contain mostly basic Unix commands. Removing them might cause the whole system to stop working, so use with caution.

depends: Turn all dependency problems into warnings.

depends-version: Don't care about versions when checking dependencies.

breaks: Install, even if this would break another package.

conflicts: Install, even if it conflicts with another package. This is dangerous, for it usually causes overwriting of some files.

confmiss: If a conffile is missing and the version in the package did change, always install the missing conffile without prompting. This is dangerous since it means not preserving a change (removing) made to the file.

confnew: If a conffile was modified and the version in the package did change, always install the new version without prompting, unless the --force-confdef is also specified, where the default action is preferred.

confold: If a conffile was modified and the version in the package did change, always keep the old version without prompting, unless the --force-confdef is also specified, where the default action is preferred.

confdef: If a conffile was modified and the version in the package did change, always choose the default action without prompting. If there is no default action, it stops to ask the user unless --force-confnew or --force-confold is also been given, where it uses that to decide the final action.

confask: If a conffile was modified, always offer to replace it with the version in the package, even if the version in the package did not change. If any of --force-confmiss, --force-confnew, --force-confold, or --force-confdef is also given, it is used to decide the final action.

overwrite: Overwrite one package's file with another's file.

overwrite-dir: Overwrite one package's directory with another's file.

overwrite-diverted: Overwrite a diverted file with an undiverted version.

unsafe-io: Do not perform safe I/O operations when unpacking. Currently this implies not performing file system syncs before file renames, which is known to cause substantial performance degradation on some file systems, unfortunately the ones that require the safe I/O on the first place due to their unreliable behaviour causing zero-length files on abrupt system crashes.

Note: For ext4 filesystems, consider using instead the mount option nodelalloc, which fixes both the performance degradation and the data safety issues, the latter by making the file system not produce zero-length files on abrupt system crashes with any software not doing syncs before atomic renames.

Warning: Using this option might improve performance at the cost of losing data, use with care.

architecture: Process even packages with wrong or no architecture.

bad-version: Process even packages with wrong versions.

bad-path: PATH is missing important programs, so problems are likely.

not-root: Try to (de)install things even when not root.

bad-verify: Install a package even if it fails authenticity check.
--ignore-depends=package,... Ignore dependency-checking for specified packages (actually, checking is performed, but only warnings about conflicts are given, nothing else).
--no-act,
--dry-run,
--simulate
Do everything that is supposed to be done, but don't write any changes. This is used to see what would happen with the specified action, without actually modifying anything.

Be sure to give --no-act before the action-parameter, or you might end up with undesirable results. For example, dpkg --purge foo --no-act first purges package foo and then try to purge package --no-act, even though you probably expected it to actually do nothing.
-R, --recursive Recursively handle all regular files matching pattern *.deb found at specified directories and all its subdirectories. This can be used with -i, -A, --install, --unpack and --avail actions.
-G Don't install a package if a newer version of the same package is already installed. This is an alias of --refuse-downgrade.
--admindir=dir Change default administrative directory, which contains many files that give information about status of installed or uninstalled packages, etc. (Defaults to /var/lib/dpkg).
--instdir=dir Change default installation directory which refers to the directory where packages are to be installed. The instdir is also the directory passed to chroot before running package's installation scripts, indicating the scripts see instdir as a root directory. This defaults to /.
--root=dir Changing root changes instdir to dir and admindir to dir/var/lib/dpkg.
-O, --selected-only Only process the packages that are selected for installation. The actual marking is done with dselect or by dpkg, when it handles packages. For example, when a package is removed, it is marked selected for deinstallation.
-E, --skip-same-version Don't install the package if the same version of the package is already installed.
--pre-invoke=command,
--post-invoke=command
Set an invoke hook command to run via sh -c before or after the dpkg run for the unpack, configure, install, triggers-only, remove and purge actions. This option can be specified multiple times. The order the options are specified is preserved, with the ones from the configuration files taking precedence. The environment variable DPKG_HOOK_ACTION is set for the hooks to the current dpkg action.

Note: front-ends might call dpkg several times per invocation, which might run the hooks more times than expected.
--path-exclude=glob-pattern,
--path-include=glob-pattern
Set glob-pattern as a path filter, either by excluding or re-including previously excluded paths matching the specified patterns during install.

Warning: take into account that, depending on the excluded paths, you might completely break your system, use with caution.

The glob patterns use the same wildcards used in the shell, where '*' matches any sequence of characters, including the empty string and also '/'. For example, '/usr/*/READ*' matches '/usr/share/doc/package/README'. As usual, '?' matches any single character (again, including '/'). And '[' starts a character class, which can contain a list of characters, ranges and complementations.

Note: the current implementation might re-include more directories and symlinks than needed to be on the safe side and avoid possible unpack failures.

This can remove all paths except some particular ones; a typical case is:

--path-exclude=/usr/share/doc/*

--path-include=/usr/share/doc/*/copyright

to remove all documentation files except the Copyright files.

These two options can be specified multiple times, and interleave with each other. Both are processed in the given order, with the last rule that matches a file name making the decision.
--status-fd n Send machine-readable package status and progress information to file descriptor n. This option can be specified multiple times. The information is generally one record per line, in one of the following forms:

status: package: status

Package status changed; status is as in the status file.

status: package : error : extended-error-message

An error occurred. Any possible newlines in extended-error-message are converted to spaces before output.

status: file : conffile-prompt : 'real-old' 'real-new' useredited distedited

User is being asked a conffile question.

processing: stage: package

Sent before a processing stage starts. The stage is one of upgrade, install (both sent before unpacking), configure, trigproc, disappear, remove, purge.
--status-logger=command Send machine-readable package status and progress information to the shell command's standard input. This option can be specified multiple times. The output format used is the same as in --status-fd.
--log=file name Log status change updates and actions to file name, instead of the default /var/log/dpkg.log. If this option is given multiple times, the last file name is used. Log messages are of the form `YYYY-MM-DD HH:MM:SS status state pkg installed-version' for status change updates; `YYYY-MM-DD HH:MM:SS action pkg installed-version available-version' for actions where action is one of install, upgrade, remove, purge; and `YYYY-MM-DD HH:MM:SS conffile file-name decision' for conffile changes, where decision is either install or keep.
--no-debsig Do not try to verify package signatures.
--no-triggers Do not run any triggers in this run (activations are still recorded). If used with --configure package or --triggers-only package, then the named package postinst still runs even if only a triggers run is needed. Use of this option may leave packages in the improper triggers-awaited and triggers-pending states. This can be fixed later by running: dpkg --configure --pending.
--triggers Cancels a previous --no-triggers.

Information about packages

dpkg maintains some usable information about available packages. The information is divided in three classes: states, selection states and flags. These values are intended to be changed mainly with dselect.

Package states

not-installed The package is not installed on your system.
config-files Only the configuration files of the package exist on the system.
half-installed The installation of the package was started, but not completed for some reason.
unpacked The package is unpacked, but not configured.
half-configured The package is unpacked and configuration was started, but not yet completed for some reason.
triggers-awaited The package awaits trigger processing by another package.
triggers-pending The package was triggered.
installed The package is unpacked and configured OK.

Package installation states

install The package is selected for installation.
hold A package marked to be on hold is not handled by dpkg, unless forced to do that with option --force-hold.
deinstall The package is selected for deinstallation (i.e., we want to remove all files, except configuration files).
purge The package is selected to be purged (i.e., we want to remove everything from system directories, even configuration files).

Package flags

reinst-required A package marked reinst-required is broken and requires reinstallation. These packages cannot be removed, unless forced with option --force-remove-reinstreq.

Files

/etc/dpkg/dpkg.cfg Configuration file with default options.
/var/log/dpkg.log Default log file (see /etc/dpkg/dpkg.cfg and option --log).

The other files listed below are in their default directories. See option --admindir to see how to change locations of these files.

/var/lib/dpkg/available List of available packages.
/var/lib/dpkg/status Statuses of available packages. This file contains information about whether a package is marked for removing or not, whether it is installed or not, etc. See section INFORMATION ABOUT PACKAGES for more info.

The status file is backed up daily in /var/backups. It can be useful if it's lost or corrupted due to filesystems troubles.

Additionally, the following files are components of a binary package: control, conffiles, preinst, postinst, prerm, and postrm.

Environment

HOME If set, dpkg uses it as the directory from which to read the user specific configuration file.
TMPDIR If set, dpkg uses it as the directory where it creates temporary files and directories.
PAGER The program dpkg executes when displaying the conffiles.
SHELL The program dpkg executes when starting a new shell.
COLUMNS Sets the number of columns dpkg should use when displaying formatted text. Currently only used by -l.
DPKG_SHELL_REASON Defined by dpkg on the shell spawned on the conffile prompt to examine the situation. Current valid value: conffile-prompt.
DPKG_CONFFILE_OLD Defined by dpkg on the shell spawned on the conffile prompt to examine the situation. Contains the path to the old conffile.
DPKG_CONFFILE_NEW Defined by dpkg on the shell spawned on the conffile prompt to examine the situation. Contains the path to the new conffile.
DPKG_RUNNING_VERSION Defined by dpkg on the maintainer script environment to the version of the currently running dpkg instance.
DPKG_MAINTSCRIPT_PACKAGE Defined by dpkg on the maintainer script environment to the package name being handled.
DPKG_MAINTSCRIPT_ARCH Defined by dpkg on the maintainer script environment to the architecture where the package is built.
DPKG_MAINTSCRIPT_NAME Defined by dpkg on the maintainer script environment to the name of the script running (preinst, postinst, prerm, postrm).

Examples

dpkg -l '*vi*'

List installed packages related to the editor vi.

dpkg --print-avail elvis vim | less

View the entries of the packages elvis and vim as listed in /var/lib/dpkg/available.

less /var/lib/dpkg/available

Manually view the list of available software packages.

dpkg -r elvis

Remove the installed package elvis.

dpkg -i vim_4.5-3.deb

Install the package contained in the file vim_4.5-3.deb.

dpkg --get-selections >myselections

Make a local copy of the package selection states.