How to Keep HomeBrew Clean on a Mac
HomeBrew is a package manager (similar to the linux “apt-get”/”zypper”/”yum” front end package managers that are usually shipped with different distros) with one exception: it was built for Apple MacOS systems as a solution since Apple doesn’t offer or ship one with MacOS.
While HomeBrew is a great solution and offers most features other modern package managers offer and has a rich reposatory, it lacks some useful features that make it difficult to use for the purpose of managing installed packages. One issue that I would like to describe here is the lack automatic removal of dependencies.
HomeBrew does not keep logs of installed dependencies the same way that the popular Linux package managers do. Some packages require dependencies to work and while HomeBrew can detect the required dependencies and install them automatically as part of the installation process, it will not remove them automatically while removing packages.
This can lead to outdated unnecessary software with vulnerabilities being installed on the system.
The good news is that there is a solution for this issue: rmtree.
rmtree is (as its official GitHub page describes) an external HomeBrew command that allows the user to “Remove a formula and its unused dependencies.”
While the page warns that it may not work in all cases, I personally found it usable and reliable.
The command can be added to HomeBrew by simply executing the following command:
$ brew tap beeftornado/rmtree
Once added, packages (and their dependencies) can be removed by using “brew rmtree” instead of the normal “brew remove”.
Remove All Packages
Prefer to start over and remove all installed packages? Look not further!
Below is a command I put together that will accomplish this quickly and easily:
$ brew uninstall –ignore-dependencies –force $(brew list)
This command will obtain a list of all installed packages (With the help of the “brew list” command) and force all of them to be removed “ignoring” dependencies to eliminate any warnings from showing up.
Once this is done, I usually like to execute the “brew cleanup” and then the “brew doctor” commands to make sure that HomeBrew is fully clean and functional (the doctor command will make sure that there are no issues with HomeBrew).
It is also a good idea to execute “brew update” once in a while to make sure that the most recent version of HomeBrew is installed on the system.
Please note that the commands above will not remove or modify any casks.