Short answer

“Could not get lock /var/lib/dpkg/lock-frontend” means another apt or dpkg run is working, often automatic updates on a new server. The message names its process. Wait for it: sudo apt-get -o DPkg::Lock::Timeout=300 install <package> waits up to five minutes and then carries on. Do not delete the lock file.

Tested on Ubuntu 26.04 and 24.04, two separate runs. On 24.04? One section differs, each step marked “On Ubuntu 24.04” right under it: The fix: make apt wait for the lock. Everything that differed

The error

On the test server another apt-get install was running in the background, as automatic updates do after a new server’s first boot. A second install at that moment:

Server
$ sudo apt-get install -y ncduE: Could not get lock /var/lib/dpkg/lock-frontend. It is held by process 2244 (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?
Output hidden (3 lines)

apt-get gives up at once (exit code 100) and installs nothing. The first line names the process that has the lock; the N: line is apt’s own warning against deleting the file.

Find what holds the lock

Server
$ sudo lsof /var/lib/dpkg/lock-frontendCOMMAND  PID USER FD   TYPE DEVICE SIZE/OFF   NODE NAMEapt-get 2244 root 4uW  REG  253,1        0 100952 /var/lib/dpkg/lock-frontendapt-get 2498 root 4u   REG  253,1        0 100952 /var/lib/dpkg/lock-frontend
Output hidden (3 lines)

Two apt-get processes have the file open, but only one holds the lock: the W in 4uW marks it, and its PID is the one in the error. The other is a helper of the same run. ps shows what that run is doing and for how long:

Server
$ ps -C apt-get,apt,dpkg,unattended-upgr -o pid,etime,args    PID     ELAPSED COMMAND   2244       00:03 apt-get install -y tree   2498       00:02 apt-get install -y tree
Output hidden (3 lines)

An install that started seconds ago will finish soon. If you see unattended-upgr, that is the automatic updates service: on a new server it can take several minutes. Only if the same process sits there for a long time with nothing happening is it stuck; then stop it through its service, not by deleting files.

The fix: make apt wait for the lock

DPkg::Lock::Timeout tells apt to wait up to that many seconds for the lock instead of failing. This is also the setting to put in scripts and provisioning tools that run right after a server boots:

Server
$ sudo apt-get -o DPkg::Lock::Timeout=300 install -y ncdu[… 17 lines not shown …]Scanning processes...Scanning linux images... Running kernel seems to be up-to-date. No services need to be restarted. No containers need to be restarted. No user sessions are running outdated binaries. No VM guests are running outdated hypervisor (qemu) binaries on this host.
Output hidden (13 lines)

It waited about 75 seconds, until the other run was done, and then installed the package. On Ubuntu 26.04 apt shows a single “Waiting for cache lock” line while it waits and removes it when it gets the lock, so only the install is left on screen.

On Ubuntu 24.04

Ubuntu 24.04’s apt prints a new “Waiting for cache lock” line every second while it waits (75 lines in the test), then the install:

Server
$ sudo apt-get -o DPkg::Lock::Timeout=300 install -y ncduWaiting for cache lock: Could not get lock /var/lib/dpkg/lock-frontend. It is held by process 1966 (apt-get)Waiting for cache lock: Could not get lock /var/lib/dpkg/lock-frontend. It is held by process 1966 (apt-get)Waiting for cache lock: Could not get lock /var/lib/dpkg/lock-frontend. It is held by process 1966 (apt-get)[… 102 lines not shown …]
Output hidden (4 lines)

Why deleting the lock file is wrong

Many guides end with rm /var/lib/dpkg/lock*. The lock is not the file: apt asks the kernel for a lock on the open file, and the kernel drops it the moment that process exits, even if it crashed. After the other run ended, nobody holds it and the file is simply still there:

Server
$ sudo lsof /var/lib/dpkg/lock-frontend; ls -l /var/lib/dpkg/lock-frontend-rw-r----- 1 root root 0 Nov  8 16:25 /var/lib/dpkg/lock-frontend
Output hidden (1 line)

So there is never a “stale” lock to delete. If you delete the file while an install runs, the next apt creates a new file and locks that one, and two installs can then change the package database at the same time, which leaves half-installed packages. Check that nothing was left half-done:

Server
$ sudo dpkg --audit
Server
$ dpkg -s tree ncdu | grep -E "^(Package|Status):"Package: treeStatus: install ok installedPackage: ncduStatus: install ok installed
Output hidden (4 lines)

dpkg --audit printed nothing, and both installs, the background one (tree) and ours (ncdu), are complete.

Troubleshooting

  • The error names a process, and it is apt-get, apt or unattended-upgr: another install or update runs. Wait for it. Details.
  • Several processes in lsof: the one with W holds the lock. Details.
  • A script fails right after a server boots: add -o DPkg::Lock::Timeout=300 to its apt-get commands. Details.
  • Someone already deleted the lock files: when no apt is running, run sudo dpkg --audit; empty output means nothing is half-done. Details.

What these steps changed on the server

Both runs recorded the server before the first step and after the last (see the Tested on box below): the two packages were installed, one after the other, and nothing else changed.

Ubuntu 26.04 vs 24.04

The same check ran on a fresh 26.04 server (apt 3.2) and on a fresh 24.04 server (apt 2.8), each with its own run log. The error and the fix were the same; how apt shows the wait differed:

WhatUbuntu 26.04Ubuntu 24.04
SystemUbuntu 26.04.1 LTS7.0.0-43-genericUbuntu 24.04.5 LTS6.8.0-147-generic
apt3.2.1ubuntu0.12.8.3
dpkg1.23.7ubuntu11.22.6ubuntu6.6
lsof4.99.4+dfsg-2build24.95.0-1build3
procps2:4.0.4-9ubuntu12:4.0.4-4ubuntu3.3
ncdu1.22-1build11.19-0.1
tree2.3.1-12.1.1-2ubuntu3.24.04.2
apt-get while it waits for the lockone “Waiting for cache lock” progress line, erased when the lock is freea new “Waiting for cache lock: Could not get lock …” line every second
ProofRun log, 15 checks passedRun log, 15 checks passed