When running sudo apt upgrade or installing new software packages in Ubuntu, few terminal failures halt administration as abruptly as: E: Sub-process /usr/bin/dpkg returned an error code (2). Unlike minor dependency warnings, error code 2 indicates a structural breakdown within the low-level Debian package management database, preventing APT from installing, updating, or removing any software on the system.
This fatal error usually occurs when package installation scripts are terminated mid-execution by a power outage, the primary dpkg status database (/var/lib/dpkg/status) is corrupted, or filesystem metadata inside /var/lib/dpkg/info/ contains bad blocks. Use the step-by-step methods below to repair the dpkg database and restore package management in Ubuntu.
Restore the dpkg Status File from Backup
The file /var/lib/dpkg/status records the exact installation state of every package on your Ubuntu machine. If this file contains syntax errors or was truncated during an unexpected reboot, dpkg aborts with error code 2. Ubuntu automatically keeps rolling backups of this file:
- Open a terminal and navigate to the dpkg directory:
cd /var/lib/dpkg - Check the size and contents of your current status file:
ls -lh status status-old - If
statusis empty (0 bytes) or damaged, back it up and restore from the latest working snapshot:sudo cp status status.broken sudo cp status-old status - If
status-oldis also damaged, inspect automated daily database backups in/var/backups/:ls -lh /var/backups/dpkg.status* sudo zcat /var/backups/dpkg.status.0.gz | sudo tee /var/lib/dpkg/status > /dev/null - Run an update to test if the database parses cleanly:
sudo apt update
Reconfigure Unfinished Packages
Once the database status file is restored, force dpkg to resume interrupted installation scripts:
sudo dpkg --configure -a
If any package was left half-installed, this command executes post-installation configuration routines to bring the package state to installed.
Clear Corrupted Package Info Files
If error code 2 references a specific package whose installation manifests in /var/lib/dpkg/info/ are corrupted, isolate and remove those specific manifest files:
- Move the troubled package info files out of the active directory (replace
package-namewith the software cited in your terminal error):sudo mv /var/lib/dpkg/info/package-name.* /tmp/ - Update your package list and reinstall the clean package files:
sudo apt update sudo apt install --reinstall package-name
Fix Broken Dependencies and Flush Cache
Finally, clean package archive caches and repair any dangling dependency relationships:
sudo apt --fix-broken install
sudo apt clean
sudo apt autoremove
Your package manager will now operate normally without encountering sub-process execution faults.