Getting the could not open lock file /var/lib/dpkg/lock error message can be confusing because it might very well ask if you’re root immediately afterward even if you were running an update command with the sudo prefix. This error command will show up on nearly everything that uses the apt-get package manager. Whether you’re updating a desktop or laptop running one of the various versions of Ubuntu or a Raspberry Pi with the Raspbian Debian spin, then you might find that it can be an issue on any of these platforms.
Nevertheless, the fix is rather simple. You can usually ignore anything asking you whether or not you’re root and go straight to the heart of the problem, which is related to two processes attempting to access a locked file at the same time.
Method 1: Making Sure sudo Can Access Every Part of the Command
If you were to run sudo apt-get update && apt-get upgrade, then you might get this error since the second process doesn’t have the right authority to access the /var/lib/dpkg/lock file.
Try instead running sudo apt-get update && sudo apt-get upgrade to see if that makes any difference. If it does, then your problem is already fixed and it was merely because you weren’t giving the second process sufficient permissions to access the file. Keep in mind that this command will update and then upgrade all of your packages, so it might prompt you to approve it and it might take a while to run.
Otherwise, though, the problem is already completely dealt with and you don’t need to do anything else.
Method 2: Removing the /var/lib/dpkg/lock File
Those who are using curl, wget or another command that downloads a file discretely might also have to preface that command with sudo should they be working from Raspbian since this takes the place of apt-get in some cases. Those working with pure Debian, Xubuntu, Lubuntu, Linux Mint and other similar desktop/laptop-oriented won’t have to worry about this, but it can be worth another try.
If this still didn’t work, then you might have to check that you don’t have some other program trying to access the file while you’re running your update. Try using sudo lsof /var/lib/dpkg/lock to see if any other processes are running that have it open. Should you have it opened up somewhere, then you can just close down the application.
Keep in mind that having read and write access for an application to open this file means by extension that said program must have root privileges, so you’ll need root privileges to close down the program. You can even try looking through the Task Manager application if you’re using a graphical desktop environment that supports this feature. Simply right-click on the offending process and close if you have the permissions to do so. Try the kill or killall commands from the command line too.
You can now try running your apt-get commands again and seeing if that makes any difference, but you won’t have to do anything else if it had. If you’re still having problems, then try removing the lock files outright. While this should be looked at as a last resort, it usually doesn’t cause any problems related to the apt-get program. You’ll still want to exhaust other methods before you do this since it can mess with the apt system.
Rebooting sometimes helps, but if you’re on a constantly on server or a Raspbian device then this may not be a viable option. Try running sudo rm /var/lib/dpkg/lock && sudo rm /var/lib/apt/lists/lock, which will free up the particular files that apt is taking a look at when you’re running it. Make sure to be careful about the names of those files, since running rm as root is destructive.
Assuming that you’ve done so after only exhausting the other options, then you can try running your update commands again and see what happens. You shouldn’t have any further issues because the files would no longer exist at that point, though the apt-get routines will be able to rebuild them while they run.
PRO TIP: If the issue is with your computer or a laptop/notebook you should try using the Reimage Plus Software which can scan the repositories and replace corrupt and missing files. This works in most cases, where the issue is originated due to a system corruption. You can download Reimage Plus by Clicking Here