Issue#
Running apt install or apt upgrade on Debian/Ubuntu fails right away, often just after a server boots:
E: Could not get lock /var/lib/dpkg/lock-frontend. It is held by process 1234 (apt-get)
N: Be aware that removing the lock file is not a solution and may break your system.
E: Unable to acquire the dpkg frontend lock (/var/lib/dpkg/lock-frontend), is another process using it?
Cause#
Only one process can use the dpkg database at a time, so apt and dpkg take locks under /var/lib/dpkg/ and /var/lib/apt/lists/. The usual holders are:
unattended-upgradesorapt-daily.servicerunning in the background, typically a few minutes after boot.- Another
apt/apt-getsession, possibly in a different SSH or tmux window. - A previous apt run that was killed and left dpkg half-configured.
Deleting the lock files while another process still uses the database can corrupt it. Do not delete them.
Solution#
- Find which process holds the lock:
sudo lsof /var/lib/dpkg/lock-frontend /var/lib/dpkg/lock /var/lib/apt/lists/lock
# or
sudo fuser -v /var/lib/dpkg/lock-frontend
ps -fp 1234
- If it is
unattended-upgradesorapt-daily, it is safest to wait for it to finish. You can follow its progress:
systemctl status apt-daily.service apt-daily-upgrade.service unattended-upgrades.service
sudo tail -f /var/log/unattended-upgrades/unattended-upgrades-dpkg.log
- If you cannot wait, stop the timers and service cleanly. Do not use
kill -9:
sudo systemctl stop apt-daily.timer apt-daily-upgrade.timer
sudo systemctl stop apt-daily.service apt-daily-upgrade.service unattended-upgrades.service
- If the holder is a hung apt process you started yourself, end it gracefully first:
sudo kill 1234 # SIGTERM; give it a few seconds
- If no process holds the lock any more, but apt complains that dpkg was interrupted, finish the pending configuration:
sudo dpkg --configure -a
sudo apt-get install -f
- Re-enable the timers if you stopped them:
sudo systemctl start apt-daily.timer apt-daily-upgrade.timer
Tip: In scripts or cloud-init, use apt's built-in wait instead of a sleep loop:
sudo apt-get -o DPkg::Lock::Timeout=300 install -y <pkg>(apt 1.9.11 and later).
Verify#
Nothing should be holding the lock now:
sudo lsof /var/lib/dpkg/lock-frontend || echo "lock is free"
sudo dpkg --audit && echo "dpkg database is consistent"
sudo apt-get update && sudo apt-get check
dpkg --audit prints nothing when every package is fully installed, and apt-get check finishes without errors.