brew upgrade broke Vim on OS X (dyld: Library not loaded)

I've not run brew update && brew upgrade for a long time.

I just did a massive upgrade which turned out to broke my Vim.

This is what I got after running vim:

dyld: Library not loaded: /usr/local/opt/ruby/lib/libruby.2.3.0.dylib
  Referenced from: /usr/local/bin/vim
  Reason: image not found
Trace/BPT trap: 5

I've came across some websites but none of them were actually helpful.

They said that it might be related to rvm but brew uninstall vim; rvm system; brew install vim didn't help. I tried to reinstall my MacVim as well but it didn't solve the problem.

Adding export PATH="/usr/local/bin:$PATH" to my .bash_profile didn't help either.

Here's the list for future reference:

  • How to point MacVim to the right perl on OSX 10.10?
  • vim 7.4.488 build fails if Homebrew ruby is installed but /usr/bin/ruby comes first in PATH
  • brew installed Vim in Terminal with RVM (Ruby 1.9.3), MacVim and Command-T
  • OS X El Capitan: Could not open library 'libc.dylib'

What happened to my Vim? How can I fix it?


Solution

Try to reinstall ruby and vim, e.g.

brew reinstall ruby vim

Troubleshooting

Here are other suggestions if above won't help:

  • Find any references to DYLD_LIBRARY_PATH, DYLD_FALLBACK_LIBRARY_PATH or DYLD_FRAMEWORK_PATH environmental variables and remove them from the startup shell scripts (e.g. ~/.bashrc, ~/.bash_profile, etc.).

tl;dr

brew link --overwrite ruby

Details

Actually, it turned out to be quite simple. I followed the suggestion to run brew doctor and I got these results:

Warning: You have unlinked kegs in your Cellar
Leaving kegs unlinked can lead to build-trouble and cause brews that depend on
those kegs to fail to run properly once built. Run `brew link` on these:
    ruby

So I ran brew link ruby. This is what I got:

Linking /usr/local/Cellar/ruby/2.3.1... v
Error: Could not symlink bin/rake
Target /usr/local/bin/rake
already exists. You may want to remove it:
  rm '/usr/local/bin/rake'

As a result I decided to run brew link --overwrite ruby and now my Vim is working once again!


I ran into a similar error when doing brew upgrade where libperl doesn't get loaded.

dyld: Library not loaded: /usr/local/opt/perl/lib/perl5/5.24.1/darwin-thread-multi-2level/CORE/libperl.dylib
  Referenced from: /usr/local/Cellar/vim/8.0.0636/bin/vim
  Reason: image not found

Fix was to reinstall the broken dependency and vim:

brew uninstall --ignore-dependencies perl
brew uninstall vim
brew install vim

For me, I found that brew somehow installed macvim and it kept conflicting with OS stock vim. Attempts to use regular vim, and running git commands such as these produced errors:

$ git commit --amend
dyld: Library not loaded: /System/Library/Perl/5.16/darwin-thread-multi-2level/CORE/libperl.dylib
  Referenced from: /usr/local/Cellar/macvim/7.4-73/MacVim.app/Contents/MacOS/Vim
  Reason: image not found
error: vi died of signal 6
error: There was a problem with the editor 'vi'.
Please supply the message using either -m or -F option.

So I went ahead and uninstalled any vim-related packages that brew had installed:

$ brew list

And surely removing macvim with $ brew uninstall macvim solved the issue


I tried reinstalling, relinking, removing all available versions etc. for ruby and vim but nothing really helped.

Thus, I just decided to rename my dylib file from

/usr/local/opt/ruby/lib/libruby.2.3.dylib

to

/usr/local/opt/ruby/lib/libruby.2.3.0.dylib

The result of that rename is that it works perfectly fine now.