The dpkg error code (2) typically occurs when the package manager encounters file system or permission issues, corrupt package lists, or incomplete installations. This can usually be fixed by reconfiguring dpkg, fixing broken dependencies, or removing lock files.
The "E: Sub-process /usr/bin/dpkg returned an error code (2)" error indicates that the Debian Package Manager (dpkg) has encountered a critical error while processing packages. This error occurs when apt tries to run dpkg as a subprocess to install, remove, or configure packages, but dpkg encounters an issue and exits with error code 2. The error can stem from several sources: corrupted package database files, missing directories in /var/lib/dpkg/, file permission issues, stale lock files preventing dpkg from running, or incomplete package installations from previous attempts. Unlike error code 1, which typically indicates configuration issues, error code 2 usually points to more serious structural problems with the package manager state.
The most common fix is to reconfigure all packages that are installed but not properly configured:
sudo dpkg --configure -aThe -a flag tells dpkg to configure all unconfigured packages. This resolves issues where package installation was incomplete or interrupted.
If reconfiguring dpkg doesn't resolve the issue, use apt to fix broken dependencies:
sudo apt --fix-broken installThis command will attempt to resolve dependency issues and complete interrupted installations. The --fix-broken (or -f) flag specifically addresses broken package dependencies.
If the above steps don't work, dpkg may be blocked by lock files that weren't cleaned up properly. Remove them carefully:
sudo rm /var/lib/dpkg/lock
sudo rm /var/lib/apt/lists/lock
sudo rm /var/cache/apt/archives/lockAfter removing these lock files, attempt to run your apt command again. You may also need to reconfigure dpkg again with sudo dpkg --configure -a.
The dpkg updates directory may be missing. Create it if it doesn't exist:
sudo mkdir -p /var/lib/dpkg/updatesThen update your package lists and try again:
sudo apt update
sudo apt upgradeThis directory is used by dpkg to track ongoing operations and is sometimes removed during system cleanup or after crashes.
If a specific package's metadata is corrupted (evident from error messages mentioning a specific package), you can remove its info file:
sudo rm /var/lib/dpkg/info/[package-name].listReplace [package-name] with the actual package name from the error message. After removal, update and upgrade:
sudo apt update
sudo apt upgradeUse this method only when you've identified the specific problematic package.
If none of the above fixes work, there may be underlying file system corruption. Check your system logs for filesystem errors:
sudo dmesg | grep -i errorLook for messages about filesystem issues. If you find concerning errors, you may need to run filesystem check tools like fsck (ideally in single-user mode or from a live USB), but be cautious as this can be destructive if run incorrectly.
Error code 2 specifically indicates a file system or permission issue rather than a package configuration problem. If you encounter this repeatedly, it may indicate:
- Filesystem issues: Run sudo smartctl -a /dev/sda (replace sda with your disk) to check disk health
- SELinux/AppArmor: Security policies may prevent dpkg from accessing files. Check sudo getenforce and sudo aa-status
- Read-only filesystem: Your /var/lib/dpkg directory may have become read-only. Check with mount | grep /var
- Disk space: Ensure sufficient space exists: df -h /var
In rare cases where the dpkg database is severely corrupted and the above steps fail, you may need to manually reconstruct the database state, which is an advanced procedure best documented in the Debian dpkg manual.
dpkg: serious warning: files list file for package 'package-name' contains empty filename
How to fix "files list file contains empty filename" in APT
dpkg-divert: error: rename involves overwriting 'path' with different file
How to fix dpkg-divert rename conflicts in APT
E: Sub-process /usr/bin/dpkg returned an error code (1) during kernel installation
How to fix "dpkg returned an error code (1)" in APT kernel installation
dpkg: dependency problems prevent configuration of triggers
dpkg: dependency problems prevent configuration of triggers in apt
dpkg: unrecoverable fatal error, aborting
How to fix "dpkg: unrecoverable fatal error, aborting" in APT