Recommended resources
The freely available resources this lesson will direct you to.
Hello. In the previous lesson, you learned to validate package provenance, downloaded package integrity, and installed-file integrity. Recovery begins when a transaction did not reach its intended final state: a download was interrupted, a maintainer script failed, dependency versions no longer fit together, or another package-management process owns the database lock.
For LFCS work, do not treat recovery as a memorized pair of commands. The reliable approach is to identify the current package state, remove the underlying obstacle, let one package manager repair the transaction, then verify that the database is coherent again. This lesson covers that workflow for interrupted APT and dpkg operations, broken dependencies, locks, and partially configured packages.
Understand what APT and dpkg were doing when failure occurred
APT is the high-level package manager. It chooses versions, resolves dependencies, downloads archives, and then invokes dpkg. dpkg is the lower-level installer: it unpacks files, records package state in its database, runs package-maintainer scripts, processes triggers, and marks packages configured.
An interruption can occur at several distinct points:
| Point of failure | Likely state | Meaning |
|---|---|---|
| Before unpacking begins | Package not installed | Usually no package-state repair is needed. |
| During file unpacking | half-installed |
Installation began but did not finish. |
| After unpacking, before configuration | unpacked |
Package files are present, but its configuration scripts have not completed. |
| During configuration | half-configured |
A maintainer script, trigger, dependency, disk, or service problem interrupted configuration. |
| After configuration | installed |
Package is correctly unpacked and configured. |
The important distinction is that unpacking files is not the same as configuring the package. A package can have its executable files present while its service account, systemd unit actions, database initialization, cache generation, or trigger processing remains incomplete.
The authoritative dpkg manual is worth reading here because it describes the states that recovery commands are designed to resolve.
Read the Debian dpkg manual sections that define incomplete package states and the two principal diagnostic and repair actions. This supplies the precise meaning behind “partially configured.”
In “Package states,” read from the incomplete states through the installed state. Focus on the difference between half-installed, unpacked, and half-configured. Then read the complete “--configure package...|-a|--pending” entry, including its explanation of unpacked packages and maintainer scripts. Within that entry, note configuration work: configuration can modify system state, so it must be retried only after the actual failure is understood. Finally, in the “-C, --audit” entry, read from the audit description. Treat dpkg --audit as a diagnostic report, not as a repair action.
Start a recovery investigation with the package database itself:
sudo dpkg --audit
This reports packages that are partially installed, incompletely configured, or have inconsistent control metadata. Read its output carefully. If it names a package, do not assume the named package is the root cause; it may simply be the first package whose configuration could not complete.
For a focused view of a particular package’s state:
dpkg-query -W -f='${db:Status-Abbrev} ${binary:Package} ${Version}\n' PACKAGE
The three-character status abbreviation is useful evidence, but dpkg --audit is usually clearer during an incident because it explains which incomplete states need attention.
Before repair, preserve the original error message when possible. Package failures are often caused by conditions outside the package database:
- no free space or no inodes on
/,/var, or/tmp; - a filesystem remounted read-only after an I/O error;
- a failed service startup in a package’s
postinstscript; - a missing dependency or incompatible repository version;
- an invalid local configuration file;
- a lost network connection while packages were needed.
Useful supporting evidence includes:
df -h / /var /tmp
df -i / /var /tmp
sudo tail -n 80 /var/log/dpkg.log
sudo tail -n 80 /var/log/apt/term.log
If a package configuration script attempted to start a service and failed, inspect that service rather than repeatedly rerunning package commands:
systemctl status SERVICE
sudo journalctl -u SERVICE -b --no-pager
A repeated dpkg --configure -a cannot correct an invalid service configuration, a full filesystem, or a repository that no longer provides a required version.
The standard repair cycle
Once you know no other package manager is running, the usual recovery cycle has a deliberate order.
-
Audit the current state.
sudo dpkg --audit -
Attempt to finish configuration already waiting in dpkg.
sudo dpkg --configure -aThis configures every package that has been unpacked but not fully configured. It is the primary recovery action after an interrupted installation or upgrade.
-
If dependencies prevent configuration, simulate a dependency repair.
sudo apt-get -s --fix-broken installRead the simulation. A repair might install missing dependencies, complete an upgrade, or in some cases propose removing packages. Do not approve a proposed removal merely because the command is called “fix broken.”
-
Run the reviewed repair.
sudo apt-get --fix-broken installThe short form is also common:
sudo apt-get -f install -
Finish any configuration that remains pending and validate.
sudo dpkg --configure -a sudo apt-get check sudo dpkg --audit
apt-get check checks dependency satisfaction without changing packages. A clean exit from apt-get check and no output from dpkg --audit are strong indications that package dependencies and dpkg state are coherent again.
The precise order can change based on the error:
- If
dpkg --configure -asucceeds, dependency repair may not be needed. - If
dpkg --configure -areports unmet dependencies, run the simulatedapt-get --fix-broken install, then the actual repair. - If
apt-get --fix-broken installcannot find a suitable package version, resolve the repository, pinning, hold, or version conflict before trying again. - If a maintainer script fails for a non-dependency reason, correct that reason and retry configuration.
For example, an interrupted installation of a locally downloaded .deb may leave the package unpacked with missing dependencies. APT can often retrieve those dependencies from configured repositories:
sudo dpkg --configure -a
sudo apt-get -s --fix-broken install
sudo apt-get --fix-broken install
sudo dpkg --configure -a
Conversely, if a package’s postinst script fails because a daemon cannot start due to an invalid file under /etc, dependency repair will not solve it. Correct or restore the configuration, verify the service manually if appropriate, and then retry only the affected package:
sudo dpkg --configure PACKAGE
Treat package-manager locks as coordination, not corruption
APT and dpkg locks prevent two writers from changing package state concurrently. A lock error is therefore usually evidence that the protection is working.
Common lock paths include:
/var/lib/dpkg/lock-frontendfor APT frontends;/var/lib/dpkg/lockfor the dpkg database;/var/cache/apt/archives/lockfor the package archive cache;/var/lib/apt/lists/lockwhile repository indexes are updated.
Do not delete lock files as a first response. A lock is associated with a running process and an open file descriptor; deleting the pathname does not safely terminate the process that holds the lock. It can instead permit a second package manager to proceed while the first is still modifying package data.
Use the PID from the error message when one is given:
ps -p PID -o pid,ppid,stat,etime,cmd
If the error has no PID, identify processes holding the common package locks:
sudo lsof \
/var/lib/dpkg/lock-frontend \
/var/lib/dpkg/lock \
/var/cache/apt/archives/lock \
/var/lib/apt/lists/lock
Or use fuser for a specific lock:
sudo fuser -v /var/lib/dpkg/lock-frontend
On Ubuntu, background update jobs are frequent legitimate lock holders:
systemctl status apt-daily.service apt-daily-upgrade.service unattended-upgrades.service
systemctl list-timers apt-daily.timer apt-daily-upgrade.timer
If an update is active and making progress, wait for it. Examine its journal if you need evidence of progress or failure:
sudo journalctl -u apt-daily.service \
-u apt-daily-upgrade.service \
-u unattended-upgrades.service \
--since "30 minutes ago"
The following practical guide provides a sound lock-handling sequence. Its key operational rule matches production practice: find the process, wait when it is healthy, stop it gracefully only when evidence shows it is stuck, then repair the transaction.
How to Fix APT and dpkg Lock Errors on Ubuntu and Debian | Linuxize
Read this Linuxize guide for a concise operational workflow for identifying package-lock owners, checking automatic updates, and recovering only after a process has stopped.
In “Step 1: Check the Process Holding the Lock,” read the explanation and commands for ps, lsof, and fuser. Pay special attention to the lock-file warning: the file’s presence is not proof of an active lock, and removing it is not a safe repair. Then read “Step 2: Check Automatic Updates” and “Step 3: Stop a Stuck Package Process.” Read from safe process termination. Distinguish a slow but active update from a genuinely stalled process. Finish with “Step 4: Repair Interrupted Package Configuration” and the troubleshooting note “dpkg --configure -a reports dependency problems.” Read from the post-lock recovery steps.
If a process is demonstrably stuck, first request normal termination:
sudo kill PID
This sends SIGTERM, allowing the process an opportunity to exit cleanly. Confirm that it is gone before proceeding:
ps -p PID
If a systemd update service owns the work, stop the service rather than killing an arbitrary child process:
sudo systemctl stop apt-daily.service apt-daily-upgrade.service unattended-upgrades.service
Then verify that no process owns the lock and perform the package repair cycle:
sudo fuser -v /var/lib/dpkg/lock-frontend
sudo dpkg --configure -a
sudo apt-get --fix-broken install
Avoid SIGKILL unless normal termination has failed and you have established that the process is not progressing. A forced termination makes incomplete package configuration more likely. A process in uninterruptible I/O sleep can also indicate a storage problem; investigate filesystem and kernel evidence rather than cycling package commands or rebooting impulsively.
If no process holds the lock, leave the lock file alone. Lock files commonly remain on disk even when no process has an active lock. The relevant question is whether a process has the file open and locked, not whether the path exists.
For unattended automation, a bounded lock wait is preferable to deleting a lock or racing another transaction:
sudo apt-get -o DPkg::Lock::Timeout=60 install PACKAGE
This waits up to 60 seconds for the dpkg lock. If the timeout expires, the script should report the failure and stop.
Diagnose dependency failures rather than forcing past them
A dependency failure means that the versions APT can currently select do not meet all declared package relationships. Typical causes include:
- a direct
dpkg -iinstallation whose dependencies were not installed; - a partially completed distribution or package upgrade;
- a disabled, removed, or unreachable repository;
- mixing release suites or third-party repositories;
- a package held at an incompatible version;
- a package removal that was interrupted.
Start with a non-modifying check:
sudo apt-get check
sudo dpkg --audit
Then inspect version selection for the package named in the error and its missing dependency:
apt-cache policy PACKAGE DEPENDENCY
apt-mark showhold
A held package is not automatically wrong. It may represent an intentional operational decision. But if a hold blocks a required upgrade, remove it only after confirming the intended version path:
sudo apt-mark unhold PACKAGE
Likewise, do not use --force options to bypass dependency checks. They can install a package database state that appears complete to dpkg but is functionally incompatible at runtime.
If APT reports that a required version has “no installation candidate,” check the configured repositories and refresh their metadata:
sudo apt update
apt-cache policy PACKAGE
A signature or repository error during apt update is a source-configuration problem, not a reason to use insecure repository options. Resolve the bad source, trust configuration, network issue, or release mismatch first. The provenance checks from the previous lesson remain part of recovery: a broken dependency must not lead you to install an arbitrary .deb from an unverified source.
Do not run apt autoremove as a reflex while dependency state is broken. It can remove automatically installed packages that appear unused only because a transaction is incomplete. First restore a consistent state; then simulate any cleanup separately.
When a package is marked “reinstallation required”
Occasionally, dpkg --audit reports a package that is badly damaged or marked as requiring reinstallation. Prefer reinstallation from a trusted configured repository when the candidate version is appropriate:
apt-cache policy PACKAGE
sudo apt-get -s install --reinstall PACKAGE
sudo apt-get install --reinstall PACKAGE
sudo dpkg --configure PACKAGE
The simulation matters because reinstalling can interact with dependencies and may select a newer candidate than the currently damaged version.
Forcing removal with --force-remove-reinstreq is a last-resort measure, not a normal recovery command:
sudo dpkg --remove --force-remove-reinstreq PACKAGE
This can cause dpkg to forget package-managed files that remain on disk. Never use it casually on core packages, boot components, package-management components, or services whose data and dependencies you have not assessed. If removal is justified, follow it with a normal APT repair and audit:
sudo apt-get -s --fix-broken install
sudo apt-get --fix-broken install
sudo dpkg --audit
The safer general principle is: restore a package when possible; force removal only when the package cannot be restored and you have a recovery plan.
A compact LFCS recovery runbook
When an APT or dpkg operation fails, use this sequence as a disciplined baseline.
-
Read and preserve the terminal error. Check free space, filesystem health symptoms, and relevant service logs.
-
Identify any package-manager lock holder with the PID,
lsof, orfuser. -
Wait for a legitimate active process. Stop only a proven-stuck process, preferably through its systemd service.
-
Audit dpkg state:
sudo dpkg --audit -
Attempt pending configuration:
sudo dpkg --configure -a -
If dependencies are broken, inspect repository selection and holds, then simulate repair:
sudo apt-get -s --fix-broken install -
Apply the reviewed repair:
sudo apt-get --fix-broken install sudo dpkg --configure -a -
Validate completion:
sudo apt-get check sudo dpkg --audit -
Retry the original, now-understood operation only after these checks are clean.
You now have a recovery model rather than a collection of emergency commands. dpkg --audit identifies incomplete state; dpkg --configure -a completes unpacked packages; apt-get --fix-broken install resolves dependency gaps under APT’s control; and lock diagnosis ensures that only one writer touches package state at a time. The key safety rule is to correct the underlying cause before retrying configuration.
Next, the course moves from package administration to change tracking: initializing or cloning a Git repository and recording an administrative configuration change with status, add, and commit.