Introduction / Why You Need This
dpkg is a low-level package manager that forms the foundation of package management systems in Debian-based distributions (Debian, Ubuntu, Linux Mint, etc.). While apt and apt-get are convenient for working with repositories, dpkg is indispensable for manually installing local .deb files, inspecting package states in detail, and resolving complex dependency issues. This guide covers the essential operations every Linux administrator or advanced user will need.
Requirements / Preparation
- Debian-based distribution: These instructions are current for Debian, Ubuntu, Linux Mint, and their derivatives.
- Superuser privileges: Most operations (
-i,-r,-P) requiresudo. - Local
.debfile: For the installation step, you will need a downloaded package file (e.g., from the project's official website). - Basic terminal familiarity: Ability to navigate (
cd,ls) and edit commands.
Step 1: Check dpkg Presence and Version
Although dpkg is almost always pre-installed, it's useful to verify its presence and check the version.
dpkg --version
Expected output (example):
Debian `dpkg` package management program version 1.22.0 (amd64).
If the command is not found, install the dpkg package using your distribution's package manager (e.g., sudo apt update && sudo apt install dpkg).
Step 2: Install a Package from a Local .deb File
This is the primary operation for installing software not available in official repositories or for installing a specific version.
- Navigate to the directory containing the downloaded file (e.g.,
~/Downloads). - Perform the installation:
sudo dpkg -i package_file.deb
-i(install) — the key flag.- Important:
dpkgdoes not automatically resolve dependencies. If the package requires other libraries or programs, the installation will fail with an error. In this case, proceed to Step 6 to fix it.
Example:
cd ~/Downloads
sudo dpkg -i google-chrome-stable_current_amd64.deb
Step 3: Remove a Package
Removal via dpkg is more "raw" than via apt, but it gives you precise control.
- Remove package (keeps configs):
sudo dpkg -r package_name-r(remove) — removes the package's binaries and data but preserves configuration files in/etc/. This is useful if you plan to reinstall the package with the same settings.
- Purge package (removes configs):
sudo dpkg -P package_name-P(purge) — removes everything, including configurations. Use with caution; settings will be lost permanently.
Example:
sudo dpkg -r nginx
# or
sudo dpkg -P nginx
Step 4: View Information About Installed Packages
dpkg provides several ways to get information.
- List all installed packages:
dpkg -l
The output is very long. Usegrepto filter:dpkg -l | grep -i python - Detailed information about a specific package:
dpkg -s package_name
Shows status, version, description, dependencies, sizes, and other metadata. - List files installed by a package:
dpkg -L package_name
Useful to understand exactly where a package "installed itself". - Search for a package by file:
dpkg -S /path/to/file
Answers the question "Which package owns this file?".
Example:
dpkg -s curl
dpkg -S /usr/bin/curl
Step 5: Verify Package Integrity
If you suspect system files have been altered (e.g., after a hack or crash), you can verify if they match the package database.
sudo dpkg -V package_name
- Without a package name, it will verify all installed packages (this can take a long time).
- In the output, characters in the first 5 columns indicate changes (e.g.,
5= checksum change,M= permission mode change). Empty columns mean the file is unchanged.
Step 6: Resolving Dependency Problems (Common Case)
The most common error when using dpkg -i:
dpkg: dependency problems prevent configuration of package_name
This means the package requires other packages that are not yet installed.
Solution: Use apt, which can automatically find and install missing dependencies from repositories.
sudo apt --fix-broken install
This command will analyze broken dependencies and install the missing packages. After it completes successfully, you can try sudo dpkg -i package_file.deb again, or simply proceed—apt often configures the package automatically after fixing dependencies.
Force reconfiguration: If a package is installed but not configured (status rc), run:
sudo dpkg --configure -a
Step 7: Working with Packages in "Read-Only" Mode (extract)
Sometimes you just need to extract files from a .deb archive without installing them to the system (e.g., to view scripts).
- Create a temporary directory.
- Unpack the
ararchive (the.debformat is anararchive):mk temp_dir && cd temp_dir ar xx /path/to/package.deb - Extract the contents of
data.tar.xz(ordata.tar.gz):tar -xf data.tar.xz
Now all the package's files will be in the current directory.
Verifying the Result
After performing the main operations, check the outcome:
- After installation:
dpkg -l | grep package_name(status should beii). - After removal:
dpkg -l | grep package_name(status should bercor the package should not appear). - After fixing dependencies: Ensure the
sudo apt --fix-broken installcommand completed without errors. - Check program functionality: Run it (e.g.,
nginx -vorgoogle-chrome --version).
Possible Issues
- Error "dpkg: error: parsing file '/var/lib/dpkg/status' near line X..."
- Cause: Corrupted package database.
- Solution: Restore from backup:
sudo cp /var/backups/dpkg.status.0 /var/lib/dpkg/status. If no backup exists, try creating an empty file:echo "Package: status\ndpkg" | sudo tee /var/lib/dpkg/status(caution: this resets installation history).
- "dpkg: warning: package not marked as conffile, but configuration file found: ..."
- Cause: A configuration file (
conffile) was manually modified, and the package is trying to overwrite it. - Solution: Either save your version of the file, or allow
dpkgto install the version from the package (during an upgrade). Usedpkg-divertif you need to preserve a custom file.
- Cause: A configuration file (
- Package "stuck" in "half-installed" or "half-configured" state
- Solution: Force remove the problematic package:
sudo dpkg --remove --force-remove-reinstreq package_name, then clean up dependencies withsudo apt --fix-broken install.
- Solution: Force remove the problematic package:
- No permissions for file in installation directory
- Cause: Attempting to install a package to a system directory without
sudo. - Solution: Always use
sudofor write operations (-i,-r,-P).
- Cause: Attempting to install a package to a system directory without
Additional Features (Briefly)
dpkg-reconfigure package_name— re-runs the package's configuration script (useful after changing a config or on configuration errors).dpkg --get-selections— outputs a list of all packages with their status (install, hold, deinstall). Can be saved to a file for backup.dpkg --set-selections < file— restores package selections from a file created by--get-selections.dpkg -C— verifies the filesystem integrity of packages, looking for missing or corrupted files.
Conclusion
You have mastered a basic but critically important tool—dpkg. Remember: for everyday work with repositories, use apt. Reserve dpkg for manually installing .deb files, detailed auditing, and solving complex package problems. Always check dependencies after a manual install with sudo apt --fix-broken install.