“Could not lock /var/lib/dpkg/lock’ error on Ubuntu

ADVERTISEMENT

In Ubuntu, you can install programs and packages in three ways – terminal commands (sudo apt-get install), Synaptic package manager (which does not come installed by default anymore) and Ubuntu Software Center (which is the preferred way of installing apps on Ubuntu now).

One common issue I have faced while using Terminal and/or Synaptic and Software Center is the administrator lock error.

E: Could not get lock /var/lib/dpkg/lock – open (11 Resource temporarily unavailable)
E: Unable to lock the administration directory (/var/lib/dpkg/)

This error happens if more than one app tries to hold the lock. The quick fix to this is to close all programs that may use admin lock (package managers, terminal, update manager and Software Center), then try again.

ADVERTISEMENT

Sometimes, this may not work. You can try this command in terminal to find what is running

ps -e | grep -e apt -e adept | grep -v grep

You can force the lock to be removed, then attempt the operation again. Issue the command below to remove the lock by force (note that this may cause issues for whichever program was holding the lock).

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

Another cause for this error is running apt-get/aptitude without sudo command. This means that the command is not granted root permission, which in turn does not allow the lock because it does not have the necessary privilege. Running the above command to clear locks is pointless in such cases. What you need to do, instead, is run the command with sudo in front, so as to grant it the right permissions.

In searching for fixes online while I found some commands that remove the lock, but do not prevent system stability issues or interrupt installation or processes.

sudo fuser -cuk /var/lib/dpkg/lock; sudo rm -f /var/lib/dpkg/lock
sudo fuser -cuk /var/cache/apt/archives/lock; sudo rm -f /var/cache/apt/archives/lock

Do NOT issue the above commands, it turned out to be a bad idea for me because it caused the system to kill all processes, including Firefox and other apps that would not cause any admin locks. Use it at your own risk, and I personally would not recommend it.

Another cause for the admin lock error is the Ubuntu update manager checking for updates at system startup (if you have set it to be so). That goes on in the background, and if you start using Synaptic immediately after startup, this error may pop up. Try it a few minutes after startup.