Fixing a Broken apt Lock After an Interrupted Upgrade or Installation
Sometimes package management on Ubuntu or Debian gets stuck because apt
or dpkg
can’t acquire a lock. This usually happens if an upgrade, install, or update process was interrupted or if another process is still running in the background. The error looks like this:
E: Could not get lock /var/lib/apt/lists/lock - open (11: Resource temporarily unavailable)
E: Unable to lock directory /var/lib/apt/lists/
Here’s a short guide on how to safely resolve the problem.
Step 1 – Check which process is holding the lock
Use lsof
to see what’s holding the lock file:
sudo lsof /var/lib/apt/lists/lock
If you see an apt
or dpkg
process that shouldn’t be running, kill it:
sudo kill <PID>
You can also double-check with:
ps aux | grep -E 'apt|dpkg'
Step 2 – Remove stale lock files
Once you’re sure no apt
/dpkg
processes are running, remove the lock files:
sudo rm /var/lib/apt/lists/lock
sudo rm /var/cache/apt/archives/lock
sudo rm /var/lib/dpkg/lock-frontend
Step 3 – Reconfigure dpkg and fix broken packages
Clean up any half-installed packages:
sudo dpkg --configure -a
sudo apt install -f
Step 4 – Update and upgrade
Now you should be able to refresh the package lists and upgrade safely:
sudo apt update
sudo apt upgrade
sudo apt dist-upgrade
Step 5 – Clean up unused packages
Finish with a cleanup:
sudo apt autoremove -y
sudo apt autoclean
Conclusion
A stuck apt
lock can look serious, but it’s usually easy to fix. The key is to identify and stop any running processes, remove stale lock files, and then repair the package database. With these steps, you can get your package manager back into a healthy state and continue upgrading or installing software without issues.