Fix "E: Could not get lock /var/lib/dpkg/lock-frontend" (apt, Ubuntu, Debian)
E: Could not get lock /var/lib/dpkg/lock-frontend. It is held by process 1234 (unattended-upgr)
- E: Unable to acquire the dpkg frontend lock (/var/lib/dpkg/lock-frontend), is another process using it?
- E: Could not get lock /var/lib/dpkg/lock. It is held by process 5678 (apt-get)
- E: Could not get lock /var/lib/apt/lists/lock. It is held by process 910 (apt)
- E: Unable to lock directory /var/lib/apt/lists/
- E: Could not get lock /var/lib/dpkg/lock - open (11: Resource temporarily unavailable)
Another program is already using apt or dpkg, most often automatic updates (unattended-upgrades) right after boot. Wait for it to finish or close the other package manager. Do not delete the lock files: while the process is running, that can corrupt the package database.
What this error means
apt and dpkg place a lock on files such as /var/lib/dpkg/lock-frontend so two programs never modify the package database at the same time. The kernel holds this lock as long as the process that took it is alive, and it goes away by itself when that process ends. The message gives its PID and name, truncated to 15 characters (unattended-upgr for unattended-upgrades). The lock file always exists, which is normal: deleting it releases nothing and lets a second apt work alongside the first, hence the warning "removing the lock file is not a solution".
Common causes
- Automatic updates:
unattended-upgradesand theapt-dailytimers often run a few minutes after boot, especially on a fresh cloud VM. - Another package manager open: Software Updater or Ubuntu Software (
packagekitd), Synaptic, or anaptstarted in another terminal or SSH session. - Provisioning in progress: cloud-init is still installing packages on first boot while your script already runs
apt install. - Stuck process: an
aptwaiting for an answer to a prompt (debconf) in another session, or a frozen installation.
Step-by-step fix
-
Identify the process
See which program holds the lock and for how long. Replace 1234 with the PID from the message.
ps -o pid,etime,cmd -p 1234 sudo fuser -v /var/lib/dpkg/lock-frontend /var/lib/dpkg/lock pgrep -a 'apt|dpkg|unattended' -
Wait for automatic updates to finish
If it is
unattended-upgradesorapt-daily, let it finish: it usually takes a few minutes. You can follow its progress in the logs.sudo tail -f /var/log/unattended-upgrades/unattended-upgrades.log sudo tail -f /var/log/dpkg.log -
In a script: wait for the lock instead of failing
apt can wait for the lock to be released with the
DPkg::Lock::Timeoutoption (in seconds). On a cloud VM, also wait for cloud-init to finish before installing.cloud-init status --wait sudo apt-get -o DPkg::Lock::Timeout=600 update sudo apt-get -o DPkg::Lock::Timeout=600 install -y nginx -
Close the other package manager
Close Software Updater, Ubuntu Software or Synaptic, and cleanly finish the
aptin the other terminal (answer its prompt if it is waiting for one). -
Truly stuck process: stop it cleanly
Only if the process has made no progress for a long time (nothing new in
/var/log/dpkg.log), stop it with a normal signal, notkill -9in the middle of an installation. Then repair half-configured packages.sudo systemctl stop unattended-upgrades # or, for another process sudo kill 1234 sudo dpkg --configure -a sudo apt-get install -f -
Deleting a lock: only as a very last resort
It is almost never needed: a reboot releases every lock. Only delete the files if
fuserandpgrepconfirm that no apt or dpkg process is running and the error persists. Then immediately rundpkg --configure -a.# Both must print nothing sudo fuser -v /var/lib/dpkg/lock-frontend /var/lib/dpkg/lock /var/lib/apt/lists/lock pgrep -a 'apt|dpkg' sudo rm /var/lib/dpkg/lock-frontend /var/lib/dpkg/lock /var/lib/apt/lists/lock /var/cache/apt/archives/lock sudo dpkg --configure -a
Did this fix work for you?
Confirm it in one click: it will rise for the next developers stuck on the same error.
Keep it from coming back
- In provisioning scripts, use
cloud-init status --waitand-o DPkg::Lock::Timeout=600rather thansleeploops. - Do not disable
unattended-upgradesto avoid the error: you would lose automatic security fixes. Adjust the schedule of theapt-dailyandapt-daily-upgradetimers instead. - Run only one package manager at a time on a machine.
Frequently asked questions
Can I delete /var/lib/dpkg/lock-frontend?
Not while an apt or dpkg process is running: two simultaneous installs can corrupt the package database. Wait, or stop the process cleanly. Deleting it is only justified after checking that no process holds the lock.
What is unattended-upgr?
The truncated name of unattended-upgrades, the service that automatically installs security updates on Ubuntu and Debian.
The error comes back after a reboot, why?
Automatic updates start again at boot. Wait a few minutes, then run sudo dpkg --configure -a if a previous installation had been interrupted.
Useful tools for this error
More Systems (Windows, macOS, Linux) errors
- Fix "command not found" and "is not recognized as an internal or external command"
- Fix "running scripts is disabled on this system" in PowerShell
- Fix "xcrun: error: invalid active developer path" on macOS
- Fix "SSL certificate problem: unable to get local issuer certificate" (git, curl, pip, npm)
- Fix "user is not in the sudoers file. This incident will be reported."
- Fix "/bin/bash^M: bad interpreter: No such file or directory"
- Fix "App is damaged and can't be opened" on macOS (Gatekeeper)