How to recover deleted “dpkg” directory in Debian or Ubuntu

Question:

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

root@akupedia:~$ 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?

Solution:

root + rm + -r = disaster

So let us begin step by step?

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

Now, let’s see if your dpkg is working (start praying):

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

Now, let’s take a deep breath. Phew!!! Do ls -l /var/lib/dpkg and compare with the above list. If some -old file is not present don’t worry it will be there after few days.

Akash Angle

I am a Full time Linux user who has quit using Windows for unknown reasons, making my life truly open source.

Recent Posts

Get the most juice from your ISP/router — setting MTU size & other handy tweaks

This is not an ad-vocation by any means for TP-link branding, however a real life…

5 months ago

How to make any Android phone up-to 3x faster – Developer options unleashed

First of all we need to hit the kernel version on any Android device. You…

5 months ago

systemd-resolve command not found in Ubuntu Desktop

Use resolvectl status instead. It's like something deprecates and suddenly things get broken! In systemd…

10 months ago

How to exclude multiple directories with rsync?

Geeky question: This is what people and friends have tried: rsync -arv --exclude "/home/john/.ccache:/home/ben/build" /home/john…

10 months ago

How to resolve apt-get -f not working

You might encounter this error which appears to be very common on Debian based Distro's…

10 months ago

How to install Broadcom STA wireless drivers on Kali Linux

PS: This article is for only Kali Linux users, that too having a Broadcom Wireless…

10 months ago