How To Use Dpkg Command In Linux.
This guide explores the dpkg command syntax and options through various examples.
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.
dpkg Command Syntax
The basic syntax for the dpkg command is:
The command accepts one action and zero or more options.
The dpkg command acts as a front-end for the following two tools:
The dpkg-deb command, which shows information about .deb packages.
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:
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.
Installs the package.
Unpacks the package without configuration.
Configures an unpacked package.
Removes an installed package. Does not remove configuration files and other data.
Purges an installed or removed package. Deletes configuration files and other data.
Fetches packages with current selections.
Sets file selection states from a file read from standard input.
Builds a .deb package.
Lists package contents.
Shows information about a package.
Lists packages by matching the pattern.
List installed package’s file locations.
Shows the status of an installed package.
Search for a pattern in installed packages.
Handles action recursively in target directory and subdirectories.
Logs status change updates to a file.
Shows 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.
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:
Confirm the .deb file downloaded by listing directory contents with the ls command:
The output shows the .deb package.
Install a Debian package with the dpkg command and the -i or –install tag:
The command requires sudo to install a .deb package.
List Known Packages
To list all known packages, use the -l tag:
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.
The first three columns provide a complete overview of the package status:
u – Unknown
i – Install
h – Hold
r – Remove
p – Purge
n – Not installed
c – Config files
H – Half installed
U – Unpacked
F – Half configured
W – Awaiting triggers
t – Pending triggers
i – Installed
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:
To list multiple packages, separate each package name with a space.
To remove a Debian package, use the following command:
For example, to remove the cowsay package, use:
The -r option does not remove the configuration files. Use this option when removing software for reinstallation.
To purge a package, use the -P option:
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:
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.
To unpack the package without configuration, use:
The command unpacks all the files from the package.
To configure an unpacked .deb package or reconfigure an installed package, use:
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:
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:
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:
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:
The output lists all the file directories and file locations.
Get Package Selections
To fetch the package names by selection, use:
The list shows package names and the selection marking.
List Installed Packages
To fetch a list of installed packages, use the following command:
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:
The command expects the package state as standard input. The possible conditions are:
For example, to stop a package from updating, use:
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:
For example, search for apt-get:
The output shows apt-get belongs to the apt package.
Alternatively, search for a specific file. For example:
The output shows the file belongs to the cowsay package.
The dpkg command offers a tool for comparing program version numbers. The syntax is:
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:
The command returns 0, indicating the first version number is greater than the second.
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:
To see the foreign architecture support, use:
To add or remove architecture support, use:
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:
The output shows the dpkg version along with the architecture.
To show a simple help menu, use the following command:
After following the examples from this tutorial, you should master the basics of the dpkg command.