2019-03-23

Homebrew blew up from libffi, ruby, or? Here's how to fix it.

I wanted to upgrade Homebrew itself and what it installed on my Mac.  It's probably been over a year, so lots of outdated stuff.  I ran:

brew update
brew upgrade

Everything looked good, and it gave me some instructions to add to my PATH the Homebrew installed ruby and ruby gems along the lines of adding the following to my .bash_profile:

PATH="/usr/local/opt/ruby/bin:$PATH"
PATH="/usr/local/lib/ruby/gems/2.6.0/bin:$PATH"

So I did that, and tried out ruby, irb, and emacs. It starts spitting a bunch of different errors, and I probably confounded the causes and errors as I was trouble shooting, but it gave me errors about:

dyld: Library not loaded: /usr/local/opt/libffi/lib/libffi.6.dylib

Especially when I ran emacs, I got:

dyld: Library not loaded: /usr/local/opt/libffi/lib/libffi.6.dylib
Referenced from: /usr/local/opt/p11-kit/lib/libp11-kit.0.dylib
Reason: image not found

And when I ran irb, I got:

Traceback (most recent call last):
2: from /usr/local/opt/ruby/bin/irb:23:in `<main>'
1: from /usr/local/Cellar/ruby/2.6.2/lib/ruby/2.6.0/rubygems.rb:302:in `activate_bin_path'
/usr/local/Cellar/ruby/2.6.2/lib/ruby/2.6.0/rubygems.rb:283:in `find_spec_for_exe': can't find gem irb (>= 0.a) with executable irb (Gem::GemNotFoundException)

Hmm?  I tried a bunch of stuff and it probably made it more confusing, especially as I started running brew and got errors like:

/System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require': cannot load such file -- active_support/core_ext/object/blank (LoadError)

Or when I ran brew help and got:
 
/System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require': cannot load such file -- mechanize/version (LoadError)

Well now I just wanted to "start over".  You could do that by deleting all of Homebrew and what it installed, to start fresh, but what a pain!  I'd have to know what packages to re-install with a new Homebrew installation and go through installing those one by one.

Instead, the easier thing to try first is this:

1. Remove from your PATH the brew installed ruby and rubygem

On Macs, it's probably "safest" when troubleshooting Homebrew to use the Mac OS X's original Apple installed ruby.  Yes, it's an old version of ruby, but when I changed my PATH to use the Homebrew installed ruby, I probably made troubleshooting much more difficult.  It's tough enough to troubleshoot Homebrew, but if brew (a ruby program) runs on the brew installed ruby, brew just might be running on a ruby that brew screwed up...

2. try brew update-reset

This fetches and resets Homebrew and all tap repositories to the latest from their git repositories.  In other words, restart them fresh.

3. brew reinstall libffi

emacs, ruby, python, and a bunch of other stuff needs it.  Even if you've got it installed.  Reinstall it to make sure it's installed right.  It fixed emacs for me for sure.  Probably ruby too.

4. update and upgrade brew again

brew update
brew upgrade

And that finally fixed it.  I added the ruby and gem paths back into PATH in my .bash_profile, and installed irb as a ruby gem (Mac OS X has old versions of both ruby and irb pre-installed, but getting the latest means installing ruby via brew and irb as a ruby gem).

No comments: