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
  • 69,112
Sambit
  • 1,264

3 Answers3

83

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
  • 69,112
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,067
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,752