Cyberithub

21+ Practical dpkg Command Examples for Linux Beginners

Advertisements

In this article, I will go through 21+ Practical dpkg command examples which will be helpful for Linux Beginners. If you are a Linux professional then I am sure you must have used some kind of package manager to manage your Linux packages. It could either be yum or dnf if you are using RHEL/CentOS based systems or it could be zypper if you are using OpenSUSE based OS or it might be apt or apt-get if you are using Ubuntu based Systems.

Similarly dpkg is the package manager for Debian Based Systems. You can perform all sorts of package operations like install, update and delete using dpkg command. We will see some real world examples of dpkg command in below section. More about dpkg command.

Synopsis

dpkg [option...] action

21+ Practical dpkg Command Examples for Linux Beginners

Practical dpkg command examples for Linux Beginners 

Also Read: How to Add a Package Repository to Debian 11

Example 1: How to Check dpkg command version

If you want to check dpkg command version then you need to use dpkg --version command as shown below. As you can see from the output, current version is 1.20.9.

NOTE:

Please note that here I am using root user to run all the below commands. You can use any user with sudo access to run all these commands. For more information Please check Step by Step: How to Add User to Sudoers to provide sudo access to the User.
root@debian:~# dpkg --version
Debian 'dpkg' package management program version 1.20.9 (amd64).
This is free software; see the GNU General Public License version 2 or
later for copying conditions. There is NO warranty.

 

Example 2: How to Check the System Architecture

If you want to check the architecture of your Debian system then you need to use --print-architecture option with dpkg command as shown below. As you can see my system architecture is of amd64 type.

root@debian:~# dpkg --print-architecture
amd64

 

Example 3: How to Install a Package in Debian

If you want to install a Debian package then you need to use -i option. Here we are installing wget package using dpkg -i wget_1.21-1+b1_amd64.deb command as shown below.

root@debian:~# dpkg -i wget_1.21-1+b1_amd64.deb
(Reading database ... 136422 files and directories currently installed.)
Preparing to unpack wget_1.21-1+b1_amd64.deb ...
Unpacking wget (1.21-1+b1) over (1.21-1+b1) ...
Setting up wget (1.21-1+b1) ...
Processing triggers for man-db (2.9.4-2) ...

 

Example 4: How to remove a Package in Debian

If you want to remove a package then you need to use -r option. In this example we are removing wget using dpkg -r wget command as shown below.

root@debian:~# dpkg -r wget
(Reading database ... 136422 files and directories currently installed.)
Removing wget (1.21-1+b1) ...
Processing triggers for man-db (2.9.4-2) ...

 

Example 5: How to Purge Configuration of a Package 

If you want to purge the configuration of a package after it is uninstalled then you need to use -P option. In this example we are purging the configuration of wget using dpkg -P wget command as shown below.

root@debian:~# dpkg -P wget
(Reading database ... 136333 files and directories currently installed.)
Purging configuration files for wget (1.21-1+b1) ...

 

Example 6: How to Unpack a Package using dpkg command

If you want to unpack a package then you need to use --unpack option. In this example, we are unpacking wget_1.21-1+b1_amd64.deb package using dpkg --unpack wget_1.21-1+b1_amd64.deb command as shown below.

root@debian:~# dpkg --unpack wget_1.21-1+b1_amd64.deb
Selecting previously unselected package wget.
(Reading database ... 136332 files and directories currently installed.)
Preparing to unpack wget_1.21-1+b1_amd64.deb ...
Unpacking wget (1.21-1+b1) ...
Processing triggers for man-db (2.9.4-2) ...

 

Example 7: How to Configure an unpacked Package

If you are looking to configure an unpacked package then you need to use --configure option. In this example we are configuring an unpacked wget package by using dpkg --configure wget command as shown below.

root@debian:~# dpkg --configure wget
Setting up wget (1.21-1+b1) ...

 

Example 8: How to Check the Status of a Package

If you want to check the complete status of a package then you need to use -s option. In this example we are checking the status of wget using dpkg -s wget command as shown below.

root@debian:~# dpkg -s wget
Package: wget
Status: install ok installed
Priority: standard
Section: web
Installed-Size: 3477
Maintainer: Noël Köthe <noel@debian.org>
Architecture: amd64
Multi-Arch: foreign
Source: wget (1.21-1)
Version: 1.21-1+b1
Depends: libc6 (>= 2.28), libgnutls30 (>= 3.7.0), libidn2-0 (>= 0.6), libnettle8, libpcre2-8-0 (>= 10.22), libpsl5 (>= 0.16.0), libuuid1 (>= 2.16), zlib1g (>= 1:1.1.4)

 

Example 9: How to Check a Package Installation

If you want to list an installed package then you need to use -l option. In this example we are listing wget package by using dpkg -l wget command as shown below.

root@debian:~# dpkg -l wget
Desired=Unknown/Install/Remove/Purge/Hold
| Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend
|/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)
||/ Name Version Architecture Description
+++-==============-============-============-=================================
ii wget 1.21-1+b1 amd64 retrieves files from the web

 

Example 10: How to Check the location of an Installed Package

If you want to check the location of an Installed Package then you need to use -L option. In this example we are checking the location of wget package using dpkg -L wget command as shown below.

root@debian:~# dpkg -L wget
/.
/etc
/etc/wgetrc
/usr
/usr/bin
/usr/bin/wget
/usr/share
/usr/share/doc
/usr/share/doc/wget
/usr/share/doc/wget/AUTHORS
/usr/share/doc/wget/MAILING-LIST
/usr/share/doc/wget/NEWS.gz
/usr/share/doc/wget/README
/usr/share/doc/wget/changelog.Debian.amd64.gz
/usr/share/doc/wget/changelog.Debian.gz

 

Example 11: How to get List of Package Selections

If you want to get the selection state of a package then you need to use --get-selections option with dpkg command. In this example we are list the selection state of wget package by using dpkg --get-selections wget command as shown below.

root@debian:~# dpkg --get-selections wget
wget                   install

 

Example 12: How to Print target package of one or more pre-dependencies

If you want to print a package which is the target of one or more relevant pre-dependencies and has itself no unsatisfied pre-dependencies then you need to use --predep-package option with dpkg command as shown below. If such a package is present then exit status will show 0 otherwise it will show 1. As you can see for me, exit status is showing 1 so there at there are no such package available.

root@debian:~# dpkg --predep-package
root@debian:~# echo $?
1

 

Example 13: How to Verify all the Installed Packages

If you want to verify the integrity of all the installed packages then you need to use dpkg --verify command as shown below.

root@debian:~# dpkg --verify
??5?????? c /etc/ssh/ssh_config

 

Example 14: How to Simulate installation of a Package

If you only want to simulate the installation of a package without actually installing that package then you need to use --dry-run option. This option will only show the changes that it will do when we actually run the command without --dry-run option.

root@debian:~# dpkg --dry-run -i wget_1.21-1+b1_amd64.deb
Selecting previously unselected package wget.
(Reading database ... 136333 files and directories currently installed.)
Preparing to unpack wget_1.21-1+b1_amd64.deb ...

 

Example 15: How to Set the State of every non-essential package to deinstall

If you want to set the state of every non-essential package to deinstall then you need to use --clear-selections option with dpkg command as you can see below.

root@debian:~# dpkg --clear-selections

 

Example 16: How to Ignore a package installation if newer version is already installed

If you want to ignore the installation of a package if newer version of that package is already installed then you need to use -G option. In this example we are trying to install a specific version of wget package if its latest version is already not installed by using dpkg -G -i wget_1.21-1+b1_amd64.deb command as shown below.

root@debian:~# dpkg -G -i wget_1.21-1+b1_amd64.deb
Selecting previously unselected package wget.
(Reading database ... 136333 files and directories currently installed.)
Preparing to unpack wget_1.21-1+b1_amd64.deb ...
Unpacking wget (1.21-1+b1) ...
Setting up wget (1.21-1+b1) ...
Processing triggers for man-db (2.9.4-2) ...

 

Example 17: How to Skip Package Installation if the same version already installed

If you want to skip the package installation if the same version of that package is already installed then you need to use -E option with dpkg command. In this example we are checking if the version of wget package is the same with the installed version and then only we are installing by using dpkg -E -i wget_1.21-1+b1_amd64.deb command. As you can see since we already have the same version which we are trying to install so it is skipping this installation.

root@debian:~# dpkg -E -i wget_1.21-1+b1_amd64.deb
dpkg: version 1.21-1+b1 of wget already installed, skipping

 

Example 18: How to make a local copy of Package Selection States

You can also make a local copy of package selection states by redirecting all the output to a local file. Here we are redirecting all the packages states to a file called myselections using dpkg --get-selections> myselections command as shown below.

root@debian:~# dpkg --get-selections> myselections
root@debian:~# cat myselections
accountsservice deinstall
acl deinstall
adduser deinstall
adwaita-icon-theme deinstall
aisleriot deinstall
alsa-topology-conf deinstall
alsa-ucm-conf deinstall

 

Example 19: How to Replace Old information with the information from Package File

If you want to replace old information with the new information from package file then you can use --merge-avail option with dpkg command. In this example, we are dumping all the packages info and then replacing the old information with the information from package file using apt-cache dumpavail | dpkg --merge-avail command as shown below.

root@debian:~# apt-cache dumpavail | dpkg --merge-avail
Updating available packages info, using -.
Information about 58591 packages was updated.

 

Example 20: How to Install a Package from a Directory

If you want to install a package from a directory then you need to use -R option and specify all the packages in a directory for installation. Here I have created a directory called install/ where I am putting up my wget package and then installing using dpkg -R -i install/ command as shown below.

root@debian:~# dpkg -R -i install/
Selecting previously unselected package wget.
(Reading database ... 136333 files and directories currently installed.)
Preparing to unpack ...//wget_1.21-1+b1_amd64.deb ...
Unpacking wget (1.21-1+b1) ...
Setting up wget (1.21-1+b1) ...
Processing triggers for man-db (2.9.4-2) ...

 

Example 21: How to Check all the options available with dpkg command

If you want to check all the options available with dpkg command then you need to use dpkg --help command as shown below.

root@debian:~# dpkg --help
Usage: dpkg [<option>...] <command>

Commands:
-i|--install <.deb file name>... | -R|--recursive <directory>...
--unpack <.deb file name>... | -R|--recursive <directory>...
-A|--record-avail <.deb file name>... | -R|--recursive <directory>...
--configure <package>... | -a|--pending
--triggers-only <package>... | -a|--pending
-r|--remove <package>... | -a|--pending
-P|--purge <package>... | -a|--pending
-V|--verify [<package>...] Verify the integrity of package(s).
--get-selections [<pattern>...] Get list of selections to stdout.
--set-selections Set package selections from stdin.
--clear-selections Deselect every non-essential package.

 

Example 22: How to Check Man Page of dpkg command

If you want to check the man page of dpkg command then you need to use man dpkg command as shown below.

root@debian:~# man dpkg
dpkg(1) dpkg suite dpkg(1)

NAME
dpkg - package manager for Debian

SYNOPSIS
dpkg [option...] action

WARNING
This manual is intended for users wishing to understand dpkg's command line options and package states in more detail than that provided by dpkg
--help.

It should not be used by package maintainers wishing to understand how dpkg will install their packages. The descriptions of what dpkg does when
installing and removing packages are particularly inadequate.

Leave a Comment