97

I installed Carpadio on my Xubuntu install (a Live USB), which then pulled like over 50 packages from, I suppose, the Ubuntu side of the repository. Now I would like to undo this. However for all my hunting, I can't find anything more useful than How can I reverse sudo apt get install command, which just suggests apt-get purge. This is not useful since all it does is remove 2 packages (carpadio and carpadio-gnomepanel).

/var/log/apt/history.log has a list of all the packages that was installed along with that command. So I was wondering if there is any easy way to go about removing all of them? Else I can just reinstall, this was a fresh install anyway, but I am curious.

A small sample of the history log (1/5-1/10 of the entire list)

Commandline: apt-get install cardapio-gnomepanel
Install: libdbusmenu-qt2:amd64 (0.9.2-0ubuntu1, automatic), python-packagekit:amd64 (0.7.2-4ubuntu3, automatic), cups-pk-helper:amd64 (0.2.1.2-1ubuntu0.1, automatic), geoclue:amd64 (0.12.0-1ubuntu12, automatic), unity:amd64 (5.18.0-0ubuntu1, automatic), indicator-printers:amd64 (0.1.6-0ubuntu1, automatic), libevolution:amd64 (3.2.3-0ubuntu6, automatic), libqt4-declarative:amd64 (4.8.1-0ubuntu4.3, automatic), libmtp9:amd64 (1.1.3-1ubuntu0.1, automatic), tracker-miner-fs:amd64 (0.14.0-2ubuntu1, automatic), gir1.2-rb-3.0:amd64 (2.96-0ubuntu4.2, automatic), python-aptdaemon.pkcompat:amd64 (0.43+bzr805-0ubuntu7, automatic), gnome-media:amd64 (3.4.0-0ubuntu3.1, automatic), metacity:amd64 (2.34.1-1ubuntu11, automatic), nautilus:amd64 (3.4.2-0ubuntu6, automatic), libcompizconfig0:amd64 (0.9.7.0~bzr428-0ubuntu6, automatic), compiz-plugins-default:amd64 (0.9.7.12-0ubuntu1, automatic), libunistring0:amd64 (0.9.3-5, automatic), libebackend-1.2-1:amd64 (3.2.3-0ubuntu7, automatic), ubuntu-docs:amd64 (12.04.6, automatic), python-mako:amd64 (0.5.0-1, automatic),

To all those who have suggested autoremove: I am not sure why, but I got just 1 other python related package (python-keybinder or something) as "unnecessary" and that is all that autoremove was able to remove.

Between the install and uninstall, only major change was installing xubuntu-restricted-extras.

Karthik T
  • 2,116

11 Answers11

107

Basically, you'll have to both apt-get remove (or apt-get purge) the package and run apt-get autoremove after that, to have it revert the installation of package-one.

Let's look at the whole process:

  • sudo apt-get install package-one installs 50 dependencies with it marked "automatic" as also can be seen from the log excerpt in your question

  • sudo apt-get purge package-one removes (purges) just one, but do run this!

    All following install actions (if you run any) will yield an informational message with the no longer needed packages:

    The following packages were automatically installed and are no longer required:
      package-two package-three [...]
    Use 'apt-get autoremove' to remove them.
    

    This list is basically just a list of all packages marked as "automatic" without a reverse dependency on them. In other words, there's no reason for them to be installed as far as the package management is concerned.

    Note: No installation is needed! It's just to demonstrate that APT is smart to tell you about your unneeded packages!

  • sudo apt-get autoremove --purge removes (purges) these

More information

gertvdijk
  • 69,427
22

When you want to remove a package and its dependencies that installed at the time, you should use:

sudo apt-get purge package
sudo apt-get --purge autoremove

but to undo apt actions, you can use apt-undo script. It is a simple script that can undo the apt actions performed in Ubuntu. It is to be noted that this script can only work if you use it instead of apt-get to install/upgrade/remove/purge/downgrade your packages.

To install apt-undo in Ubuntu, run the following commands in the terminal:

sudo add-apt-repository ppa:lkjoel/apt-undo
sudo apt-get update
sudo apt-get install apt-undo

usage:

apt-undo install yourpackages
apt-undo remove yourpackages
apt-undo purge yourpackages
apt-undo upgrade
apt-undo dist-upgrade
apt-undo install yourpackages=old.version
apt-undo install yourpackages=new.version

To undo, the above aptitude actions run following commands in the terminal:

apt-undo undo
Eliah Kagan
  • 119,640
Ramin Omrani
  • 1,274
7

Use the power of Unix. Take the log file line that you have, and construct a command that will undo what apt did. For example:

$ echo 'Install: libdbusmenu-qt2:amd64 (0.9.2-0ubuntu1, automatic), python-packagekit:amd64 (0.7.2-4ubuntu3, automatic), cups-pk-helper:amd64 (0.2.1.2-1ubuntu0.1, automatic),'|perl -pe 's/ \(.*?\)//g; s/,//g; s/^Install: //'
libdbusmenu-qt2:amd64 python-packagekit:amd64 cups-pk-helper:amd64

So you can use this purge all packages you installed accidentally, given the appropriate line from your log file:

$ dpkg -P $(echo '(full log line here)' | perl -pe 's/ \(.*?\)//g; s/,//g; s/^Install: //')

(I've used perl instead of sed because sed uses a type of regular expression which doesn't support non-greedy matches, which was the easiest way of constructing what I needed)

Robie Basak
  • 15,910
4

To undo the last apt install you may also use this:

sudo apt purge $(grep "Install:" /var/log/apt/history.log | tail -n 1 | tr " " "\n" | grep -E ":amd64|:i386|:arm" | tr "\n" " ")

This will catch the last apt install from the history and suggests to purge the corresponding packages. apt will display you a list of all packages in question which needs to be confirmed, so you keep control about what happens.

You can create a function called ”aptpurgelastinstall“ by executing this command:

echo -e "\n# undo the last apt installation with aptpurgelastinstall" >> ~/.bashrc && echo 'aptpurgelastinstall () { sudo apt purge $(grep "Install:" /var/log/apt/history.log | tail -n 1 | tr " " "\n" | grep -E ":amd64|:i386|:arm" | tr "\n" " ") ; }' >> ~/.bashrc && . ~/.bashrc

Now you simply need to type aptpurgelastinstall in a shell to undo the last installation.


If you want to specify how many previous installations should be purged (all in one step), then change the 1 in the code to $1.

Then you can type for instance aptpurgelastinstall 3 in the shell and the last three installations will be completely purged.

T.K
  • 43
2

Technically "remove" or "autoremove" operations are not "undo" although they can be made to do the job good enough most of the times.

By default, apt-get will leave packages behind, unless you specify -o APT::Suggests-Important=0 -o APT::Recommends-Important=0

It is more reliable to take a look at the apt history.log and uninstall all the installed packages. I have created a small node-based script to ease the process:

https://github.com/rolfen/apt-history

Also, here is another, more manual approach to "rolling back" changes:

https://unix.stackexchange.com/a/236711

Rolf
  • 2,229
2
  1. Paste the long list of unwanted packages from the apt log (var/log/apt/history.log) into a new_file.txt
  2. Execute perl -pe 's/\(.*?\)(, )?//g' /path/to/new_file.txt
  3. Copy the output of the last command to the clipboard or pipe it there in the first place.
  4. Execute sudo apt-get remove [paste the contents of the clipboard here]

Done.

Basically we need to format the list of packages from the apt log into something apt-get can understand. The parenthesis, words in between, and the commas need to go so that's where the perl comes in to play. Yes, there are better (more efficient) ways - but I've done this, its easy to understand, and it works.

Insperatus
  • 5,093
1

To undo the action of the sudo apt install command, you want to do two things:

  1. Uninstall the packages that command installed
  2. Undo the marking of the packages that were marked as "manually installed" and marked them as "automatically installed" instead

For example, let's say I installed a bunch of build dependencies for something:

$ sudo apt install build-essential libgl1-mesa-dev libxkbcommon-x11-0 libpulse-dev libgles-dev libgles1 libglvnd-core-dev libglvnd-dev libopengl-dev

It's impossible to tell just from the text of this command to know how to undo it, because we don't know yet which packages this command installed, which packages were marked as manually installed, and which packages were unaffected (since they were already installed and marked as manually installed). We need to look at the output of that command first, and for each line, undo it.

Here's the output:

$ sudo apt install build-essential libgl1-mesa-dev libxkbcommon-x11-0 libpulse-dev libgles-dev libgles1 libglvnd-core-dev libglvnd-dev libopengl-dev
build-essential is already the newest version (12.9ubuntu2).
libxcb-glx0 is already the newest version (1.14-3ubuntu1).
libxcb-glx0 set to manually installed.
The following NEW packages will be installed
  libgl1-mesa-dev libgles-dev libgles1 libglvnd-core-dev libglvnd-dev libopengl-dev libpulse-dev
# output cut for brevity

Lines that look line this:

libxcb-glx0 is already the newest version (1.14-3ubuntu1).
libxcb-glx0 set to manually installed.

... can be undone by running:

$ sudo apt-mark auto libxcb-glx0

Lines that look like this:

The following NEW packages will be installed
  libgl1-mesa-dev libgles-dev libgles1 libglvnd-core-dev libglvnd-dev libopengl-dev libpulse-dev

... can be undone by running:

$ sudo apt remove libgl1-mesa-dev libgles-dev libgles1 libglvnd-core-dev libglvnd-dev libopengl-dev libpulse-dev

Lines that look like this:

build-essential is already the newest version (12.9ubuntu2).

... do not need to be undone, since the package was already installed and marked as manually installed.

If you just take the list of packages from sudo apt install and run sudo apt remove, you risk removing packages that depend on these packages, even though those packages were already installed before the sudo apt install command was run.

Flimm
  • 44,031
1

After you remove the one package, and you can remove it thoroughly by adding --purge, for, apt-get remove --purge <package>, it should give you a message that the library packages it pulled in were installed automatically and no longer necessary, at which point you can run apt-get autoremove to remove them.

Ken Kinder
  • 4,250
1

Follow the sudo apt-get purge packagename with

sudo apt-get autoremove. 

Unless it's metapackage this should take care of it.

Also see the faq at How do I manually remove all Cardapio files?.

chaskes
  • 15,536
0

If you have just added a ppa and want to revert, you need to do the steps described below. Understand this can fail, when you've installed other packages you don't want to uninstall. I'm sure this can be done with a script. Personally I think this should be a part of apt, and there should be better administration of which packages are installed because of dependencies and which are installed by request of the user.

1) remove the ppa from /etc/apt/sources.list Remember the time you added the ppa. If you're lucky, you don't need to be precise.

2) make a list what to reinstall, from the moment you installed the PPA: cat /var/log/dpkg.log | grep 'upgrade '

3) make a list what to remove, from the moment you installed the PPA: cat /var/log/dpkg.log | grep 'install '

4) remove all packages from step 3: sudo apt-get remove {list of packages from step 3}

5) reinstall all packages from step 2: sudo apt-get install --reinstall {list of packages from step 2}

6) If things went wrong and you cannot think of a solution, put back the PPA and reinstall all packages from the two lists.

0

I know it's a quite old question but I came here searching for help and maybe I can help someone. I had a situation similar to @Karthik-T's one after installing unity8-desktop-session-mir. I know it's a big one. It pulled 233 packets with him.

Using autoremove I get removed only 34 of them. The point is that during unity8-desktop-session-mir installation, it removed two packages (python3-aptdaemon.pkcompat and evolution-data-server-online-accounts). It's stated in /var/log/apt/history.log, after the Install section.

To completely undo the Unity 8 installation I issued the followings:

  1. sudo apt-get purge unity8-desktop-session-mir
  2. sudo apt-get --purge autoremove
  3. sudo apt-get install python3-aptdaemon.pkcompat evolution-data-server-online-accounts (it removed 8 packages related to Unity 8)
  4. sudo apt-get --purge autoremove

This will not remove absolutely everything you installed before, but in some case is good enough.

PS: perhaps the autoremove at step two is not necessary, as I issued again at step four.