dpkg - Linux
Overview
dpkg is a package management command used with Debian and its derivatives such as Ubuntu. It is utilized to install, remove, and manage Debian software packages, but does not handle dependency resolution automatically. dpkg is most effectively used for low-level package management, troubleshooting, and for scenarios where precise control over packages is required.
Syntax
The basic syntax for dpkg is as follows:
dpkg [option...] action
Where action can be one of the tasks dpkg can perform (e.g., installation, removal) and option… consists of various flags that modify the behavior of the command.
Options/Flags
Here are some commonly used options in dpkg:
-ior--install: Install a package.debfile.-ror--remove: Remove an installed package (leaves configuration files).--purge: Remove an installed package including configuration files.-lor--list: List all installed packages, optionally filtered by a pattern.-sor--status: Show package status and information.-Lor--listfiles: List files installed to your system from package.-Cor--audit: Check for partially installed packages.-Sor--search: Search for a filename from installed packages.
Examples
Installing a Package
Install a package from a .deb file:
dpkg -i package-file.deb
Removing a Package
Remove a package by name:
dpkg -r package-name
Listing Installed Packages
List all installed packages containing the word “nginx”:
dpkg -l '*nginx*'
Checking Package Information
Check information of an installed package:
dpkg -s package-name
Finding Which Package a File Belongs To
Search for which installed package a specific file belongs to:
dpkg -S /bin/ls
Common Issues
- Dependency errors: When installing packages that have unmet dependencies,
dpkgwill refuse to install the package. Using tools likeaptorapt-getwhich automatically handle dependencies is recommended. - Configuration files not removed: When removing a package using
-ror--remove, configuration files remain on the system. Use--purgeto completely remove the package including its configuration files.
Integration
dpkg is often combined with shell scripting to automate installation tasks or integrated into recovery routines. Here’s an example where dpkg is used to reinstall all currently installed packages:
dpkg --get-selections | grep -w 'install' | cut -f1 | xargs sudo dpkg --reinstall
Related Commands
apt-get: Advanced package handling utility that handles downloading and installing packages with dependency management.apt-cache: Interface to search and query available information about installed and installable packages.
For further reading, consult the official Debian dpkg manual by visiting the Debian documentation or the dpkg man page (man dpkg).