I'm trying to make a script to setup a new VM to be ready for use, which involves some installs, but when I put the install commands (apt-get --yes install <package>) in the a script they always fail with:

E: Could not get lock /var/lib/dpkg/lock - open (11: Resource temporarily unavailable)

The script is run as root (sudo ./setup.sh)

When I check for apt process I get:

4 S root      2227     1  0  80   0 -  1126 -      07:33 ?        00:00:00 /bin/sh /usr/lib/apt/apt.systemd.daily update

0 S root      2231  2227  0  80   0 -  1126 -      07:33 ?        00:00:00 /bin/sh /usr/lib/apt/apt.systemd.daily lock_is_held update

The thing is, this only happens when apts are executed from a script, if I execute exactly the same commands manually they never have this problem. Does the shell script run apt installs differently somehow?

In any case, what is the best way to run apt (or yum for that matter) installer from a shell script in a way that woudn't cause it to lock up?

  • Which user is running the script? Since you use sudo you must have a) a way to pass the password or b) a sudoers entry for the user to allow this command without password.
    – anon
    Aug 17, 2018 at 14:49
  • 1
    ^ Or you're running the script as root - in which case the sudo is irrelevant and meaningless Aug 17, 2018 at 14:50
  • @roaima I do run the script as root, so you're right I don't need sodo's in the script itself
    – Maxim
    Aug 17, 2018 at 15:09

2 Answers 2


You have to write sudo apt-get install <package> -y in the script. Execute the script with ./setup.sh

But of cause your user has to be a sudoer (sudo access).

  • The OP says that their script (in its entirely) is run as root using sudo. Dec 15, 2019 at 17:34

It tells you (apt cannot get lock) that you need to run that bash script further in to post-install or wait for those process names not to be present. You could be mean about it and kill the processes and delete the lockfile then apt-get update or just wait for them to finish.

Things that could make this fail - if you run apt-get install something as part of installing another package (don't do that, just use dependencies).

  • More likely apt is running from cron Dec 16, 2019 at 2:26

You must log in to answer this question.

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