5

Yesterday I had some problem with /var/lib/dpkg/lock during update. I knew that in such cases removing lock file could solve problem. So I tried to do it, but by mistake I removed all DPKG directory. So now I cannot do any update, install, upgrade and so on.

I try via Software Center, Terminal, Qapt, Synaptic. None of these work because all of them do such operations via dpkg. And as my dpkg is corrupted, so I have problem.

How can I completely restore DPKG? I have downloaded dpkg_1.16.1.2ubuntu7_amd64.deb (I use 64bit Ubuntu 12.04) file, but what to do with it in terminal? Simple double-clicking on .deb opens Software Center and you already know, it doesn't work.

P.S. Terminal gives such error:

dpkg: error: cannot read info directory: No such file or directory
E: Sub-process /usr/bin/dpkg returned an error code (2)
Braiam
  • 67,791
  • 32
  • 179
  • 269
  • 3
    Just a suggestion for next time; whenever deleting something slightly important, it is best not to delete it. But instead mv it to somewhere else, or install the trash-cli and use trash-put <file> which will move the file to the trash folder, allowing you to easily restore it from trash if you ever mess up. – Dan Aug 20 '12 at 07:52
  • 2
    Please edit your question with which version of Ubuntu you are running, and whether it's 32-bit or 64-bit. You should be able to get something working by copying a LiveCD's /var/lib/dpkg directory to your system. – ish Aug 20 '12 at 08:27
  • İzx, i use 12.04 64 bit. İ have liveCD, but unfortunatelly it is 32bit. it seems for 64 and 32 bit versions dpgk direcory contents are different, so i should download 64bit version from internet. – Elvin Haci Aug 20 '12 at 20:51
  • 1
    Thank you izx, i copied dpkg folder from live cd to my file system, and it worked. Although it brought nearly 400 MB unneeded update, but it is no problem, i re-updated all those packages. – Elvin Haci Aug 21 '12 at 14:28

2 Answers2

3

Okay I haven't tried this, but it should work I think.

First boot from a live CD.

Next open a terminal and type

sudo fdisk -l

From this you want to determine the partition that your Ubuntu installation is on. If you have only one hard drive it will be /dev/sdaX, where X is the partition number.

Next mount the Ubuntu installation's partition replacing X with the correct partition number.

sudo mount /dev/sdaX /mnt

Next navigate to the directory where dpkg_1.16.1.2ubuntu7_amd64.deb is and use this command:

sudo dpkg --root=/mnt -i dpkg_1.16.1.2ubuntu7_amd64.deb

Hopefully this will work, and you can reboot into your Ubuntu installation and find that dpkg has been reinstalled. If there are any errors let me know and I will try to help you.

edit:

I've been reading through the dpkg manpage and I think the command I suggested above might not work. Here are the relevant sections of the manpage for reference.

   --admindir=dir
          Change default administrative directory,  which  contains
          many   files   that  give  information  about  status  of
          installed or uninstalled  packages,  etc.   (Defaults  to
          /var/lib/dpkg)

   --instdir=dir
          Change default installation directory which refers to the
          directory where packages are to be installed. instdir  is
          also  the  directory  passed  to chroot(2) before running
          package's installation  scripts,  which  means  that  the
          scripts see instdir as a root directory.  (Defaults to /)

   --root=dir
          Changing  root  changes  instdir  to  dir and admindir to
          dir/var/lib/dpkg.

using --root=dir as I suggested would set the admin folder to /mnt/var/lib/dpkg--the folder that you deleted.

Instead try this:

sudo dpkg --force-overwrite --instdir=/mnt -i dpkg_1.16.1.2ubuntu7_amd64.deb

This will use the liveCD's /var/lib/dpkg folder, but the --force-overwrite flag should make it install even though it thinks the package is already installed.

Good luck!

edit2

While this should work to reinstall dpkg I don't think it will rebuild the package lists that are in /var/lib/dpkg. Following the advice in izx's comment to copy the directories/file from a liveCD and go from their is probably your best bet.

adempewolff
  • 11,958
  • This isn't nearly enough to recover your system. The dpkg database would be essentially empty, it would be like you didn't installed anything at all. – Braiam Jun 16 '15 at 00:42
1

I faced a similar situation recently. And most of the answers I found online were not of much help.

Basically, another option you have is to copy /var/lib/dpkg from another existing working installation. You can always install a fresh copy of your distribution in a virtual machine (like VirtualBox), and copy the directory from that clean copy.

I would try most options before this, so give it a shot as a last resort. I had another computer at home running the same distro, so I just copied the whole directory onto my machine, replacing a corrupted /var/lib/dpkg. Be sure to backup so you can return to whatever state you were in if this doesn't work.

JulioHM
  • 634