How to Undo a Yum Install Including Dependencies on CentOS/RHEL

Posted on

As you tinker around with your CentOS/RHEL installation, you’ll find that you need to constantly install and uninstall stuff using yum. Perhaps you found a better solution than the one you’re currently using. Or you decided to go with a different technology and the older one needs to be removed. Maybe you wanted to test something out and decided to revert the changes.

Anyone using a Windows GUI knows how easy it is to uninstall a package. Simple go to “Add/Remove Programs”, click the uninstall option and you’re done. In Linux, it’s a bit different. This is because a package manager like “yum” installs not just the package you want, but a number of dependencies as well. These dependencies are separate packages that are needed for the original one to work. Linux software is a spiderweb of dependencies. In a way, this is great because it allows programmers to make changes and update dependencies independently of the original package.

But it can also leave a lot of useless stuff lying around your system if you don’t remove them properly.

Installing a Package also Installs Dependencies

For example, let’s say I want to install PHP 7 on my CentOS system. I first check to see if the package is available in my set of repos:

In this case, php7 is available with the REMI repo. I install it using yum as usual:

Note that while I’m installing PHP 7, a bunch of other dependencies are installed as well. In this case, 4 of them.

  1. php70-php-cli
  2. php70-php-common
  3. php70-php-json
  4. php70-runtime

Without these packages, PHP 7 can’t run. And these packages might have additional dependencies as well. It’s quite possible for this to continue and for a single “yum install” command to install dozens of packages with dependencies.

Removing a Package Doesn’t Remove Dependencies

The “yum remove” command uninstalls packages. Unfortunately, it focuses only on the specific package mentioned in the command. It doesn’t also include dependencies. So for example, if I remove the same PHP 7 package that I just installed, this is what happens:

You can see that only one package is removed. All the other dependencies that were installed remain on the system. What do we do about this? How do we completely “undo” a yum installation?

Undoing using yum’s “History” Command

The yum command maintains a history of all the times it was used. We can access this history by typing in the command:

yum history

This gives us the following:

You can see that each usage of yum has a separate line with an ID. It’s easy to tell which command you’re looking for. The most recent installation of phh70 has an ID number of 46. We can get further details about a particular yum history entry by using this command:

yum history info 46

This tells us which packages were altered or installed:

Conveniently, the “yum history” command also allows us to completely undo a yum install. That means all the actions taken with a particular yum command will be reverted. Not just the installation of the main package, but all the dependencies tied to that particular yum command as well. To do this, we simply need to specify the ID number to rollback like this:

yum history undo 46

This provides the following output:

Undo a yum Install

You can see that compared to the first time we used “yum remove”, this time all 5 packages that were being installed will be removed. 4 dependencies, plus one. This rollback only applies to yum history ID 46. Be careful because some dependencies that were installed with one yum command, might be necessary for other packages you might have installed afterwards. In general, the more recent a particular yum command, the safer it is to roll it back.

Use the yum history command often to completely get rid of packages you no longer need. It allows you to undo a yum install cleanly, and without hassles.

One Reply to “How to Undo a Yum Install Including Dependencies on CentOS/RHEL”!

Leave a Reply

Your email address will not be published. Required fields are marked *