How to uninstall then install brew: a short adventure

26 Oct 2015, by Pang Yan Han

Roughly 2 weeks ago I was writing a Bash script to automate some stuff, one of them being installing brew. To test if this script was working, I needed to uninstall brew. So I googled for it and came across the homebrew FAQ which says to run the following:

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/uninstall)"

which I did.

Then I proceeded to run my script, which contains the following line of code (from the brew website) to install brew:

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

However, this command caused the following error:

It appears Homebrew is already installed. If your intent is to reinstall you

should do the following before running this installer again:

    ruby -e “$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/uninstall)”

Ok, I thought I uninstalled brew already. Never mind, maybe the uninstallation script went wrong. So I ran it again:

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/uninstall)"

and if memory serves (after all, it was about 2 weeks ago), I got the following error:

Failed to locate Homebrew!

Ok… let’s install brew again:

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

and I got the same error again:

It appears Homebrew is already installed. If your intent is to reinstall you

should do the following before running this installer again:

    ruby -e “$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/uninstall)”

Wtf.

Refusing to believe this, I probably went through the cycle at least 2 more times. I have seen and know the definition of insanity, which is:

doing the same thing over and over again and expecting different results.

and I was definitely not insane. So to solve this problem, I had to do something different. I googled around but couldn’t find anything helpful. After some time, the following thought firmly presented itself in my mind: I screwed up my brew installation and there is a chance I won’t be able to install brew again.

But I needed brew back. And while there is indeed a chance that I screwed things up so bad that I wouldn’t be able to install brew ever again, that’s a pretty slim chance. I knew for a fact that brew was uninstalled, because 1. brew itself was gone and 2. all the programs that were installed using brew were gone as well. So the uninstallation mostly worked properly. The script that fucked up was probably the installation script. I knew that I’d have to look inside the brew installation script to see what went wrong. Kind of intimidating. I was also pretty tired and needed a break, so I went for one.

After my break, I downloaded the brew installation script file. Ok. So it’s over 200 but below 300 line of ruby code. Not so intimidating after all. Then I looked around for text that resembled the above error I got that complained about brew being installed on my system. And I found it! Here’s how the ruby code looked like:

abort <<-EOABORT unless Dir["#{HOMEBREW_PREFIX}/.git/*"].empty?
It appears Homebrew is already installed. If your intent is to reinstall you
should do the following before running this installer again:
    ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/uninstall)"
EOABORT

Hmm, so to trigger this abort, Dir["#{HOMEBREW_PREFIX}/.git/*"].empty? must be false. So what is HOMEBREW_PREFIX ? Turns out it’s defined right at the top of the brew installation script as /usr/local . Ok, so /usr/local/.git must be present on my mac. And indeed it is. That’s the cause of the error. And indeed it looks like a .git directory whenever one clones a git repository. Let’s remove it:

rm -rf /usr/local/.git

And run the brew installation script:

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

which worked flawlessly this time.

What did I learn from this?

  1. Taking a break can be very helpful when you’ve gotten stuck on a problem for a long time. It may seem counterintuitive, but it has worked for me many, many times.
  2. Do not put anything on a pedestal - you can achieve more than you think and usually a lot of things are not as intimidating as they seem once you get down to them. I thought that it will be very intimidating to look into the brew installation script to find out what’s causing the error I was getting, but I was pleasantly surprised both by the amount of code I had to read (not a lot) and the ease with which I diagnosed the problem. Of course, not everything is as straightforward as this but often times they’re not as scary as we think - it all depends on how determined we are towards solving the problem. That being said, opening up the brew installation script to find out the error (after I’ve taken my break of course) wouldn’t have been as straightforward a decision to make if I hadn’t had the experience of “opening up the box” many, many times by looking into other people’s code, finding out what’s wrong and fixing the error. I’ve clearly forgotten when I first had to do this but, the first step I took was extremely important.
comments powered by Disqus