dpkg is the low-level package manager for Debian-based Linux systems, and these examples show how to install, remove, inspect, and query .deb packages directly. You type a dpkg command with an action flag and a package name or .deb file, and dpkg performs that action on your system. Below are the most common dpkg commands with concrete examples, plus context on how dpkg relates to apt and when you should use each.
What Is dpkg?
dpkg is the core package management tool on Debian, Ubuntu, and other Debian-based distributions. It installs, removes, and queries individual .deb packages. Unlike apt, dpkg does not automatically resolve dependencies or download packages from repositories; it works directly with .deb files you already have. apt is built on top of dpkg, handling dependency resolution and repository access.
Installing Packages with dpkg
To install a .deb file, use the -i (install) flag:
sudo dpkg -i /path/to/package.deb
For example:
sudo dpkg -i google-chrome-stable_current_amd64.deb
If dpkg reports missing dependencies, run sudo apt-get install -f to fix them. This tells apt to fetch and install any missing dependencies from configured repositories.
Removing Packages
To remove an installed package but keep its configuration files, use -r (remove):
sudo dpkg -r package-name
Example:
sudo dpkg -r google-chrome-stable
To remove the package and its configuration files, use -P (purge):
sudo dpkg -P package-name
Purge is useful when you want a clean slate, for example before reinstalling a misconfigured package.
Querying Installed Packages
To list all installed packages, use -l (list):
dpkg -l
To filter the list, add a pattern:
dpkg -l | grep nginx
To check whether a specific package is installed and see its version, use -s (status):
dpkg -s package-name
Example output includes Package, Status, Version, and Description fields.
Inspecting .deb Files Without Installing
To see information about a .deb file before installing it, use -I (info):
dpkg -I package.deb
This shows the package name, version, architecture, maintainer, and description.
To list the files that a .deb would install, use -c (contents):
dpkg -c package.deb
To see which installed package owns a particular file, use -S (search):
dpkg -S /usr/bin/nginx
Common dpkg Errors and Fixes
One frequent error is “dependency problems – leaving unconfigured.” This happens when you install a .deb that requires other packages not present. Fix it with sudo apt-get install -f.
Another is “package is in a very bad inconsistent state.” This often means a previous install was interrupted. Try sudo dpkg --configure -a to reconfigure all unpacked but unconfigured packages.
If you see “dpkg: error: dpkg status database is locked by another process,” wait for the other process to finish or close the package manager (like Software Center or apt) that is running.
dpkg vs apt: Which to Use?
Use apt for most package management tasks because it resolves dependencies and fetches packages from repositories. Use dpkg when you have a .deb file and need to install it directly, or when you need to inspect a .deb file without installing it. apt is a frontend to dpkg, so any package installed with apt is managed by dpkg underneath.
For example, to install a downloaded .deb, you can use sudo apt install ./package.deb in newer apt versions, which also handles dependencies. But dpkg remains useful for low-level operations like listing files in a package or checking which package owns a file.
Safety and Best Practices
Always back up your system before major package operations. Installing .deb files from untrusted sources can compromise your system. Verify the authenticity of the .deb file if possible, and prefer packages from official repositories or trusted vendors.
When removing packages, double-check the package name with dpkg -l | grep name to avoid removing the wrong package. Purging a package deletes its configuration files, which may include user data for some applications.
Frequently Asked Questions
How do I list all installed packages with dpkg?
Run dpkg -l to list all installed packages. To filter, pipe to grep, e.g., dpkg -l | grep python. The output shows package name, version, architecture, and description.
What does ‘dpkg: error: requested operation requires superuser privilege’ mean?
It means you need to run the command with sudo or as root. dpkg modifies system-wide package databases, so it requires root privileges. Use sudo dpkg ....
How do I fix broken dependencies after a dpkg install?
Run sudo apt-get install -f. This tells apt to resolve and install any missing dependencies. If that fails, try sudo dpkg --configure -a to configure any unpacked packages.
Can I use dpkg to upgrade a package?
Yes, installing a newer .deb over an older one with sudo dpkg -i new.deb will upgrade it, but dpkg won’t fetch dependencies. For normal upgrades, use sudo apt upgrade.
How do I see what files a package installed?
Use dpkg -L package-name. This lists all files that belong to the installed package. For a .deb file not yet installed, use dpkg -c file.deb.
What is the difference between dpkg and apt?
dpkg is the low-level tool that directly handles .deb packages, while apt is a higher-level tool that resolves dependencies, downloads packages from repositories, and calls dpkg to do the actual installation. Use apt for everyday tasks and dpkg for direct .deb file operations.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.
More cards in this drawer
- Campus Tech Innovation Hubs: What They Are
- Poco C51 Airtel Prepaid Locked Variant India?
- Jenkins vs GitHub Actions: Which CI/CD Fits Your Team?
- Turn Off Backup Encryption in iTunes?
- Shared VPS Hosting: Is It Better?
- Disk Defrag Pro: What It Does and Whether You Need It