0

I have a fresh installation of Ubuntu 18.04-x64.

In the terminal I enter the command:

sudo apt update

And I get the following error:

E: Could not get lock /var/lib/apt/lists/lock - open (11: Resource temporarily unavailable)
E: Unable to lock directory /var/lib/apt/lists/

When I run sudo apt update again, the command runs successfully and I get:

Reading package lists... Done                                                  
Building dependency tree       
Reading state information... Done
103 packages can be upgraded. Run 'apt list --upgradable' to see them.

This behaviour is consistent. I have a VM snapshot of the fresh Ubuntu installation and everytime the first sudo apt update fails with this error and the second sudo apt update completes successfully.

EDIT: Should have mentioned that the sudo apt update command lies inside a script. Running the script with sudo privileges seems to have fixed the problem.

  • 1
    As you have learned, it is unwise to use 'sudo' within a script. Glad to see you seem to have solved the problem. – user535733 Mar 28 '19 at 13:27

1 Answers1

0

That issue is happening because the system identifies the fact the lock file is being used by another process. You can identify the process and kill it by

$ps aux | grep -i apt
$sudo kill -9 <pid>

If something happened during an apt-get process, like a system shutdown, then there will be no process having the file locked, by the problem will still be there. In that case you can reconfigure the necessary files, as follows:

$sudo rm /var/lib/apt/lists/lock
$sudo rm /var/cache/apt/archives/lock
$sudo rm /var/lib/dpkg/lock
$sudo dpkg --configure -a
Sarriman
  • 316
  • So if I'm running the sudo apt update command inside a script, should I add those two lines in front of it to ensure it runs correctly? – CrispJam Mar 28 '19 at 13:10
  • You haven't mentioned that this is happening inside a script. It might be that the script does not have insufficient permissions. So try running it with sudo. What I wrote you above, is in case the problem occurs into the terminal operations. – Sarriman Mar 28 '19 at 13:13
  • I'm pretty new to shell scripting so I assumed that what is written inside a script is executed just as if I typed it out to the terminal, one line at a time – CrispJam Mar 28 '19 at 13:16
  • That is the case. But if you simply run the script by ./script.sh, then the script might not have the sufficient permissions to complete the operations you automated. – Sarriman Mar 28 '19 at 13:17
  • @CrispJam If you have dealt with the issue, accept the answer or the comments of it, to help others find the solution to a similar problem. – Sarriman Mar 28 '19 at 13:22