dpkg Command in Linux With Examples

Introduction

dpkg in Linux is the primary package manager for Debian and Debian-based systems, such as Ubuntu. The tool installs, builds, removes, configures, and retrieves information for Debian packages. The command works with packages in .deb format.

This guide explores the dpkg command syntax and options through various examples.

dpkg Command in Linux With Examples

Prerequisites

  • A system with a Debian or a Debian-based OS.
  • Access to the command line/terminal as sudo.
  • A .deb file to work with the examples.

dpkg Command Syntax

The basic syntax for the dpkg command is:

dpkg [options] action

The command accepts one action and zero or more options.

The dpkg command acts as a front-end for the following two tools:

1. The dpkg-deb command, which shows information about .deb packages.

2. The dpkg-query command, which shows the information from the dpkg database.

The dpkg command runs actions from dpkg-query and dpkg-deb. Therefore, the following two commands show the same result:

dpkg -l
dpkg-query -l

The action -l is a dpkg-query action that lists all packages from the dpkg database. The dpkg command detects the foreign options and runs dpkg-query automatically.

dpkg Command Options

The table below provides brief descriptions of commonly used options and actions for the dpkg command.

SyntaxTypeDescription
-i <package.deb>
--install <package.deb>
ActionInstalls the package.
--unpack <package.deb>ActionUnpacks the package without configuration.
--configure <package>ActionConfigures an unpacked package.
-r <package>
--remove <package>
ActionRemoves an installed package. Does not remove configuration files and other data.
-P <package>
--purge <package>
ActionPurges an installed or removed package. Deletes configuration files and other data.
--get-selectionsActionFetches packages with current selections.
--set-selectionsActionSets file selection states from a file read from standard input.
-b <directory>
--build <directory>
Action
(from dpkg-deb)
Builds a .deb package.
-c <package.deb>
--contents <package.deb>
Action
(from dpkg-deb)
Lists package contents.
-I <package.deb>
--info <package.deb>
Action
(from dpkg-deb)
Shows information about a package.
-l <pattern>
--list <pattern>
Action
(from dpkg-query)
Lists packages by matching the pattern.
-L <package>
--listfiles <package>
Action
(from dpkg-query)
List installed package's file locations.
-s <package>
--status <package>
Action
(from dpkg-query)
Shows the status of an installed package.
-S <pattern>
--search <pattern>
Action
(from dpkg-query)
Search for a pattern in installed packages.
-R
--recursive
OptionHandles action recursively in target directory and subdirectories.
--log=<file>OptionLogs status change updates to a file.
--no-act
--dry-run
--simulate
OptionShows output without committing changes. Use before action.

Actions that change the system, such as package installation or removal, require sudo privileges. Information-based options do not require special user privileges.

Privileged access management helps secure the system by disallowing regular users to make system-wide changes.

Note: To elevate a user's permissions, add the user to sudoers.

dpkg Command Examples

If you have a .deb package, continue to the examples below. If not, download a simple .deb package for testing, such as the cowsay command-line gimmick.

To get the file, run the following:

wget http://archive.ubuntu.com/ubuntu/pool/universe/c/cowsay/cowsay_3.03+dfsg2-4_all.deb
wget cowsay terminal output

Confirm the .deb file downloaded by listing directory contents with the ls command:

ls -l *.deb
ls deb package terminal output

The output shows the .deb package.

Install Package

Install a Debian package with the dpkg command and the -i or --install tag:

sudo dpkg -i <package.deb>

For example:

sudo dpkg -i cowsay_3.03+dfsg2-4_all.deb
sudo dpkg -i terminal output

The command requires sudo to install a .deb package.

List Known Packages

To list all known packages, use the -l tag:

dpkg -l
dpkg -l terminal output

The command prints the packages in pager mode. Navigate using the arrow keys or use space to list by page. Press q to exit the list. The columns list each package's name, version, architecture, and description.

Note: Pager modules, such as the less command, help display long outputs page by page.

The first three columns provide a complete overview of the package status:

1. Action selection:

  • u - Unknown
  • i - Install
  • h - Hold
  • r - Remove
  • p - Purge

2. Package status:

  • n - Not installed
  • c - Config files
  • H - Half installed
  • U - Unpacked
  • F - Half configured
  • W - Awaiting triggers
  • t - Pending triggers
  • i - Installed

3. Error flags:

  • R - Reinstallation required
  • <empty> - No errors

Therefore, a package status "ii " means dpkg successfully installed the package selected for installation without errors.

To fetch a specific instance from the list, use:

dpkg -l <package>

For example:

dpkg -l cowsay
dpkg -l specific package terminal output

To list multiple packages, separate each package name with a space.

Remove Package

To remove a Debian package, use the following command:

dpkg -r <package>

For example, to remove the cowsay package, use:

sudo dpkg -r cowsay
dpkg -r terminal output

The -r option does not remove the configuration files. Use this option when removing software for reinstallation.

Purge Package

To purge a package, use the -P option:

sudo dpkg -P <package>

For example:

sudo dpkg -P cowsay
sudo dpkg -P terminal output

The command removes a package along with any configuration files. Use purge to remove a program from the system altogether.

Show Package Contents

A .deb package contains several files and directories, indicating where the package resides after installation or unpacking.

To display the package contents, run:

dpkg -c <package.deb>

For example:

dpkg -c cowsay*
files and directories in the .deb package file

The output shows all the files and directories in the .deb package file. The example package resides in /usr/games/ and /usr/share/ after installation or unpacking.

Unpack Package

To unpack the package without configuration, use:

sudo dpkg --unpack <package.deb>

For example:

sudo dpkg --unpack cowsay*
sudo dpkg --unpack terminal output

The command unpacks all the files from the package.

Configure Package

To configure an unpacked .deb package or reconfigure an installed package, use:

dpkg --configure <package>

For example:

sudo dpkg --configure cowsay
sudo dpkg --configure terminal output

Unpacking and configuring a Debian package divides the installation process into two steps.

Check If the Package Installed

To check whether a package is installed, use the -s tag:

dpkg -s <package>

For example:

dpkg -s cowsay
dpkg -s terminal output

The Status line shows whether a package is installed.

Show Package Information

To show package information directly from the .deb file, use the following command:

dpkg -I <package.deb>

For example:

dpkg -I cowsay*
dpkg -I terminal output

The command shows package information, such as the size, dependencies, and documentation references. Use this method to check package information before installing a package.

Install All Packages from a Directory

Use the -R option before -i to install packages recursively:

dpkg -R -i <directory>
sudo dpkg -R -i terminal output

The command installs all packages located in the directory and any subdirectories. If multiple instances of the same package unpack, only one configuration occurs.

List Installed Package Files' Locations

To list the package files and their location, use the -L tag:

dpkg -L <package>

For example:

dpkg -L cowsay
dpkg -l list files terminal output

The output lists all the file directories and file locations.

Get Package Selections

To fetch the package names by selection, use:

dpkg --get-selections
dpkg --get-selections terminal output

The list shows package names and the selection marking.

List Installed Packages

To fetch a list of installed packages, use the following command:

dpkg --get-selections | grep -v "deinstall" | awk '{ print $1 }'
dpkg get selectionsinstalled packages terminal output

The command does the following:

  • grep filters out packages marked "deinstall".
  • awk prints only the first column with the package names.

Use this method when you require a list of installed programs.

Set Package Selections

Set the package selection for a package in the following format:

echo <package> <state> | dpkg --set-selections

The command expects the package state as standard input. The possible conditions are:

  • install
  • hold
  • deinstall
  • purge
  • unknown

For example, to stop a package from updating, use:

echo cowsay hold | sudo dpkg --set-selections

The package is in a hold state, meaning the program receives no automatic updates.

Search Package Containing Files

To find a package containing a file, use the -S option:

dpkg -S <pattern>

For example, search for apt-get:

dpkg -S apt-get
dpkg -s apt-get terminal output

The output shows apt-get belongs to the apt package.

Note: Learn about the difference between apt vs. apt-get.

Alternatively, search for a specific file. For example:

dpgk -S gnu.cow
dpkg -s file terminal output

The output shows the file belongs to the cowsay package.

Compare Versions

The dpkg command offers a tool for comparing program version numbers. The syntax is:

dpkg --compare-versions <version number> <comparison operator> <version number>

The possible comparison operators are:

  • lt - Strictly less than
  • le - Less than or equal to
  • eq - Equal
  • ne - Not equal
  • ge - Greater than or equal to
  • gt - Strictly greater than

dpkg returns 0 (success) for a correct comparison and a non-zero value for failure.

For example, compare the following version numbers:

dpkg --compare-versions 1.2-3 gt 1.1-4
echo $?
dpkg --compare-versions terminal output

The command returns 0, indicating the first version number is greater than the second.

Multi-Architecture Support

All Debian packages contain supported architecture information. The restriction ensures the system does not end up with binaries for an incorrect architecture.

Some architectures support different architectures natively. For example, an amd64 (a 64-bit system) supports i386 (a 32-bit system) installations.

The dpkg command matches the host's architecture. Fetch the information with:

dpkg --print-architecture
dpkg --print-architecture terminal output

To see the foreign architecture support, use:

dpkg --print-foreign-architectures
dpkg --print-foreign-architecture terminal output

To add or remove architecture support, use:

sudo dpkg --add-architecture <architecture name>
sudo dpkg --remove-architecture <architecture name>

In both cases, the foreign architectures list updates with the changes immediately.

Check dpkg Command Version

To check which dpkg version the system is running, use:

dpkg --version
dpkg --version terminal output

The output shows the dpkg version along with the architecture.

Print Help

To show a simple help menu, use the following command:

dpkg --help 
dpkg --help terminal output

Use the man command to find find the complete documentation.

Conclusion

After following the examples from this tutorial, you should master the basics of the dpkg command.

Next, try the more modern apt package manager, which searches through the official repositories for packages.

Was this article helpful?
YesNo
Milica Dancuk
Milica Dancuk is a technical writer at phoenixNAP who is passionate about programming. Her background in Electrical Engineering and Computing combined with her teaching experience give her the ability to easily explain complex technical concepts through her content.
Next you should read
How to Fix Broken Packages in Ubuntu
October 14, 2021

A bad internet connection or misconfigured third-party installers can corrupt packages and cause problems on your computer. This...
Read more
RPM vs. YUM: Learn the Difference
February 9, 2022

This article explains the difference between two Red Hat-based Linux package managers - RPM and YUM. See the key differences to help you decide which...
Read more
How to Fix 'add-apt-repository command not found' on Ubuntu & Debian
August 7, 2019

This guide shows you how to fix the 'add-apt-repository command not found' error in Ubuntu. This specific Ubuntu...
Read more
How to List Installed Packages on Ubuntu
March 9, 2022

Having a precise list of installed packages helps system admins maintain, replicate, and reinstall systems. In this tutorial, learn how to list...
Read more