Broken Packages in Linux: 3 Proven Fixes and Costly Mistakes
"Broken packages" is one name for several different problems, and each has its own fix. A package can be unpacked but waiting for a dependency, half configured because its install script failed, or simply blocked because another program holds the package manager's lock. To repair broken packages safely you first need to know which one you have. I created all three on Debian 13 with small test packages, so every error message and fix below is real, and then checked the old post's Fedora and Arch commands.

Contents
First, find out what is broken
Two commands find broken packages without changing anything. apt-get check reports unmet dependencies, and dpkg --audit lists packages in a half-installed or half-configured state. Then look at the first two letters of dpkg -l for the package: ii is fine, iU means unpacked but not configured, iF means configuration failed. The dpkg manual lists every state letter.
Case 1: dpkg -i left unmet dependencies
This is the most common way people end up with broken packages: installing a downloaded .deb with dpkg -i. dpkg does not fetch dependencies:
dpkg: dependency problems prevent configuration of needs-cowsay:
needs-cowsay depends on cowsay; however:
Package cowsay is not installed.
dpkg: error processing package needs-cowsay (--install):
dependency problems - leaving unconfigured
Errors were encountered while processing:
needs-cowsay
The package stayed in state iU, one of the broken packages dpkg --audit reports, and from then on apt refused to install anything else, even an unrelated package like htop:
needs-cowsay : Depends: cowsay but it is not going to be installed
E: Unmet dependencies. Try 'apt --fix-broken install' with no packages (or specify a solution).
The message tells you the fix, and it worked. apt --fix-broken install (the long form of the old post's apt install -f) installed cowsay plus six packages it needs, then configured the waiting package:
Setting up cowsay (3.03+dfsg2-8) ...
Setting up needs-cowsay (1.0) ...
needs-cowsay postinst running
Read the list apt proposes before you confirm. Here it only installed packages; if the only way out is to remove something, --fix-broken will propose that too. To avoid this case, install local files with apt instead of dpkg (apt install ./package.deb, or a full path as in case 2 below), so apt can plan the dependencies before it unpacks anything.
Case 2: a failing install script
Install scripts cause the hardest broken packages. The second test package had a post-installation script that exits with an error, as happens when a script expects a service, user or file that is missing:
dpkg: error processing package bad-postinst (--configure):
installed bad-postinst package post-installation script subprocess returned error exit status 1
Errors were encountered while processing:
bad-postinst
E: Sub-process /usr/bin/dpkg returned an error code (1)
dpkg -l showed iF, and dpkg --audit said "The following packages are only half configured, probably due to problems configuring them the first time." This is where the usual advice stops working. dpkg --configure -a, the old post's third step, simply ran the same script again and failed the same way. apt --fix-broken install failed too, ending with "Error: Sub-process /usr/bin/dpkg returned an error code (1)". Neither command can fix a script that fails every time.
The real fix is to read the script's error, which appears just above the dpkg lines, and repair its cause: start the service it needs, create the missing file, free the port. The scripts live in /var/lib/dpkg/info/<package>.postinst. In the lab I made the script succeed and then dpkg --configure -a finished with "Setting up bad-postinst (1.0) ..." and state ii. Editing a maintainer script is a last resort for a package you understand, and the next upgrade replaces it anyway. If you just want the package gone, removing it is usually cleaner than patching its script.
Case 3: the dpkg lock is held
"Could not get lock" does not mean broken packages, but it is often described that way. With another process holding the lock, apt said exactly who it was:
E: Could not get lock /var/lib/dpkg/lock-frontend. It is held by process 3086 (python3)
E: Unable to acquire the dpkg frontend lock (/var/lib/dpkg/lock-frontend), is another process using it?
fuser -v /var/lib/dpkg/lock-frontend showed the same PID. On real systems that process is usually unattended-upgrades, a software center or a second terminal. Wait for it, or stop it cleanly; once it ended, the same apt-get install htop worked.
The old post told readers to delete lock files with rm /var/lib/apt/lists/lock and rm /var/cache/apt/archives/lock. Look at what those files are:
-rw-r-----. 1 root root 0 Nov 18 15:07 /var/cache/apt/archives/lock
-rw-r-----. 1 root root 0 Nov 18 15:07 /var/lib/apt/lists/lock
-rw-r-----. 1 root root 0 Nov 18 15:07 /var/lib/dpkg/lock
-rw-r-----. 1 root root 0 Nov 18 15:07 /var/lib/dpkg/lock-frontend
Empty files. The lock is held by a running process, not by the file's content, so deleting the file does not stop that process. It only lets a second apt run at the same time, which is exactly how a real half-configured system is made. Find the process; never remove the files.
Corrections to the old commands
dpkg -l | grep ^..rcounted 0 here and does not find broken packages: the third column ofdpkg -lis an error flag, shown as an uppercaseR(reinstall required). Usedpkg --auditinstead.- On Arch,
pacman -S --forcenow fails: "error: invalid option '--force'". - On Fedora (dnf5 5.4.8.0),
rpm -Vis the right tool to find a damaged package. After I appended a line to a bash-completion file it printedS.5....T. /usr/share/bash-completion/bash_completion(size, checksum and time changed).dnf reinstall bash-completionrestored it, andrpm -Vwas silent again.
Fix broken packages: the safe order
- Diagnose:
apt-get check,dpkg --audit,dpkg -l <package>. - Lock errors: find the process with the PID apt prints; wait or stop it.
- Unmet dependencies:
apt --fix-broken install, and read what it proposes. - Half-configured:
dpkg --configure -a; if it fails again, fix the cause shown in the script's own error. - Still stuck: remove the package (
apt remove, ordpkg --removefor a package apt cannot handle).
The apt-get manual documents check and --fix-broken. On Red Hat systems, undoing an upgrade that broke things is covered in yum downgrade, upgrade and rollback. If the package system is fine but desktop settings are broken, see resetting Ubuntu to default settings.
Comments
Post a Comment