How to use the command 'dpkg' (with examples)
- Linux
- December 25, 2023
The dpkg
command is the Debian package manager, used for managing .deb packages on Debian-based Linux distributions. It provides various subcommands to perform package installation, removal, and other related operations.
Use case 1: Install a package
Code:
dpkg -i path/to/file.deb
Motivation: You want to install a .deb package on your Debian-based Linux system.
Explanation:
-i
: This option is used to install the package.path/to/file.deb
: The path to the .deb package file that you want to install.
Example output:
Selecting previously unselected package package-name.
(Reading database ... 100%
(Preparing to unpack path/to/file.deb ...
Unpacking package-name (version) ...
Setting up package-name (version) ...
Use case 2: Remove a package
Code:
dpkg -r package
Motivation: You want to remove a previously installed package from your system.
Explanation:
-r
: This option is used to remove the package.package
: The name of the package you want to remove.
Example output:
(Reading database ... 100%
(Reading database ... 100%
(1/2) Removing package-name (version) ...
(2/2) Removing package-name (version) ...
Use case 3: List installed packages
Code:
dpkg -l pattern
Motivation: You want to list all the installed packages on your system matching a specific pattern.
Explanation:
-l
: This option is used to list packages.pattern
: The pattern to match against the package names.
Example output:
||/ Name Version Architecture Description
+++-==============-=========-============-=================================
ii package-name 1.0 amd64 A brief description of the package
ii package2-name 2.0 amd64 Another package description
Use case 4: List a package’s contents
Code:
dpkg -L package
Motivation: You want to view the contents of a specific installed package.
Explanation:
-L
: This option is used to list the files provided by the package.package
: The name of the package.
Example output:
/.
/usr
/usr/bin
/usr/bin/executable
/usr/share
/usr/share/doc
/usr/share/doc/package
/usr/share/doc/package/changelog.Debian.gz
/usr/share/doc/package/copyright
Use case 5: List contents of a local package file
Code:
dpkg -c path/to/file.deb
Motivation: You want to see the contents of a .deb package file before installing it.
Explanation:
-c
: This option is used to display the contents of the package file.path/to/file.deb
: The path to the .deb package file.
Example output:
drwxr-xr-x root/root 0 2021-01-01 00:00 ./
drwxr-xr-x root/root 0 2021-01-01 00:00 ./usr/
drwxr-xr-x root/root 0 2021-01-01 00:00 ./usr/bin/
-rwxr-xr-x root/root 123456 2021-01-01 00:00 ./usr/bin/executable
Use case 6: Find out which package owns a file
Code:
dpkg -S path/to/file
Motivation: You want to determine which installed package owns a specific file.
Explanation:
-S
: This option is used to search for the package owning the specified file.path/to/file
: The path to the file.
Example output:
package-name: /path/to/file
Conclusion:
In this article, we explored various use cases of the dpkg
command. We covered how to install and remove packages, list installed packages, list a package’s contents, view the contents of a local package file, and determine the package owning a file. The dpkg
command is a powerful tool for managing packages on Debian-based Linux systems.