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
sudo reboot
.