sodcalculator

10 Tech How-To

Unable to Lock Error Ubuntu: Fixes That Work

Guide 10-43552 · Updated · Step by step

If you see an “Unable to lock” error on Ubuntu, it usually means another process is using the package manager, or a stale lock file was left behind after a crash. The fix is to identify the locking process and either wait for it, kill it, or remove the stale lock file, depending on the situation.

What the Error Means

Ubuntu uses lock files to prevent two package management tools from running at the same time. When you run apt, dpkg, or the Software Center, they create a lock file in /var/lib/dpkg/lock, /var/lib/apt/lists/lock, or /var/cache/apt/archives/lock. If another process holds one of these locks, your command fails with a message like:

E: Could not get lock /var/lib/dpkg/lock-frontend - open (11: Resource temporarily unavailable)
E: Unable to acquire the dpkg frontend lock (/var/lib/dpkg/lock-frontend), is another process using it?

This is a safety feature, not a bug. It prevents corruption that would happen if two installers wrote to the same files at once.

Check for a Running Package Manager

Before deleting anything, check if an update or install is already running. Open a terminal and run:

ps aux | grep -E 'apt|dpkg|unattended-upgrades'

If you see a process like apt-get, dpkg, or unattended-upgrades, let it finish. The unattended-upgrades service runs automatically in the background and can hold the lock for several minutes after you boot. You can also check with sudo lsof /var/lib/dpkg/lock-frontend to see exactly which PID holds the lock.

Wait or Kill the Process

If a legitimate update is in progress, wait a few minutes and retry. If it seems stuck, you can kill it:

  1. Find the PID with sudo lsof /var/lib/dpkg/lock-frontend.
  2. Terminate it gently with sudo kill PID.
  3. If it doesn’t respond, use sudo kill -9 PID as a last resort.

After killing, you may need to run sudo dpkg --configure -a to fix any interrupted installations.

Remove Stale Lock Files

If no process is running, the lock file is probably stale—left over from a crash or a force-quit. You can safely delete it:

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

Then reconfigure the package manager:

sudo dpkg --configure -a
sudo apt update

Deleting lock files is generally safe when no package manager is running, but it should be your last resort after checking for active processes.

Common Variants of the Error

You might see different messages depending on which tool you’re using:

  • “Could not get lock /var/lib/dpkg/lock” – usually means dpkg itself is busy.
  • “Unable to acquire the dpkg frontend lock” – the frontend lock, used by apt and Software Center.
  • “Could not get lock /var/lib/apt/lists/lock” – apt’s list update lock.
  • “Waiting for cache lock” – appears when you run a GUI tool like the Software Center while apt is running.

All have the same root cause and the same fixes.

Preventing Future Lock Errors

Most lock errors happen because you ran two package commands at once, or because the automatic update service was still running. To avoid them:

  • Don’t run sudo apt install while the Software Center is open.
  • Wait a few minutes after boot before running apt commands.
  • If you use a script, add a check for the lock file before proceeding.
  • Consider disabling unattended-upgrades if it frequently interferes, but be aware that this reduces automatic security updates.

If you regularly see lock errors, your system may have a deeper issue like a hung dpkg process that needs a reboot.

Frequently Asked Questions

Is it safe to delete the lock files?

Yes, if no package manager is running. Lock files are only meaningful while a process holds them. If you delete them while apt is active, you risk corrupting the package database. Always check with ps aux | grep apt first.

Why does this happen after every reboot?

Ubuntu’s unattended-upgrades service runs shortly after boot. If you run apt within the first few minutes, you’ll hit the lock. Wait a bit or check the process list first.

What if I see “Could not open lock file /var/lib/dpkg/lock-frontend – open (13: Permission denied)”?

That’s a different error—it means you’re not root. Use sudo before the command, or run sudo -i to get a root shell.

Can I use “killall apt” to fix it?

You can, but it’s blunt. It kills all apt processes, which could interrupt a legitimate install. Use it only if you’re sure nothing important is running.

Does this error affect Ubuntu updates from the GUI?

Yes, the Software Center and the update manager use the same locks. If you see “Waiting for cache lock” there, close other package tools and wait for the background process to finish.

What if the error persists after removing the locks?

Run sudo dpkg --configure -a and then sudo apt --fix-broken install. If it still fails, check for a process that keeps recreating the lock, or reboot into recovery mode and run dpkg --configure -a from there.

Related

General steps for current mainstream versions; menus can differ by device and version. See the disclaimer or report an error.

More in this bed

The whole Tech How-To bed →