This error occurs when apt or dpkg is locked by another package manager process, typically one that is stuck or running in the background. Fix it by identifying the blocking process, terminating it, and reconfiguring dpkg.
The dpkg package manager uses lock files to prevent concurrent package operations from corrupting the database. When this error appears, it means another process is holding one of these locks (usually /var/lib/dpkg/lock or /var/lib/dpkg/lock-frontend). This commonly happens when a previous package operation was interrupted, crashed, or is still running in the background. The lock file is a safety mechanism—removing it without stopping the blocking process can damage your system.
First, see if there are any active apt processes:
ps aux | grep -i aptIf you see active apt processes, wait for them to finish naturally. If you see only your grep command, proceed to the next step.
Use lsof to find the process holding the lock:
sudo lsof /var/lib/dpkg/lock
sudo lsof /var/lib/dpkg/lock-frontendNote the PID (Process ID) if one is returned. If no process is listed, the lock file may be stale.
If a process was found, kill it gracefully:
sudo kill PIDWait a few seconds. If the process doesn't terminate, force kill it:
sudo kill -9 PIDAlternatively, kill all stuck apt processes:
sudo pkill -9 apt
sudo pkill -9 apt-getOnce the blocking process is confirmed stopped, remove the lock files:
sudo rm /var/lib/dpkg/lock
sudo rm /var/lib/dpkg/lock-frontend
sudo rm /var/cache/apt/archives/lockWarning: Only remove lock files after confirming the blocking process is terminated. Removing them while a process is still running can corrupt your package database.
After removing locks, reconfigure dpkg to finish any pending operations:
sudo dpkg --configure -aThen update the package list:
sudo apt updateYou can now resume normal package operations.
If the above steps don't work, a system reboot will release all locks:
sudo rebootThis is the safest option if you're unsure about process termination. After reboot, dpkg will be in a clean state.
On Ubuntu systems, automatic background updates via apt-daily can trigger this error. To prevent it, disable automatic updates by editing /etc/apt/apt.conf.d/20auto-upgrades and setting:
APT::Periodic::Update-Package-Lists "0";
APT::Periodic::Download-Upgradeable-Packages "0";The Debian team strongly advises against simply removing lock files—always terminate the blocking process first. Lock files prevent corruption of the dpkg status database, which is critical to system stability. Container environments (Docker, Kubernetes) may need to handle stale locks in entrypoint scripts before running package operations.
E: Could not connect to proxy server
Could not connect to proxy server
E: Package 'package:i386' has no installation candidate
How to fix "Package package:i386 has no installation candidate" in apt
E: The value 'value' is invalid for APT::Default-Release
How to fix invalid APT::Default-Release value in APT
dpkg: error: unable to create new file 'path': Permission denied
How to fix dpkg permission denied errors in APT
subprocess installed post-removal script returned error exit status 1
How to fix "subprocess installed post-removal script returned error exit status 1" in APT