apt was having problems with dpkg. I was trying to do sudo apt install g++ but I kept on getting errors. It was very weird it kept on saying that g++ was already installed and fully upgraded.

I typed whereis g++ and it pointed me to /usr/bin/g++ But when I did ls /usr/bin/g++ it threw me a error that g++ wasn't in /usr/bin. I checked on the internet what everyone was doing and it seemed it was a problem with dpkg so I sudo rm /usr/bin/dpkg now my questions i how to fully re install it?

More importantly how can I completely refresh all my packages from scratch because I'm constantly running into problems.

I'm running Ubuntu 18.04.1 LTS.

  • Never, never, never manually remove files placed by the package manager unless you know exactly how to undo the damage it causes. – user535733 Oct 10 '18 at 1:56
  • 2
    By removing the dpkg application, you have gutted your package manager. dpkg is the application that actually installs and removes packages - apt merely computes and queues dpkg instructions and manages the repositories. Time to backup your data and reinstall. – user535733 Oct 10 '18 at 2:02
  • reinstall ubuntu? – Moshe Rabaev Oct 10 '18 at 2:05
  • 2
    Yes, reinstall Ubuntu. You deleted the engine that installs packages, so you cannot install packages anymore. Even if you fix that manually, you seems to have many other unspecified errors. It's simply not worthwhile to spend hours tracking down each problem. Reinstall the system and learn from your experiences. – user535733 Oct 10 '18 at 2:09
  • 3
    Possible duplicate of How do I recover from accidentally removing dpkg? – muru Oct 10 '18 at 2:20

Simple Answer

Reinstall Ubuntu

You've effectively gutted your system. If there's nothing really important on it, then I'd just do that as it will take you about 30 mins to an hour to get back to a working system on standard machine.

USB key recovery

Alternate Ubuntu install disk - Fix a Broken System

You could also download the Ubuntu USB key alternate version and boot from it, and then select the "Fix a broken system" option. This does a bunch of stuff like fix your MBR and check filesystem files.

Advanced answer

I've saved clients from worse mess-ups than this.

If you have access to another Ubuntu machine of the same type, or you can download and unzip the dpkg package for your architecture from your local mirror (e.g. https://packages.ubuntu.com/bionic/dpkg) then you could manually copy the dpkg program back into its original location.

As alluded to in the comments, this may not be your only problem. Once you have some semblance of dpkg and apt up and running again, if you can, I'd install the debsums package and recommend checking your other packages against the known checksums of the packages you already have installed.


dpkg -l | awk '/^ii/ { print $2 }' | xargs debsums | grep -vE 'OK$'

Then for each package mentioned:

apt-get --reinstall <package>

Then, after going through the output of that and making sure you have everything, I'd reinstall the ubuntu-core and ubuntu-desktop or ubuntu-server package also, which should bring everything back to a running state.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.