109

I just wondered if somebody could tell me what this means:

E: dpkg was interrupted, you must manually run 'sudo dpkg --configure -a' to correct the problem.
reidsr@ubuntu:~$ sudo dpkg --configure -a
dpkg: error: dpkg status database is locked by another process
reidsr@ubuntu:~$ 

How do I run sudo dpkg --configure -a manually?

2
  • Is this machine important, that is to say, can you reboot it? Should resolve this real quick
    – Huckle
    Nov 20, 2012 at 0:19
  • What worked for me is: Software & Updates --> Updates --> Change "Automatically check for updates" to "Never", and then sudo reboot.
    – mhdadk
    Jun 7, 2022 at 14:30

10 Answers 10

142

First run:

lsof /var/lib/dpkg/lock

Then make sure that process isn't running:

ps cax | grep PID

If it is running:

kill PID
#wait
kill -9 PID

Make sure process is done:

ps cax | grep PID

Then remove the lock file:

sudo rm /var/lib/dpkg/lock

Let dpkg fix itself:

sudo dpkg --configure -a

You should be fine afterwards :)

9
  • 13
    One should never remove lockfiles manually. They're there for a reason. Please, only use this as a very last resort. First try to identify and close other applications using the package management.
    – gertvdijk
    May 6, 2013 at 16:06
  • 11
    If there is a process actually using the lock file, it would probably be better to kill that process instead of just removing the lock file. You should be able to find the process with lsof /var/lib/dpkg/lock. May 27, 2014 at 15:34
  • A strong +1 for Abe's comment. Never remove a lockfile without killing all processes that have open file descriptors to it. Jan 20, 2015 at 6:48
  • 1
    @Xen2050 IMO a new answer adds unnecessary clutter. If you think a bash script that does same thing automatically is usable I'll write it as another answer. Jan 20, 2015 at 8:13
  • 2
    As it stands now, this is definitely the best answer. Yes, it is very important to make sure no running process owns the lock. I'd already checked that, nothing was running, and I needed to know how to delete the old lock. This answer covers all the bases.
    – Lambart
    Apr 4, 2015 at 19:00
30

In our case there was no running process (PID) so I hard-stopped and rebooted the VPS.

Next, there are several lock files that might need to be removed:

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

Then you can fix dpkg as suggested:

sudo apt update
sudo dpkg --configure -a
5
  • What are the lock files for?
    – jarno
    Oct 27, 2019 at 21:56
  • 1
    @jarno Lock files are just metadata that prevent processes from running over each other or overwriting each other, etc. They're supposed to be cleaned up after the process in question finishes, but if a process hangs, the lock files might remain indefinitely. Oct 30, 2019 at 20:56
  • Re: dpkg package errors: stackoverflow.com/a/44822199/1718491 Feb 2, 2021 at 12:13
  • I could only unlock the situation with this solution, also found in askubuntu.com/questions/15433/…
    – Telmo Dias
    Apr 13, 2021 at 7:34
  • 1
    The clearing of the additional lock-frontend in this answer fixed this issue for me.
    – Jacques
    Dec 7, 2021 at 8:28
10

One could also try:

sudo apt-get install -f

To fix any broken packages:

Fix; attempt to correct a system with broken dependencies in place. This option, when used with install/remove, can omit any packages to permit APT to deduce a likely solution. If packages are specified, these have to completely correct the problem. The option is sometimes necessary when running APT for the first time; APT itself does not allow broken package dependencies to exist on a system. It is possible that a system's dependency structure can be so corrupt as to require manual intervention (which usually means using dselect(1) or dpkg --remove to eliminate some of the offending packages). Use of this option together with -m may produce an error in some situations. Configuration Item: APT::Get::Fix-Broken.

0
9

You can not run several packages applications/commands/tools at the same time. Sometimes, it means that synaptic, apt-get or the package update tool are running in the backgroung. Just close other package tools, or wait for them to be finished. And dpkg will run.

4

There are some good answers here, but just wanted to add a non-lsof method of finding the dpkg lock user

Step 1: Find out who is locking dpkg:

Option #1: Using lsof (not always installed on the machine)

lsof /var/lib/dpkg/lock

Option #2: Using bash

for pid in $(ls /proc | egrep [0-9]+); do sudo ls -l /proc/$pid/fd 2>/dev/null | grep /var/lib/dpkg/lock && echo $pid; done

Step 2: Decide what you want to do with the current dpkg user

If there is no such process, there is nothing to decide, just skip to the next step.

Otherwise, you have to decide if you want to kill the process or let it finish gracefully. If you decide to kill it, just use kill <pid>. If the process still won't die, you can consider killing it using kill -9 <pid>, but it might create certain inconsistencies and I advise against it unless you know what you are doing.

Step 3: Remove the lock file

sudo rm /var/lib/dpkg/lock

Step 4: Fix dpkg internal state

sudo dpkg --configure -a

4

One approach is just to restart the PC. This worked for me.

3

Following should help:

sudo rm /var/lib/dpkg/lock

sudo dpkg --configure -a
0
1

No answer of this thread has solved it for me. I had removed the lock files, and there was no PID running, yet, whenever I executed sudo dpkg --configure -a, the shell was stuck at one of the previously locked "apt" tasks that I had force-closed by closing the terminal in the end. I also killed the apt processes using sudo killall apt apt-get. The stuck apt task in my case was "Setting up docker-ce (5:20.10.1~3-0~ubuntu-bionic)".

Solution in my case:

  • Boot in recovery mode.
  • In the recovery menu, choose "root" in order to "Drop to root shell prompt".
  • Press Enter.
  • Type 'sudo dpkg --configure -a'.
  • Go on with what you were about to do, it should work now, and / or just reboot.

Please see screenshots and detailed explanation (e.g. how to get the recovery mode at start) at the answer of docker ps stuck … docker install also just hangs.

0

This can happen if the upgrade process was interrupted (like you're connection was lost). You can try using screen as root (sudo su) to get back into the session.

screen -r
0

One thing that's not mentioned - you could have unattended/automatic upgrades enabled. If so, unattended-upgr is likely holding the lock on dpkg. In that case, forcibly removing the lock file is NOT a good idea!

You can check if it's running using the following:

ps -A | grep unattended-upgr

You can also see if that's the process holding the lock using Step 1 of Daniel Trugman's answer. Option #2 (using bash) was the only one that showed me the PID that had the file locked, and pointed me to unattended-upgr.

AFAIK, your only two options are to patiently wait for the updates to finish, or temporarily disable unattended/automatic updates, install the needed package, and turn it back on.

1
  • Welcome to AskUbuntu! Wouldn't following Daniel Trugman's answer reveal that unattended-upgrades was holding the lock on dpkg if that was the case?
    – Elder Geek
    Apr 23, 2020 at 20:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .