An interrupted system upgrade or a conflicting third-party repository leaves Linux package managers feeling wedged right when you need a patch applied. The worst advice floating around old forum threads tells people to blindly run rm /var/lib/dpkg/lock* or slam -y onto dependency-fixing commands. On hosts I administer, both shortcuts turn a five-minute repair into a torn /var/lib/dpkg/status file or a wiped desktop environment.
This guide covers Ubuntu and Debian (apt, apt-get, dpkg) alongside Fedora 41+ (dnf5 and rpm).
Diagram: Original SVG schematic by Ajay Kumar Chaturvedi.
Prerequisites and Safety Checks
Confirm these items before running package repairs:
sudoprivileges and working network/DNS access to your distribution mirrors.- Free disk space on
/and/boot(df -h / /boot), because a full/bootpartition is one of the top reasonsdpkganddnffail mid-upgrade! - Check Fedora's DNF documentation if you are on an Atomic/immutable variant (Silverblue/Kinoite), where
rpm-ostreemanages updates instead ofdnf5.
Backup and Destructive Warning: Package repair commands (--fix-broken,--allowerasing,rpmdb --rebuilddb) can remove conflicting packages or rewrite system databases. Back up/var/lib/dpkg/status(Ubuntu/Debian) or/var/lib/rpm(Fedora) before manual repairs, always run dry-run flags (-sor--assumeno) first, and read every package marked for removal before typingy.
Step-by-Step Procedure
Step 1: Check for Active Lock Holders Before Touching Locks
Target OS: Ubuntu, Debian (apt/dpkg), and Fedora (dnf5).
If apt fails with Could not get lock /var/lib/dpkg/lock-frontend, a background unattended-upgrades or apt-daily service may legitimately be finishing an install. Inspect the lock files using the `fuser(1)` manual:
# Ubuntu / Debian: show PIDs holding dpkg or apt locks
sudo fuser -v /var/lib/dpkg/lock-frontend /var/lib/dpkg/lock /var/lib/apt/lists/lockIf a PID appears, inspect what it is doing (ps -fp <PID>) and wait for it to finish. Never delete lock files while a process owns them. On Fedora, check ps aux | grep -E 'dnf|rpm|packagekit' and let active background transactions exit cleanly.
Step 2: Repair Interrupted Configurations and Broken Dependencies on Ubuntu/Debian
Target OS: Ubuntu 24.04/26.04 LTS and Debian 12/13.
First back up the dpkg status file so you have a clean rollback snapshot:
sudo cp /var/lib/dpkg/status /var/lib/dpkg/status.bakNow ask dpkg (per the `dpkg(1)` manual) to finish configuring any packages that were unpacked before the failure:
sudo dpkg --configure -aNext, simulate dependency repair using -s (--simulate / --dry-run, documented in the `apt-get(8)` manual) so you see every Conf and Remv line without changing disk state:
sudo apt-get -s --fix-broken installRead the simulated Remv list carefully. If it looks sensible and is not stripping critical metapackages or your desktop session, apply the repair and refresh indexes:
sudo apt --fix-broken install
sudo apt updateStep 3: Clear Corrupt Caches and Unblock Held Packages on Ubuntu/Debian
Target OS: Ubuntu and Debian.
If apt reports hash sum mismatches or held-back transitions, clear cached .deb archives and inspect package holds with the `apt-mark(8)` manual:
sudo apt-get clean
apt-mark showholdIf a forgotten hold blocks an essential dependency, remove it selectively after checking why it was pinned:
sudo apt-mark unhold <package-name>I would skip adding third-party PPAs or external .deb pins as a quick workaround when apt complains about broken versions; removing or disabling the conflicting /etc/apt/sources.list.d/*.sources entry and running sudo apt update usually clears dependency knots cleanly.
Step 4: Diagnose and Sync Broken Dependencies on Fedora (dnf5)
Target OS: Fedora 41 and newer (dnf5).
Start read-only by checking the local RPM package database with the `dnf5 check` command for missing dependencies, conflicts, or duplicate installed versions:
dnf5 checkIf mirror metadata is stale or partially synced, wipe local caches via the `dnf5 clean` command and rebuild the cache:
sudo dnf5 clean all
sudo dnf5 makecacheTo realign mismatched package versions across enabled repositories without guessing, run a dry-run with --assumeno first before considering --allowerasing (documented in the `dnf5 distro-sync` command):
sudo dnf5 distro-sync --assumenoIf the output requires removing an obsolete conflict and the removal list is safe, re-run with --allowerasing (or use sudo dnf5 upgrade --skip-broken temporarily while waiting for a mirror to catch up).
Step 5: Undo a Bad DNF5 Transaction or Rebuild a Damaged RPM Index
Target OS: Fedora 41 and newer.
If an upgrade just broke an application or installed conflicting packages, inspect recent transactions and revert the bad transaction using the `dnf5 history` command:
dnf5 history list
dnf5 history info last
sudo dnf5 history undo last --assumenoIf the dry-run looks clean, drop --assumeno to apply the rollback. Only if dnf5 or rpm fails due to an unreadable RPM database error, back up /var/lib/rpm first and rebuild database indexes from installed headers per the `rpmdb(8)` manual:
sudo cp -a /var/lib/rpm /var/lib/rpm.bak
sudo rpmdb --rebuilddbVerification and Expected Result
Run each distribution's built-in integrity check with zero pending repairs:
# Ubuntu / Debian
sudo apt-get check
# Fedora 41+
dnf5 checkExpected result: apt-get check prints Reading package lists... Done, Building dependency tree... Done, and exits 0 with no broken packages; dnf5 check exits 0 with empty output.
Troubleshooting
- `/boot` is 100 percent full, causing `dpkg` or `dnf` kernel post-install script failure: Check
df -h /boot, delete stale crash dumps or keep only the current and one fallback kernel (sudo apt autoremove --purgeafter checking the removal list on Ubuntu, orsudo dnf5 remove --oldinstallonlycarefully after verifyinguname -r). - Two versions of the same package appear in `dnf5 check --duplicates`: Inspect why an upgrade aborted mid-transaction with
dnf5 history info last, finish any pending cleanup, and avoid killingdnf5during the RPM scriptlet phase. - Unmet dependencies persist after disabling a third-party repo: Run
apt policy <package>(Ubuntu/Debian) ordnf5 repoquery --installed <package>(Fedora) to spot leftover foreign packages that still pin outdated shared libraries.
