10

I used sudo apt-get remove python command and accidently removed a bunch of packages that were required. I logged in via command line and installed ubuntu-desktop again but there are other packages that are missing, and I'm looking for a way to easily reinstall those removed packages. Since there's the log at software-center I wanted to ask what the easiest way might be to roll back changes or extract the removed packages list from the software center...

note: I typed sudo apt-get install .... .... ... ... for about two dozen of those removed programs in that list, but when I pressed enter it didn't install any of them because some package names couldn't be found.

The programs were removed at the same date.

1
  • 2
    Look in /var/log/apt/history.log
    – Lekensteyn
    Jun 26, 2011 at 12:35

2 Answers 2

18

First of all, let me say that removing python can lead to many fundamental commands not work anymore. So I don't known if what follow will work.

Secondly, I assume that the package names couldn't be found error you have, derives from a mispelled name on the command line, or from a package installed locally and not available in repositories.

Then, to obtain a log of removed packages, relatively to the last dpkg log available (I don't think you need to take into account older logs):

awk '$3 == "remove" { print $1, $2, $4 }' /var/log/dpkg.log | tee list

Then edit the list file created in the current directory, and only leave lines relative to packages you want to reinstall, based on timestamp of the line. Say you save the modified file to list-mod.

To reinstall that packages, use the following command:

sudo apt-get --simulate install $(awk '{ print $3 }' list-mod)

I inserted the --simulate option to see what the command would do. If it is all ok, do the command again with that option removed.

If the command say some packages cannot be found, simply remove them from list-mod and try again.

5
  • thank you, i was able to reinstall the packages i needed! One question though, there is more than 200 packages removed on software center log, but the list file had about 150, does this mean that dpkg.log doesn't contain automatic installs/dependent packages? again thanks for this solution :)
    – Logan
    Jun 26, 2011 at 13:13
  • 1
    That log file contain every installed, upgraded, removed or purged package, and with every application from dpkg to apt-get, aptitude, Synaptic, Software Center, gdebi, etc. You can give a look at older dpkg.log files, see ls -lrt /var/log/dpkg.log*.
    – enzotib
    Jun 26, 2011 at 13:19
  • 1
    @enzotib I ran a script for purging old linux headers, and i think that the regular expression matched many of the packages which I did not want to delete (very important ones!!). Your answer saved me from a certain disaster.
    – shivisuper
    Jun 28, 2016 at 8:28
  • 1
    @enzotib You just saved me from reinstalling my OS after running a bad script. Thanks!
    – brndn2k
    Apr 9, 2018 at 8:24
  • it says 0 upgraded, 0 installed, 0 to remove and 50 not upgraded Oct 12, 2019 at 6:45
3

So, I happen to do a similar mistake: sudo apt-get purge python3.6* instead of sudo apt-get purge python3.6.*.

I was able to fix it quite fast, with the following steps:

  1. Opened the log vim /var/log/history.log.
  2. Searched for my bad command using vim command /python3.6\*.
  3. There was a line Purge: unity-control-center-signon:amd64 (0.1.7~+14.04.20140211.2-0ubuntu4), ... with a long list of purged packages (in case of removal it will start with Remove:), which I copied to another file without the Purge: part to work with it.
  4. In that file I ran the vim command: %s/:[^,]*,//g, which removed the version part and left me with a list of packages unity-control-center-signon lxc ....
  5. Finally I copied a line from the previous step and ran: sudo apt-get install unity-control-center-signon lxc ..., and that's how it got fixed.
1
  • for me it was /var/log/apt/history.log
    – Taylor
    Sep 23, 2019 at 20:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .