26

I know it is funny situation but i removed python with all associated programs from Ubuntu using sudo apt-get remove python? Obviously i can install back python, but it will take me a lot of time to install all programs that i removed. Maybe there is some solution?

Thanks

IgorDiy
  • 447
  • 1
  • 6
  • 10

9 Answers9

33

There is not an easy way but if you look at /var/log/apt/history.log you can see what was removed. Just reinstall each package that was removed.

ablackhat
  • 1,933
  • 2
  • 14
  • 19
  • 2
    It just saved my life! – Sanket Sahu May 24 '13 at 13:21
  • 3
    I registered with serverfault just to upvote this, thanks. – Phil Dec 09 '15 at 10:53
  • Thank you. Suggestion for installing the packages listed again - 1. Search for the time in file 2. Copy all the pacakges that are after the term "Remove:" 3. Remove the version number and replace the comma with space 4. then just prepend "sudo apt install" and run in terminal – Bharat D Bhadresha Feb 05 '21 at 18:42
29

I just did this mistake, you can run the below commands to undo the remove of packages This will extract the information of the removed packages from the log file and reinstall them

echo '#!/bin/bash' > restore
echo sudo apt-get install `grep Remove /var/log/apt/history.log | tail -1 | sed -e 's|Remove: ||g' -e 's|([^)]*)||g' -e 's|:[^ ]* ||g' -e 's|,||g'` >> restore
chmod +x restore 
./restore
tzafrir
  • 391
  • 3
  • 5
  • lifesaver... should've realised `apt remove libboost*` was a bad idea – ScottishTapWater Feb 24 '18 at 02:38
  • 4
    Thanks, you saved my life!! Reminder: NEVER RUN "sudo apt purge python2.7" –  Jul 09 '18 at 09:49
  • 5
    Don't run `sudo apt remove python3` haha – ThaJay Sep 06 '18 at 15:33
  • Well, I used `apt purge` so rip me, good thing it was a fresh install. Would be nice to know that key (for most people) OS systems, like netplan, require this. Sure it references it in the remove list, before asking if you're sure, but who has time to look and be sure when it's easier just to smack the `y` key and `return` your system into oblivion... – DeeJayh May 04 '19 at 17:52
8

I found a cool trick from the Ubuntu forums.

It is possible to run the following in a terminal:

awk '!/^Start|^Commandl|^End|^Upgrade:|^Error:/ { gsub( /\([^()]*\)/ ,"" );gsub(/ ,/," ");sub(/^Install:/,""); print}' /var/log/apt/history.log

Look at the output, then use

sudo apt-get install [packages]

and replace [packages] with the last line of packages that was returned (note that it may wrap).

2

Look up all the packages that got uninstalled. It is stored at /var/log/apt/history.log . You can write a shell program to install each package that was removed. Write it on any text editor GNU Emacs, vi, GNU Nano. Execute it through the terminal. All the programs that got uninstalled will get re-installed by themselves.

user234756
  • 21
  • 2
1

To add to ablackhat's answer, you can copy and paste everything after the last remove command in /var/log/apt/history.log into a new programmers editor. Regex replace :.*?, with (empty string), and copy all the resulting space-separated packages. Then run $ sudo apt-get install <paste>

Brandon
  • 136
  • 1
  • 2
  • 7
1

Upgrading restores most of it, the rest.. you can use the techniques shown above. To upgrade just type "sudo apt-get upgrade"

Thom
  • 21
  • 1
0

I tried executing @user155761 's answer, but that backfired. My /var/log/apt/history.log content got removed.

Incase you also face this, please use the .gz logs kept in same folder. In some other answers on Stack Overflow, I got to know that the backup logs are kept. This rreally saved me personally speaking.

Varun
  • 1
  • 1
0

I can't upvote or comment, due to insufficient reputation but tzafrir's answer is the closest there is to the way to actually reverse what you have just uninstalled.

I have used a shorter form, in the past and it worked fine for me. Always.

apt install $(grep Remove /var/log/apt/history.log | tr ',' '\n' | cut -d':' -f1 | tr '\n' ' ' | cut -d' ' -f3-)

Where grep Remove /var/log/apt/history.log is the following:

/home/andrei# grep Remove /var/log/apt/history.log

Remove: python3.8-venv:amd64 (3.8.10-0ubuntu1~20.04.7), python3-pexpect:amd64 (4.6.0-1build1), python3-distro-info:amd64 (0.23ubuntu1), python3-dev:amd64 (3.8.2-0ubuntu2), python3-deprecated:amd64 (1.2.7-1), gitsome:amd64 (0.8.0+ds-4), python3-json-pointer:amd64 (2.0-0ubuntu1), python3-roman:amd64 (2.0.0-3build1), netplan.io:amd64 (0.104-0ubuntu2~20.04.2), python3-dulwich:amd64 (0.19.15-1build1), cloud-init:amd64 (23.1.1-0ubuntu0~20.04.1), python3-mimeparse:amd64 (1.6.0-3build1), landscape-common:amd64 (19.12-0ubuntu4.3), python3-unittest2:amd64 (1.1.0-6.1build1), update-manager-core:amd64 (1:20.04.10.11), networkd-dispatcher:amd64 (2.1-2~ubuntu20.04.3), python3-zope.interface:amd64 (4.7.1-1), python3-distutils:amd64 (3.8.10-0ubuntu1~20.04), gvfs-backends:amd64 (1.44.1-1ubuntu1.2), python3-gpg:amd64 (1.13.1-7ubuntu2), python3-linecache2:amd64 (1.0.0-4ubuntu1), python3-olefile:amd64 (0.46-2), python3-babel:amd64 (2.6.0+dfsg.1-1ubuntu2.2), python3-software-properties:amd64 (0.99.9.11), python3-icu:amd64 (2.4.2-0ubuntu3), pyzor:amd64 (1:1.0.0-3), python3-sigmavirus24-urltemplate:amd64 (3.0.1-1), byobu:amd64 (5.133-0ubuntu1), python3-wadllib:amd64 (1.3.3-3build1), python3-setproctitle:amd64 (1.1.10-1ubuntu1), update-notifier-common:amd64 (3.192.30.17), python3-sphinx:amd64 (1.8.5-7ubuntu3), python3-distro:amd64 (1.4.0-1), python3-jwt:amd64 (1.7.1-2ubuntu2.1), python3-configargparse:amd64 (0.13.0-2), python3-ldb:amd64 (2:2.4.4-0ubuntu0.20.04.1), python3-systemd:amd64 (234-3build2), python3-josepy:amd64 (1.2.0-2), brz:amd64 (3.0.2-4ubuntu2), bzr:amd64 (2.7.0+bzr6622+brz), pastebinit:amd64 (1.5.1-1), ubuntu-advantage-tools:amd64 (27.13.6~20.04.1), python3-urllib3:amd64 (1.25.8-2ubuntu0.2), certbot:amd64 (0.40.0-1ubuntu0.1), python3-zope.hookable:amd64 (5.0.0-1build1), language-selector-common:amd64 (0.204.2), python3-jsonpatch:amd64 (1.23-3), python3-subunit:amd64 (1.4.0-0ubuntu1), python3-filelock:amd64 (3.0.12-2), python3-pbr:amd64 (5.4.5-0ubuntu1), ubuntu-standard:amd64 (1.450.2), python3-pil:amd64 (7.0.0-4ubuntu0.7), python3-pip:amd64 (20.0.2-5ubuntu1.8), python3-ply:amd64 (3.11-3ubuntu0.1), python3-keyring:amd64 (18.0.1-2ubuntu1), python3-wheel:amd64 (0.34.2-1ubuntu0.1), python3-entrypoints:amd64 (0.3-2ubuntu1), python3-traceback2:amd64 (1.4.0-5ubuntu1), python3-netifaces:amd64 (0.10.4-1ubuntu4), python3.8:amd64 (3.8.10-0ubuntu1~20.04.7), fail2ban:amd64 (0.11.1-1), open-vm-tools:amd64 (2:11.3.0-2ubuntu0~ubuntu20.04.4), python3-gssapi:amd64 (1.6.1-1build1), python3:amd64 (3.8.2-0ubuntu2), python3-service-identity:amd64 (18.1.0-5build1), python3-idna:amd64 (2.8-1), python3-wrapt:amd64 (1.11.2-2build1), python3-rpm:amd64 (4.14.2.1+dfsg1-1build2), python3-augeas:amd64 (0.5.0-1.1), python3-zope.event:amd64 (4.4-2build1), python3-testtools:amd64 (2.4.0-0ubuntu1), python3-lazr.restfulclient:amd64 (0.14.2-2build1), plymouth-theme-ubuntu-text:amd64 (0.9.4git20200323-0ubuntu6.2), python3-six:amd64 (1.14.0-2), python3-github:amd64 (1.43.7-1), ubuntu-server:amd64 (1.450.2), python3-jsonschema:amd64 (3.2.0-0ubuntu2), python3-numpydoc:amd64 (0.7.0-2), python3-lazr.uri:amd64 (1.0.3-4build1), hibagent:amd64 (1.0.1-0ubuntu1), python3-zope.component:amd64 (4.3.0-3), python3-prompt-toolkit:amd64 (2.0.10-2), python3-breezy:amd64 (3.0.2-4ubuntu2), python3-appdirs:amd64 (1.4.3-2.1), python3-mock:amd64 (3.0.5-1build1), python3-requests:amd64 (2.22.0-2ubuntu1), python3-distlib:amd64 (0.3.0-1), python3-httplib2:amd64 (0.14.0-1ubuntu1), python3-gitlab:amd64 (1:2.0.1-1), python3-fastimport:amd64 (0.9.8-5build1), python3-pyparsing:amd64 (2.4.6-1), python3-pyinotify:amd64 (0.9.6-1.2ubuntu1), python3-pymacaroons:amd64 (0.13.0-3), python3-twisted:amd64 (18.9.0-11ubuntu0.20.04.2), python3-simplejson:amd64 (3.16.0-2ubuntu2), python3-imagesize:amd64 (1.2.0-1), python3-importlib-metadata:amd64 (1.5.0-1), samba-libs:amd64 (2:4.15.13+dfsg-0ubuntu0.20.04.1), ubuntu-minimal:amd64 (1.450.2), apport:amd64 (2.20.11-0ubuntu27.25), ec2-hibinit-agent:amd64 (1.0.0-0ubuntu9.2), python3-twisted-bin:amd64 (18.9.0-11ubuntu0.20.04.2), python3-future:amd64 (0.18.2-2ubuntu0.1), python3-cryptography:amd64 (2.8-3ubuntu0.1), python3-distupgrade:amd64 (1:20.04.41), python3-zipp:amd64 (1.0.0-1), python3-debconf:amd64 (1.5.73), python3-magic:amd64 (2:0.4.15-3), python3-commandnotfound:amd64 (20.04.6), xfsprogs:amd64 (5.3.0-1ubuntu2), python3-update-manager:amd64 (1:20.04.10.11), ubuntu-release-upgrader-core:amd64 (1:20.04.41), python3-apport:amd64 (2.20.11-0ubuntu27.25), python3-docopt:amd64 (0.6.2-2.2ubuntu1), python3-constantly:amd64 (15.1.0-1build1), python3-cffi-backend:amd64 (1.14.0-1build1), python3-alabaster:amd64 (0.7.8-1build1), python3-click:amd64 (7.0-3), python3-enchant:amd64 (2.0.0-4), python3-docutils:amd64 (0.16+dfsg-2), python3-incremental:amd64 (16.10.1-3.2), python3-venv:amd64 (3.8.2-0ubuntu2), python3-markupsafe:amd64 (1.1.0-1build2), ufw:amd64 (0.36-6ubuntu1), python3-acme:amd64 (1.1.0-1), xonsh:amd64 (0.9.13+dfsg-2), python3-configobj:amd64 (5.0.6-4), python3-more-itertools:amd64 (4.2.0-1build1), python3-blinker:amd64 (1.4+dfsg1-0.3ubuntu1), python3-decorator:amd64 (4.4.2-0ubuntu1), python3-lib2to3:amd64 (3.8.10-0ubuntu1~20.04), python3-yaml:amd64 (5.3.1-1ubuntu0.1), python3-pkg-resources:amd64 (45.2.0-1ubuntu0.1), python3-breezy.tests:amd64 (3.0.2-4ubuntu2), python3-openssl:amd64 (19.0.0-1build1), python3-kerberos:amd64 (1.1.14-3.1build1), python3-hyperlink:amd64 (19.0.0-1), python3-rfc3339:amd64 (1.1-2), python3-requests-toolbelt:amd64 (0.8.0-1.1), libsmbclient:amd64 (2:4.15.13+dfsg-0ubuntu0.20.04.1), python3-certbot-apache:amd64 (0.39.0-1), sosreport:amd64 (4.4-1ubuntu0.20.04.1), python3-dbus:amd64 (1.2.16-1build1), python3-talloc:amd64 (2.3.3-0ubuntu0.20.04.1), lsb-release:amd64 (11.1.0ubuntu2), python3-certbot:amd64 (0.40.0-1ubuntu0.1), python3-gi:amd64 (3.36.0-1), python3-paramiko:amd64 (2.6.0-2ubuntu0.1), python3-tz:amd64 (2019.3-1ubuntu0.20.04.0), python3-ptyprocess:amd64 (0.6.0-1ubuntu1), unattended-upgrades:amd64 (2.3ubuntu0.3), python3-automat:amd64 (0.8.0-1ubuntu1), python3-oauthlib:amd64 (3.1.0-1ubuntu2), python3-launchpadlib:amd64 (1.10.13-1), python3-pygments:amd64 (2.3.1+dfsg-1ubuntu2.2), command-not-found:amd64 (20.04.6), ssh-import-id:amd64 (5.10-0ubuntu1), python3-nacl:amd64 (1.3.0-5), python3-hamcrest:amd64 (1.9.0-3), python3-pyrsistent:amd64 (0.15.5-1build1), supervisor:amd64 (4.1.0-1ubuntu1), python3.8-dev:amd64 (3.8.10-0ubuntu1~20.04.7), python3-wcwidth:amd64 (0.1.8+dfsg1-3), python3-requests-unixsocket:amd64 (0.2.0-2), python3-bcrypt:amd64 (3.1.7-2ubuntu1), python3-jinja2:amd64 (2.10.1-2), rpmlint:amd64 (1.11-0.2), python3-certbot-nginx:amd64 (0.40.0-0ubuntu0.1), python3-debian:amd64 (0.1.36ubuntu1), python3-gdbm:amd64 (3.8.10-0ubuntu1~20.04), python3-fixtures:amd64 (3.0.0-3build1), python3-newt:amd64 (0.52.21-4ubuntu2), python3-certifi:amd64 (2019.11.28-1), cloud-guest-utils:amd64 (0.31-7-gd99b2d76-0ubuntu1), python3-parsedatetime:amd64 (2.4-5), python3-secretstorage:amd64 (2.3.1-2ubuntu1), python3-chardet:amd64 (3.0.4-4build1), python3-problem-report:amd64 (2.20.11-0ubuntu27.25), python3-colorama:amd64 (0.4.3-1build1), python3-virtualenv:amd64 (20.0.17-1ubuntu0.4), python3-serial:amd64 (3.4-5.1), python3-pyasn1-modules:amd64 (0.2.1-0.2build1), python3-extras:amd64 (1.0.0-4build1), python3-packaging:amd64 (20.3-1), python3-apt:amd64 (2.0.1ubuntu0.20.04.1), python3-setuptools:amd64 (45.2.0-1ubuntu0.1), python3-attr:amd64 (19.3.0-2), python3-pyasn1:amd64 (0.4.2-3build1), python3-feedparser:amd64 (5.2.1-2), dh-make:amd64 (2.201903), software-properties-common:amd64 (0.99.9.11)

Full filter result here

Which was caused by sudo apt remove python3.8

-1

if you

 apt-get install python

Python will be reinstalled with all of its prerequisites which is what I'm assuming was all the associated programs your speaking about?

Nick M.
  • 371
  • 1
  • 6
  • 12
  • I have already installed Python, but programs which where dependent on it like Zim, GImp and a lot of others wasn't reverted(( Even Gedit isn't working – IgorDiy Apr 18 '12 at 20:11
  • -1. Doesn't resolve the OP's question in that everything that needs Python got removed also. – Magellan Apr 19 '12 at 14:23