DEV Community

Cover image for Fix “Could not get lock /var/lib/dpkg/lock” in Ubuntu (Safely)
FlatlinerAtheist | CLI-NERDS
FlatlinerAtheist | CLI-NERDS

Posted on Originally published at clinerds.com

Fix “Could not get lock /var/lib/dpkg/lock” in Ubuntu (Safely)

You run sudo apt install something, and instead of installing, Ubuntu slaps you with:

E: Could not get lock /var/lib/dpkg/lock-frontend
E: Unable to acquire the dpkg frontend lock, are you root?
Enter fullscreen mode Exit fullscreen mode

The error sounds scary, but it's usually Ubuntu being sensible: only one process may modify the package database at a time. Something else is already using apt — you just need to find out what.

First: wait a minute (seriously)

The most common culprit is unattended-upgrades, Ubuntu's automatic security updater. If you just booted the machine, there's a good chance it's patching in the background. Give it five minutes and try again — the error often fixes itself.

Graphical tools count too: Ubuntu Software, Synaptic, and the Software Updater all hold the same lock. Close them.

Check what's holding the lock

ps aux | grep -E "apt|dpkg" | grep -v grep
Enter fullscreen mode Exit fullscreen mode

If you see apt-get upgrade, unattended-upgr, or dpkg actively working, let it finish. You can also check who owns the lock file:

sudo fuser /var/lib/dpkg/lock-frontend
Enter fullscreen mode Exit fullscreen mode

If the process is stuck, kill it

Sometimes an interrupted apt run leaves a zombie process holding the lock forever. Kill by PID from the commands above:

sudo kill -9 <PID>
Enter fullscreen mode Exit fullscreen mode

Then repair the package state, because a killed transaction can leave half-configured packages:

sudo dpkg --configure -a
sudo apt --fix-broken install
Enter fullscreen mode Exit fullscreen mode

These two commands fix the vast majority of "I killed apt and now everything is weird" situations.

The last resort: deleting the lock file

You'll find this advice everywhere, and it works — but only do it after confirming no apt/dpkg process is running. Deleting the lock while a process is alive can corrupt the package database:

sudo rm /var/lib/dpkg/lock-frontend
sudo rm /var/lib/dpkg/lock
sudo rm /var/cache/apt/archives/lock
sudo dpkg --configure -a
Enter fullscreen mode Exit fullscreen mode

If you skipped the process check and went straight here, stop and do the ps aux step first next time.

"are you root?" — the other half of the error

If the message ends with "are you root?", the cause is even simpler: you forgot sudo. The lock error text appears in both situations, which confuses everyone. Run the command with sudo and it goes away.

If it keeps happening

  • Right after boot, every time — that's unattended-upgrades doing its job. Wait, or schedule your own updates for later
  • In scripts and cron jobs — add a retry loop, or use apt-get with DPkg::Lock::Timeout=60 so apt waits for the lock instead of failing instantly:
sudo apt-get -o DPkg::Lock::Timeout=60 install nginx
Enter fullscreen mode Exit fullscreen mode

That one flag eliminates most automation failures. Lock errors aren't a bug in your system — they're apt protecting your package database from itself.

Top comments (0)