38

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?

Braiam
  • 67,791
  • 32
  • 179
  • 269
Sambit
  • 1,224
  • 1
  • 10
  • 14

3 Answers3

82

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.

Braiam
  • 67,791
  • 32
  • 179
  • 269
  • 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 05: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 07 '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? – sugab Aug 08 '18 at 00:29
  • @bagustris download the package from the Ubuntu repositories. – Braiam Aug 08 '18 at 00:43
  • @Braiam, still using wget to download from ubuntu repo give me the same error when trying to install using dpkg -i: dpkg: error: failed to open package info file/usr/local/var/lib/dpkg/available' for reading: No such file or directory` – sugab Aug 08 '18 at 02:29
  • I solved this issue by creating directory and file: sudo touch /usr/local/var/log/alternatives.log. Thanks – sugab Aug 08 '18 at 02:48
2

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*
./configure
make
sudo make install

hope it will work.

Sukupa91
  • 3,037
  • 2
  • 20
  • 33
  • 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
  • Now I am getting error like 'E: Could not open file /var/lib/dpkg/status - open (2: No such file or directory) E: The package lists or status file could not be parsed or opened.' – Sambit Nov 28 '13 at 13:34
  • 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
  • I get an error: make: No targetsspecified andnomakefilefound. Stop. – IgorGanapolsky Dec 19 '16 at 00:36
1

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"

Elder Geek
  • 36,023
  • 25
  • 98
  • 183