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.
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.
Syntax | Type | Description |
---|---|---|
-i <package.deb> --install <package.deb> | Action | Installs the package. |
--unpack <package.deb> | Action | Unpacks the package without configuration. |
--configure <package> | Action | Configures an unpacked package. |
-r <package> --remove <package> | Action | Removes an installed package. Does not remove configuration files and other data. |
-P <package> --purge <package> | Action | Purges an installed or removed package. Deletes configuration files and other data. |
--get-selections | Action | Fetches packages with current selections. |
--set-selections | Action | Sets 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 | Option | Handles action recursively in target directory and subdirectories. |
--log=<file> | Option | Logs status change updates to a file. |
--no-act --dry-run --simulate | Option | 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.
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
Confirm the .deb file downloaded by listing directory contents with the ls command:
ls -l *.deb
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
The command requires sudo to install a .deb package.
List Known Packages
To list all known packages, use the -l
tag:
dpkg -l
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
- Unknowni
- Installh
- Holdr
- Removep
- Purge
2. Package status:
n
- Not installedc
- Config filesH
- Half installedU
- UnpackedF
- Half configuredW
- Awaiting triggerst
- Pending triggersi
- 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
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
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
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*
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*
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
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
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*
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>
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
The output lists all the file directories and file locations.
Get Package Selections
To fetch the package names by selection, use:
dpkg --get-selections
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 }'
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
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
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 thanle
- Less than or equal toeq
- Equalne
- Not equalge
- Greater than or equal togt
- 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 $?
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
To see the foreign architecture support, use:
dpkg --print-foreign-architectures
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
The output shows the dpkg version along with the architecture.
Print Help
To show a simple help menu, use the following command:
dpkg --help
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.