Skip to content

Instantly share code, notes, and snippets.

@askb
Created October 31, 2017 22:40
Show Gist options
  • Save askb/8ebc682d0def8ff73649ce48c186aa94 to your computer and use it in GitHub Desktop.
Save askb/8ebc682d0def8ff73649ce48c186aa94 to your computer and use it in GitHub Desktop.
Workaroud for apt "Could not get lock /var/lib/dpkg/lock"
#
# E: Could not get lock /var/lib/dpkg/lock - open (11: Resource temporarily unavailable)
# E: Unable to lock the administration directory (/var/lib/dpkg), is another process using
# Wait for any background apt processes to finish
# There seems to be a backgroud apt process that locks /var/lib/dpkg/lock
# and causes our apt commands to fail.
while pgrep apt > /dev/null; do sleep 1; done
@askb
Copy link
Author

askb commented Jun 26, 2018

Another alternative is to use an bash alias:

echo "alias apt='while [ \$(fuser \"/var/lib/dpkg/lock\") ]; do echo \"Waiting on apt lock...\"; sleep 1; done; sudo apt \"\$@\"'" > ~/.bashrc

@askb
Copy link
Author

askb commented Jun 26, 2018

$ apt install expect
[sudo] password for askb: 
/var/lib/dpkg/lock: 
Waiting on apt lock...
/var/lib/dpkg/lock: 
Waiting on apt lock...
/var/lib/dpkg/lock: 
Waiting on apt lock...
/var/lib/dpkg/lock: 
Waiting on apt lock...
/var/lib/dpkg/lock: 
Waiting on apt lock...
/var/lib/dpkg/lock: 
Waiting on apt lock...
/var/lib/dpkg/lock: 
Waiting on apt lock...
/var/lib/dpkg/lock: 
Waiting on apt lock...
/var/lib/dpkg/lock: 
Waiting on apt lock...
/var/lib/dpkg/lock: 
Waiting on apt lock...
/var/lib/dpkg/lock: 
Waiting on apt lock...
/var/lib/dpkg/lock: 
Waiting on apt lock...
/var/lib/dpkg/lock: 
Waiting on apt lock...
/var/lib/dpkg/lock: 
Waiting on apt lock...
/var/lib/dpkg/lock: 
Waiting on apt lock...
/var/lib/dpkg/lock: 
Waiting on apt lock...
/var/lib/dpkg/lock: 
Waiting on apt lock...
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages were automatically installed and are no longer required:
  linux-headers-4.4.0-124 linux-headers-4.4.0-124-generic linux-image-4.4.0-124-generic linux-image-extra-4.4.0-124-generic                                                                      
Use 'sudo apt autoremove' to remove them.                                                                                                                                                        
The following additional packages will be installed:                                                                                                                                             
  tcl-expect                                                                                                                                                                                     
The following NEW packages will be installed:                                                                                                                                                    
  expect tcl-expect                                                                                                                                                                              
0 upgraded, 2 newly installed, 0 to remove and 20 not upgraded.                                                                                                                                  
Need to get 241 kB of archives.                                                                                                                                                                  
After this operation, 538 kB of additional disk space will be used.                                                                                                                              
Do you want to continue? [Y/n] n
Abort.

@dzirg44
Copy link

dzirg44 commented Sep 17, 2022

Hi man, thank you for this timesaver )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment