Flash Sale:75% Off Hosting + Free DomainEnds in13h47m14sView Plans
Hostvento logoHostvento

Fix "Could not get lock /var/lib/dpkg/lock" on Ubuntu

Another program is using the package manager. Wait for it, stop it, or clear a stale lock.

VPS and Cloud Servers2 min read3 steps4 screenshots

What the error means

When you run apt you see a message such as:

E: Could not get lock /var/lib/dpkg/lock-frontend. It is held by process ...
The lock error message

Ubuntu allows only one package job at a time, so it uses a lock file. The error appears because:

  1. Another program (apt, aptitude, Synaptic or the Software Center) is running.
  2. An earlier job crashed or stopped and left the lock.
  3. A background service, such as automatic updates, is busy.

Fixes

Try them in this order, from the safest.

1. Close other programs. Close the Software Center, Synaptic and any terminal that runs apt.

2. Wait a few minutes. Automatic updates often run in the background. Try again later.

3. Reboot

sudo reboot
Rebooting the system

4. Clean the package cache

sudo apt clean
Cleaning the package cache

5. Let automatic updates finish

sudo unattended-upgrades -v
Running unattended-upgrades

6. Find the process that holds the lock

sudo lsof /var/lib/dpkg/lock-frontend

The output shows the process ID (PID) and the program name. You can read about the command with man lsof.

7. Restart a stuck service

sudo systemctl restart <service_name>

8. Stop the process

First ask it nicely (SIGTERM):

sudo kill <PID>

If it does not stop, force it (SIGKILL):

sudo kill -9 <PID>

9. Last resort: remove the lock files

Do this only when no package process is running (check with ps aux | grep -i apt):

sudo rm /var/lib/dpkg/lock
sudo rm /var/lib/dpkg/lock-frontend
sudo rm /var/cache/apt/archives/lock
sudo dpkg --configure -a
Warning: Deleting the lock while an install runs can damage your package database.

Prevent it

  • Run only one package command at a time.
  • Run automatic updates at quiet hours.
  • Update and upgrade the system on a regular basis.

Quick recap

  • The lock protects the package database.
  • Wait, reboot, or find and stop the process that holds it.
  • Remove the lock files only as a last step, and run dpkg --configure -a.