Apt Lock File Could Not Be Locked – Quick Fixes
You can't run apt because a lock file is stuck. Kill the rogue process or delete the stale file. Here's the fix.
1. A Rogue apt or dpkg Process Is Running
This is the most common cause. You were in the middle of an install or update, then closed the terminal or lost SSH. The process keeps the lock, and apt won't start a second instance.
Check for the offending process
Run this:
ps aux | grep -E 'apt|dpkg'
Look for lines like apt install, dpkg --configure, or apt-get update. The second column is the PID.
Kill it
Use SIGTERM first — it's polite:
sudo kill 1234
If it doesn't die after 5 seconds, use SIGKILL:
sudo kill -9 1234
Don't use -9 unless you have to. It can leave the package database in a dirty state. You'll know it worked when you run sudo apt update and it finishes cleanly.
2. A Stale Lock File from a Previous Crash
If no apt or dpkg process is running, the lock file itself is probably leftover from a system crash or a failed script. The file sits there, and apt refuses to overwrite it.
Locate the lock files
There are two main ones:
/var/lib/dpkg/lock-frontend
/var/lib/apt/lists/lock
/var/cache/apt/archives/lock
Remove them — but carefully
First, double-check no process is using them:
sudo lsof /var/lib/dpkg/lock-frontend
sudo lsof /var/lib/apt/lists/lock
sudo lsof /var/cache/apt/archives/lock
If lsof returns nothing, delete the files:
sudo rm /var/lib/dpkg/lock-frontend
sudo rm /var/lib/apt/lists/lock
sudo rm /var/cache/apt/archives/lock
Then reconfigure dpkg if it was interrupted:
sudo dpkg --configure -a
Now run sudo apt update. If it works, you're done. If it throws another lock error, check for step 1 again — sometimes the process recreated the lock after you killed it in a weird state.
3. Lock File Held by a Background Update (unattended-upgrades)
This one trips up a lot of people. Ubuntu and Debian run unattended-upgrades in the background. It's the process that auto-installs security patches. You try to run apt install at the same time, and you get the lock error.
Check if it's running
ps aux | grep unattended
If you see unattended-upgrades or apt.systemd.daily, wait. It usually finishes within a few minutes. On a slow VPS or Raspberry Pi, it can take up to 10 minutes.
Disable it temporarily (not recommended long-term)
If you're in a hurry, stop the service:
sudo systemctl stop unattended-upgrades
Do your install, then start it again:
sudo systemctl start unattended-upgrades
Don't disable it permanently unless you have another update system in place. Unpatched servers get owned.
Quick-Reference Summary Table
| Cause | Check | Fix |
|---|---|---|
| Rogue process | ps aux | grep -E 'apt|dpkg' |
sudo kill <PID> then sudo kill -9 <PID> if needed |
| Stale lock file | sudo lsof /var/lib/dpkg/lock-frontend |
sudo rm /var/lib/dpkg/lock-frontend and similar |
| Background upgrades | ps aux | grep unattended |
Wait, or sudo systemctl stop unattended-upgrades |
One more thing: after you fix it, always runsudo dpkg --configure -abeforesudo apt update. That step saves you from half-broken package states.
Was this solution helpful?