maple leaves, autumn, leaves, foliage, autumn leaves, autumn foliage, autumn colors, autumn season, fall foliage, fall leaves, fall colors, nature, maple, red leaves, red foliage, seoul, korea, dobong, seasonal, autumn, autumn, autumn, autumn, autumn, leaves, autumn leaves, korea

Fix ‘Could Not Get Lock /var/lib/dpkg/lock-frontend’ on Ubuntu and Kali

·

If apt stops with E: Could not get lock /var/lib/dpkg/lock-frontend, something else is already using the package system. Usually that’s the automatic update service running in the background. The fix is to wait for it, or find the process and stop it cleanly. Deleting the lock file isn’t the first step. This guide covers Ubuntu, Debian and Kali, which all use the same apt and dpkg locks.

What the error means

Only one program can change installed packages at a time. To enforce that, apt and dpkg take a lock on a few files while they work. If a second command tries to start, it can’t get the lock and gives up with something like this:

E: Could not get lock /var/lib/dpkg/lock-frontend. It is held by process 2481 (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?

These are the lock files involved:

  • /var/lib/dpkg/lock-frontend: the lock front ends like apt take before running dpkg
  • /var/lib/dpkg/lock: dpkg’s own database lock
  • /var/cache/apt/archives/lock: guards the downloaded package cache
  • /var/lib/apt/lists/lock: guards the package lists that apt update refreshes

The usual holders are the apt-daily and apt-daily-upgrade services and timers, unattended-upgrades, the desktop Software Updater, or simply another apt command in a different terminal or SSH session.

Step 1: wait a few minutes

This error shows up most on a freshly booted machine or a new VM, because the daily update timers fire soon after boot. Give it two to five minutes and run your command again. Often that’s all it needs, and it’s the safest fix there is.

Step 2: find out what’s holding the lock

Newer apt versions print the process ID and name in the error, as in the example above. If yours doesn’t, or you want more detail, check for yourself:

ps aux | grep -i apt
sudo lsof /var/lib/dpkg/lock-frontend
sudo fuser -v /var/lib/dpkg/lock-frontend /var/lib/dpkg/lock

lsof and fuser both show which process has the file open, the same way they work for network ports. I cover them in more depth in finding the process using a port with lsof, ss and fuser. If none of these commands returns a process, the lock is stale. Skip to Step 4.

Step 3: let it finish or stop it cleanly

If the holder is unattended-upgrades or the apt-daily jobs, the best option is to let them finish. If you really can’t wait, stop them through systemd rather than killing them:

sudo systemctl stop apt-daily.timer apt-daily-upgrade.timer
sudo systemctl stop apt-daily.service apt-daily-upgrade.service
sudo systemctl stop unattended-upgrades

Stopping the timers only lasts until the next reboot. If it’s a desktop Software Updater window or another terminal running apt, close it from there instead.

Don’t kill -9 a dpkg process in the middle of an install. dpkg may be partway through unpacking files or running a package’s scripts. Interrupting it can leave packages half-configured and the dpkg database inconsistent, which is a far bigger problem than waiting a few minutes.

Step 4: remove stale lock files (only if nothing holds them)

A crash, power cut or killed session can leave the lock files behind with no process attached. Only once lsof and fuser confirm that nothing holds them is it safe to remove them:

sudo rm /var/lib/dpkg/lock-frontend
sudo rm /var/lib/dpkg/lock
sudo rm /var/cache/apt/archives/lock
sudo rm /var/lib/apt/lists/lock

Never do this while a real apt or dpkg process is running. Two package managers writing at once is exactly what the locks are there to prevent.

Step 5: repair any interrupted install

If an earlier install or upgrade was cut off, finish configuring the packages and fix any broken dependencies before carrying on:

sudo dpkg --configure -a
sudo apt install -f

dpkg --configure -a configures every package that was unpacked but not set up. apt install -f (fix broken) then pulls in or removes whatever is needed to make the dependencies consistent again. After that, sudo apt update and your original command should work.

Prevent it: make apt wait instead of failing

In scripts, provisioning jobs and CI, the cleanest fix is to tell apt to wait for the lock rather than fail straight away. Use the DPkg::Lock::Timeout option with a number of seconds:

sudo apt-get -o DPkg::Lock::Timeout=60 install -y curl

With that set, apt waits up to 60 seconds for whatever holds the lock to finish, then carries on. A bounded timeout like this is better than an endless wait, because a stuck job fails with a clear error rather than hanging forever. Very old apt releases ignore the option, so check it on your system first.

On Kali in particular, it’s worth deciding how you want background updates to behave. If you’d rather they never compete with your own apt runs, see automatic updates in Kali for how to set them up or turn them off.

Quick checklist

  1. Wait two to five minutes, especially just after boot
  2. Find the holder with ps aux | grep -i apt, sudo lsof or sudo fuser -v
  3. Let it finish, or stop the apt-daily and unattended-upgrades units with systemctl stop
  4. Never kill dpkg mid-install
  5. Remove the lock files only when nothing holds them
  6. Run sudo dpkg --configure -a and sudo apt install -f
  7. Add -o DPkg::Lock::Timeout=60 in scripts

The same find-the-process-then-stop-it approach works for plenty of other “something’s already using it” errors on Linux. For ports, see what’s using a port on Linux and how to stop it.


Leave a Reply