How do I recover from accidentally removing dpkg?

By mistake I have removed dpkg from my pc (ubuntu 16.04). When I am running apt commands, it was showing error :

Could not exec dpkg!
E: Sub-process /usr/bin/dpkg returned an error code (100)

Can anyone suggest me any solution to this. Thanks in advance !!!


To fix this, open a terminal and download the dpkg Install dpkg package:

# For i386:
wget http://mirrors.kernel.org/ubuntu/pool/main/d/dpkg/dpkg_1.18.4ubuntu1_i386.deb
# For amd64
wget http://mirrors.kernel.org/ubuntu/pool/main/d/dpkg/dpkg_1.18.4ubuntu1_amd64.deb

The versions will differ for different Ubuntu releases. If apt-get is still available (only dpkg was removed), you can still use apt-get to just download the appropriate version:

apt-get download dpkg

Or you can use https://packages.ubuntu.com/dpkg to get a download link for your Ubuntu release and architecture.

Extract the data.tar.gz file from the .deb file, which is actually an ar archive:

ar x dpkg_*.deb

Extract the package contents from data.tar.gz into the filesystem:

sudo tar xf data.tar.gz -C / .

Note: this will get just enough of dpkg working to let you use it. Be sure to install the dpkg package afterwards:

sudo apt-get install --reinstall dpkg