What the message looks like
E: Could not get lock /var/lib/dpkg/lock-frontend. It is held by process 2314 (unattended-upgr)
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?Common causes
unattended-upgradesinstalls security updates in the background, often right after boot.- Another
aptordpkgcommand runs in a second terminal or a script. - A graphical updater (PackageKit, Software Updater) is active.
- A previous run was killed and left a stale lock and an interrupted dpkg state.
Check
- Which process holds the lock?
sudo lsof /var/lib/dpkg/lock-frontend - Package tools that are running
ps aux | grep -E '[a]pt|[d]pkg|[u]nattended' - Is the automatic updater active?
systemctl status unattended-upgrades --no-pager
Fix
- Wait a few minutes. If a process is shown, it is working. Run your command again afterwards.
- If you must continue now, stop the updater cleanly:
sudo systemctl stop unattended-upgrades, once it is idle. - Only if no process holds the lock, repair the state:
sudo dpkg --configure -a, thensudo apt update. - Remove the lock files only as a last resort and only when you are sure nothing uses them:
sudo rm /var/lib/dpkg/lock-frontend /var/lib/dpkg/lock. - In scripts, use
apt-get -o DPkg::Lock::Timeout=120 …so the command waits instead of failing.
Typical scenario
Your provisioning script (Ansible, cloud-init or plain shell) runs apt install right after the first boot of a fresh Ubuntu cloud instance. About every third run fails with "Could not get lock /var/lib/dpkg/lock-frontend … held by process 2314 (unattended-upgr)". Nothing is broken; the system is applying security updates. Use ps -p 2314 -o pid,etime,cmd to see what the named process ID is doing and how long it has been running.
FAQ
Is it safe to delete the lock file?
Not while a process uses it. Deleting a lock under a running dpkg can corrupt the package database. Wait, or kill nothing before you know what runs.
Why does it happen right after boot?
The system checks for security updates shortly after start. Give it a few minutes, or wait for unattended-upgrades to finish.
How can I make a script wait for the lock automatically?
Use apt-get -o DPkg::Lock::Timeout=300 install package; apt then waits up to 300 seconds instead of failing at once. The option exists since apt 1.9.11, roughly Ubuntu 20.04 and Debian 11. On older systems you need a loop that waits on fuser or lsof of the lock file.
What is the difference between lock-frontend and lock?
lock-frontend is taken by package-management front ends such as apt or the software updater; lock is taken by dpkg itself while unpacking and configuring. The message names one or the other depending on the phase, and in both cases a running process holds it.
Should I disable unattended-upgrades so this stops happening?
Usually not, because it installs security updates automatically. It is better to wait for the lock in your script or to run installs after the first update pass. If you replace the service on purpose, you need another reliable way to apply security updates.