Broken apt/dpkg Packages
Fix interrupted dpkg installations, broken dependencies, held packages, and corrupted apt state on Debian/Ubuntu.
Broken package states usually happen after an interrupted upgrade, a failed install, or a disk-full event during apt operations.
Common Error Messages
dpkg was interrupted, you must manually run 'dpkg --configure -a'Sub-process /usr/bin/dpkg returned an error code (1)Package X is in a very bad inconsistent stateYou have held broken packagesE: Unable to correct problems, you have held broken packages
Step 1: Fix Interrupted Installation
This is the most common fix and should always be tried first:
dpkg --configure -a
apt-get install -f # fix broken dependencies
apt-get upgradeStep 2: Clean Cache and Retry
apt-get clean
apt-get autoclean
apt-get update
apt-get upgradeStep 3: Fix a Specific Broken Package
# Force remove and reinstall
apt-get remove --purge packagename
apt-get install packagename
# If remove fails, force it
dpkg --remove --force-remove-reinstreq packagename
apt-get install packagename
# Reinstall without removing
apt-get install --reinstall packagenameStep 4: Fix dpkg Lock
If apt/dpkg is stuck (e.g., after a killed process):
# First, make sure no apt/dpkg process is actually running
ps aux | grep -E "apt|dpkg"
# If truly no process is running, remove the locks
rm -f /var/lib/dpkg/lock-frontend
rm -f /var/lib/dpkg/lock
rm -f /var/cache/apt/archives/lock
rm -f /var/lib/apt/lists/lock
dpkg --configure -a
apt-get updateOnly remove lock files if you are certain no apt or dpkg process is running. Removing locks while apt is active will corrupt the package database.
Held Packages
# List all held packages
dpkg --get-selections | grep hold
apt-mark showhold
# Unhold a package (allow upgrades)
apt-mark unhold packagename
# Hold a package (prevent upgrades)
apt-mark hold packagename
# Example: hold a specific kernel
apt-mark hold linux-image-6.5.0-27-genericRemove Orphaned Config Files
Packages in rc state are removed but their config files remain:
# List them
dpkg --list | grep "^rc"
# Purge all of them at once
dpkg --list | grep "^rc" | awk '{print $2}' | xargs dpkg --purgeForce Operations (Last Resort)
--force-all bypasses dependency checks and can break your system further. Use only when all other options have failed, ideally on a non-critical system or after taking a snapshot.
# Force purge a badly broken package
dpkg --force-all --purge packagename
# Force install ignoring dependencies
dpkg --force-depends -i package.debPartial Upgrade Recovery
If apt-get dist-upgrade or do-release-upgrade was interrupted:
# Resume the upgrade
dpkg --configure -a
apt-get -f install
apt-get dist-upgrade
# If specific packages block the upgrade
apt-get install -f
apt-get dist-upgrade --fix-brokenRebuild Package Database
If the dpkg database itself is corrupted:
# Backup
cp -r /var/lib/dpkg /var/lib/dpkg.backup
# Rebuild from available packages
dpkg --clear-avail
apt-get update
dpkg --configure -a
apt-get install -fCheck Package Integrity
# Verify installed files of a package
dpkg -V packagename
# List files owned by a package
dpkg -L nginx
# Find which package owns a file
dpkg -S /usr/sbin/nginxAfter Disk Full During Install
If the server ran out of disk space mid-install:
# Free up space first
apt-get clean # clear apt cache
journalctl --vacuum-size=200M # truncate logs
df -h # verify free space
# Then resume
dpkg --configure -a
apt-get -f installSee the Disk Full guide for more ways to free disk space.