Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Servers, nginx and Docker

apt refuses to run because another process holds the dpkg lock

Your error

Could not get lock /var/lib/dpkg/lock-frontend. It is held by process 1234 (unattended-upgr)2 more lines it can show

Fix it fast

Most common cause, 1 of 3

unattended-upgrades is installing security updates

Let the run finish; it releases the lock when it exits.

Fix
sudo apt-get -o DPkg::Lock::Timeout=600 install -y nginx
# optional, for short-lived machines:
sudo systemctl edit apt-daily.timer        # add [Timer] Persistent=false
sudo systemctl edit apt-daily-upgrade.timer
Did this fix it?
Full steps for this cause: check, fix, verify

Short answer

Unable to acquire the dpkg frontend lock, is another process using it? means another package manager is running right now. On Ubuntu that is usually unattended-upgrades, cloud-init on a fresh VM, or a second apt or packagekitd. Let it finish, or make apt wait with -o DPkg::Lock::Timeout=600. Never delete the lock file. Applies to apt 2.8 and 3.x.

  • Applies to Ubuntu 24.04 LTS (apt 2.8.4), Ubuntu 26.04 LTS (apt 3.2.1), cloud-init
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Which cause do you have?

The message already names the PID and, in brackets, the process name. Look at that process, how long it has run and what started it; the name and the parent tell the three causes apart.

Run this first
ps -o pid,ppid,etime,cmd -p 1234

Then find the line you see in this list and open its fix:

  1. 1unattended-upgrades is installing security updatesThe process in brackets is unattended-upgr, often right after the machine boots
  2. 2cloud-init is still installing packages on a new instanceThe holder is apt-get or dpkg, its parent chain leads to cloud-init, and the server was created minutes ago
  3. 3You or a tool started a second apt, dpkg or software updaterThe holder is apt, apt-get, dpkg or packagekitd, started from another terminal, a desktop updater or an automation task

What does the error look like?

  • Could not get lock /var/lib/dpkg/lock-frontend. It is held by process 1234 (unattended-upgr)
  • Be aware that removing the lock file is not a solution and may break your system.
  • Unable to acquire the dpkg frontend lock (/var/lib/dpkg/lock-frontend), is another process using it?

Cause 1 of 3

unattended-upgrades is installing security updates

You seeThe process in brackets is unattended-upgr, often right after the machine boots

Check

In this case the dpkg frontend lock belongs to Ubuntu's automatic updates. The apt-daily.timer and apt-daily-upgrade.timer units start them on a schedule, and when the machine was off at that time the timer fires soon after the next boot, which is exactly when people log in and run apt.

Check
systemctl status apt-daily.service apt-daily-upgrade.service --no-pager

Fix

Let the run finish; it releases the lock when it exits. For scripts, ask apt to wait for the lock instead of failing at once. On VM images that only start for short jobs, Ubuntu's guide suggests Persistent=false in an override of the timers so a missed run is not caught up at boot.

Fix
sudo apt-get -o DPkg::Lock::Timeout=600 install -y nginx
# optional, for short-lived machines:
sudo systemctl edit apt-daily.timer        # add [Timer] Persistent=false
sudo systemctl edit apt-daily-upgrade.timer

Verify

apt-get prints Waiting for cache lock while the update runs, then installs the package once the lock is free.

Verify
systemctl is-active apt-daily-upgrade.service

Confirmed by documentation.ubuntu.com

Did this fix it?

Cause 2 of 3

cloud-init is still installing packages on a new instance

You seeThe holder is apt-get or dpkg, its parent chain leads to cloud-init, and the server was created minutes ago

Check

If the lock holder descends from cloud-init, the instance's user data is still being applied. Its package module can update, upgrade and install packages through apt during the first boot, so provisioning scripts that start right away collide with it.

Check
cloud-init status --long

Fix

Block until cloud-init has finished before the first apt command in your provisioning or CI step.

Fix
cloud-init status --wait
sudo apt-get install -y nginx

Verify

cloud-init status reports done, and the apt command runs without the lock error.

Verify
cloud-init status

Confirmed by docs.cloud-init.io

Did this fix it?

Cause 3 of 3

You or a tool started a second apt, dpkg or software updater

You seeThe holder is apt, apt-get, dpkg or packagekitd, started from another terminal, a desktop updater or an automation task

Check

apt takes these locks as fcntl record locks, and the kernel releases such a lock when the process that holds it ends. So the lock files being present on disk is normal; the error only means a live process has them. Two shells, two automation tasks or a desktop software updater using apt at the same moment produce exactly this.

Check
ps -o pid,etime,cmd -C apt,apt-get,dpkg,packagekitd

Fix

Wait for the other command or close the updater, then run yours. Do not remove /var/lib/dpkg/lock-frontend or /var/lib/dpkg/lock; apt itself warns that this may break the system. If a dpkg run was killed halfway, apt later asks for dpkg --configure -a, which is a separate repair.

Fix
# wait for the other process to end, then:
sudo apt-get update
sudo apt-get install -y nginx

Verify

The command runs to the end with no lock error.

Verify
sudo apt-get check

Confirmed by salsa.debian.org

Did this fix it?

How do I stop it coming back?

  • Put -o DPkg::Lock::Timeout=600 on every apt-get call in provisioning scripts and CI runners, so a scheduled update makes them wait instead of fail.
  • Start first-boot automation with cloud-init status --wait on cloud images.
  • Run package steps one at a time per host in your automation tool, never as two background jobs on the same machine.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.