Unfortunately I've deleted dpkg directory while removing the lock. By mistake I typed

root@sam:~$ rm -r /var/lib/dpkg

Now when I am trying to install/uninstall packages it shows me following error.

E: Could not open lock file /var/lib/dpkg/lock - open (2: No such file or directory)

What should I do now?

  • Have you tried copying (Try to not delete them) the files from a Ubuntu Live CD into the system itself as Superuser?? – Dominic Hayes Feb 7 '16 at 15:47

Using root and recursive rm (rm -r) is a recipe for disasters, don't do it. The files and directories that were deleted are as follows:

ls -l /var/lib/dpkg/
total 9964
drwxr-xr-x 2 root root    4096 nov 28 11:18 alternatives
-rw-r--r-- 1 root root      11 sep 18 14:08 arch
-rw-r--r-- 1 root root 2573807 nov 28 11:18 available
-rw-r--r-- 1 root root 2561322 nov 28 10:25 available-old
-rw-r--r-- 1 root root       8 abr 24  2013 cmethopt
-rw-r--r-- 1 root root     538 sep 25 17:24 diversions
-rw-r--r-- 1 root root     457 sep 25 17:24 diversions-old
drwxr-xr-x 2 root root  483328 nov 28 11:17 info
-rw-r----- 1 root root       0 nov 28 11:18 lock
drwxr-xr-x 2 root root    4096 mar 22  2013 parts
-rw-r--r-- 1 root root     135 abr 24  2013 statoverride
-rw-r--r-- 1 root root 2269113 nov 28 11:18 status
-rw-r--r-- 1 root root 2268870 nov 28 11:18 status-old
drwxr-xr-x 2 root root    4096 nov 28 11:18 triggers
drwxr-xr-x 2 root root    4096 nov 28 11:18 updates

You removed 5 directories, the status file, etc. So, lets try to fix the stuff. First, create the directory:

sudo mkdir -p /var/lib/dpkg/{alternatives,info,parts,triggers,updates}

Recover some backups:

sudo cp /var/backups/dpkg.status.0 /var/lib/dpkg/status

Check if dpkg is working:

apt-get download dpkg
sudo dpkg -i dpkg*.deb

If everything is "ok" then repair your base files too:

apt-get download base-files
sudo dpkg -i base-files*.deb

Now try to update your package list, etc.:

dpkg --audit
sudo apt-get update
sudo apt-get check

Compare ls -l /var/lib/dpkg with the above list. If some -old file is not present don't worry it as it would be created with the normal usage of the system.

  • 1
    Thanks for response. I followed the steps as you mentioned here, but while executing sudo dpkg -i dpkg*.deb, I am getting an error as dpkg: error: failed to open package info file `/usr/local/var/lib/dpkg/status' for reading: No such file or directory – Sambit Nov 29 '13 at 5:11
  • @Sambit Impossible. Can you edit your question and add the complete output? – Braiam Nov 29 '13 at 14:01
  • @Sambit ls: cannot access /usr/local/var/lib/dpkg/status: No such file or directory is the only thing I get. /usr/local is not place to install dpkg files. – Braiam Jan 7 '14 at 13:59
  • Same problem here, Ubuntu trying to install in /usr/local/ instead of in /var/lib/dpkg. How to make it point to /var/lib/dpkg? – bagustris Aug 8 '18 at 0:29
  • @bagustris download the package from the Ubuntu repositories. – Braiam Aug 8 '18 at 0:43

Well dpkg (Debian package management system ) package provides the low-level infrastructure for handling the installation and removal of Debian software packages.So removing it was not a good idea, and you can can it back by-

Downloading the file by using wget :

wget https://launchpad.net/ubuntu/+archive/primary/+files/dpkg_1.16.1.2ubuntu7.2.tar.bz2

Extract it in xxx folder.

tar -xvf dpkg_1.16*
cd dpkg-1.16*
sudo make install

hope it will work.

  • I've go through those steps and successfully installed. but again I am getting the same problem. – Sambit Nov 28 '13 at 12:56
  • does ./configure failed or was successful?? – Sukupa91 Nov 28 '13 at 12:58
  • Already restarted and I get the same error as mentioned in my question. Also inbuilt software like "Ubuntu Software Centre", "Update Manager" are also not working. – Sambit Nov 28 '13 at 13:07
  • hey i have updated wget first commands, try the same procedure start with wget command – Sukupa91 Nov 28 '13 at 13:14
  • 1
    getting error, cp: cannot stat `/var/lib/dpkg/status-old': No such file or directory. I am facing these problem because "dpkg" directory doesn't exist in my system. – Sambit Nov 28 '13 at 13:48

Recover your system from backup. You did make backups right? Backing up your system and data is critical to deal with unexpected situations such as this one (user error) and hardware failure. If you failed to create backups such as discussed here you can attempt data recovery.

In future I suggest that you backup on a regular basis to avoid catastrophic data loss. And I doubt you'll forget that as @Braiam said "root + rm + -r = disaster"

Not the answer you're looking for? Browse other questions tagged or ask your own question.