Ubuntu – Cannot open lock file /var/lib/dpkg/lock permission denied–even using sudo

dpkg

I recently installed a fresh version of Ubuntu LTS 12.04. I had been using LTS 10.4 and saved my package.list and was planning on using

$> sudo dpkg --set-selections < package.list && apt-get dselect-upgrade

to install the same packages in the new version

However, the computer fails to run with the error

E: Could not open lock file /var/lib/dpkg/lock - open (13: Permission denied)  
E: Unable to lock the administration directory (/var/lib/dpkg/), are you root?

The simple answers to this would be am I root or a member of sudo?

  $> whoami

  surfer

   $> groups

surfer adm cdrom sudo dip plugdev lpadmin sambashare

So much for the easy answer.

Per the similar post
Unable to lock the administration directory (/var/lib/dpkg/) is another process using it?
The recommended solution is

$> sudo fuser -cuk /var/lib/dpkg/lock; sudo rm -f /var/lib/dpkg/lock

Upon executing that command, my screen goes blank, the keyboard becomes unresponsive, and my only option is to press the power button and reboot… (I would have thought this was a malicious suggestion, but many others seemed to have liked it.)

I've tried listing any programs that may be using /var/lib/dpkg/lock

$> sudo lsof 

lsof: WARNING: can't stat() fuse.gvfs-fuse-daemon file system /home/surfer/.gvfs
      Output information may be incomplete.

Note, there are no programs listed.

Similarly, no luck with

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

Nothing listed.

BTW, I can run apt-get from the command line.

$> sudo apt-get update

runs as expected and updates my package lists…

@izx:
After attempting your suggestion, I could not run

$> sudo dpkg --set-selections < package.list && apt-get dselect-upgrade

It failed with the same error. I tried this

a) immediately following the rm command in the F1 terminal
b) executing the rm command and returning to X with Ctrl+Alt+F7
c) executing the rm command and rebooting

Of interest only option c) yielded a different (but similar error message) which was

dpkg: error: dpkg status database is locked by another process

Although I cannot kill the processes using /var/lib/dpkg/lock with fuser, I did get the following output for processes that are linked to it.

$> fuser -cv /var/lib/dpkg/lock  
                     USER        PID ACCESS COMMAND  
/var/lib/dpkg/lock:  root     kernel mount (root)/var  
                     surfer     1641 ....m (surfer)gnome-settings-  
                     surfer     1656 ....m (surfer)compiz  
                     surfer     1679 ....m (surfer)nm-applet  
                     surfer     1688 ....m (surfer)nautilus  
                     surfer     1731 ....m (surfer)gtk-window-deco  
                     surfer     1733 ....m (surfer)unity-panel-ser  
                     surfer     1831 ....m (surfer)gnome-terminal  
                     surfer     1899 f.... (surfer)unity-applicati  
                     surfer     2029 F...m (surfer)update-notifier   

So I tried killing each process from the F1 terminal, and removed the lock file. No problem. The result was

$> fuser -cv /var/lib/dpkg/lock  
                     USER        PID ACCESS COMMAND  
/var/lib/dpkg/lock:  root     kernel mount (root)/var  

$> ls /var/lib/dpkg/lock  
ls: cannot access /var/lib/dpkg/lock: No such file or directory

file and links seem to be gone, but upon executing the dpkg command, I get the same error…and the lock file is back.

I'm really rather puzzled, and would welcome any help.

Best Answer

Your very First step should have been

sudo dpkg --set-selections < package.list && sudo apt-get dselect-upgrade

The following error

E: Could not open lock file /var/lib/dpkg/lock - open (13: Permission denied)  

would have never occurred , Even second time you used the same code, and got same error.

Try the right command by adding sudo as per above , and post your findings.

NOTE : The use of && enables you to execute multiple commands on same line , in the usual sense && only executes the second command if the first command returns a value reporting success . Therefore ,sudo here needs to be specified in second command .

Related Question