34

I'm getting this error when I try to run any brew command.

Holger-Sindbaeks-MacBook-Air:~ holgersindbaek$ brew help
-bash: /usr/local/bin/brew: /usr/bin/ruby: bad interpreter: No such file or directory

I have absolutely no idea on how to fix this and been searching for a long time without answer.

Prof. Falken
  • 24,226
  • 19
  • 100
  • 173
Holger Sindbaek
  • 2,278
  • 6
  • 41
  • 68

12 Answers12

65

I got this error (much the same):

/usr/local/bin/brew: /usr/local/Library/brew.rb: /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby: bad interpreter: No such file or directory /usr/local/bin/brew: line 26: /usr/local/Library/brew.rb: Undefined error: 0

and fixed by the solution below:

  1. Open brew.rb:

    $ sudo vim /usr/local/Library/brew.rb
    
  2. Change the first line's 1.8 to Current:

    Before:

    #!/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby -W0
    

    After:

    #!/System/Library/Frameworks/Ruby.framework/Versions/Current/usr/bin/ruby -W0
    

Then brew works for me. Hope it helps if any other one got this issue. :)


If you get the error

Homebrew requires Leopard or higher. For Tiger support, see: https://github.com/mistydemeo/tigerbrew

change the MACOS check from <10.5 to <10.

Tip by @TimCastelijns:

10.5 doesn't work because in comparison, it's higher than 10.10 (.1 vs .5). I added a check (and MACOS_VERSION != 10.10) instead of lowering from 10.5 to 10.

Tim
  • 41,901
  • 18
  • 127
  • 145
Kjuly
  • 34,476
  • 22
  • 104
  • 118
  • 7
    Great! Fixed my problem after updating to Yosemite. – Sebastian Wramba Oct 19 '14 at 13:08
  • when I make the changes you suggest, I get the error Homebrew requires Leopard or higher. For Tiger support, see: https://github.com/mistydemeo/tigerbrew – scientiffic Oct 21 '14 at 14:12
  • 2
    I'm getting the same result as @scientiffic whilst I'm running Yosemite – Tim Reynaert Oct 27 '14 at 14:49
  • @TimReynaert same result? his error is "requires Leopard or higher", if ur system is Yosemite, how come this error happen? :S – Kjuly Oct 28 '14 at 00:35
  • 1
    There is a comment on this page that talks about changing the /usr/local/Library/brew.rb file to: a) update the version of ruby as mentioned in the answer above b) change the MACOS check to use 10 and not 10.5 (looks like 10.10 is causing problems) I did that, did a brew update, and all seems to be well now. – danmiser Dec 02 '14 at 16:34
  • 1
    10.5 doesn't work because in comparison, it's higher than 10.10 (.1 vs .5). I added a check (`and MACOS_VERSION != 10.10`) instead of lowering from 10.5 to 10 – Tim Jul 13 '15 at 09:39
  • @TimCastelijns it's interesting, i've updated the answer to append ur tip , thx :) – Kjuly Jul 13 '15 at 13:03
  • 1
    Still is true in 2018 when installing fastlane with homebrew – brainray Jul 15 '18 at 20:09
46

What you are getting means that Homebrew has not been able to locate the Ruby interpretter at the specified location.

Install Apple Developer Kit (comes with Xcode) which should be available to you as an optional install (or you can simply download it from Apple). This will install the Ruby interpreter for you.

In case you already have Xcode installed, this means that one of these things is happening:

  1. You have a broken Ruby installation
  2. You have more than one Ruby installation
  3. Your installation has not been configured properly.

To identify if this is the first case, you can run ruby and see if you get any response.

If you don't, your installation is broken and you need to reinstall it. If you do, you then run which ruby. This should give you the absolute path to your Ruby executable. If this is anything other than /usr/bin/ruby then homebrew (and a bunch of other programs) will not be able to find it.

In case you have not ever tampered with your Ruby installation, you can check to see if /usr/bin/ruby already exists or not: cat /usr/bin/ruby. If you get No such file or directory, then you can easily create a symbolic link to your Ruby installation. Assuming the output of which ruby to by /usr/local/bin/ruby, you create the symbolic link this way: sudo ln -s /usr/local/bin/ruby /usr/bin/ruby and all should be well.

If there is a file at that location, you can run file /usr/bin/ruby to see if it's a real file, a symbolic link, or a corrupted file. If it is a symbolic link, your installation should be working, and since it's not, it probably is either a corrupted symlink or it's a bogus file.

You can remedy that by first deleting it (sudo rm /usr/bin/ruby) and then creating a new symlink to the correct location (sudo ln -s /usr/local/bin/ruby /usr/bin/ruby).

If non of the above works, you should consult the homebrew team after a clean install of Xcode and removing any traces of a Ruby installation on your system.

EDIT

Alternatively, as pointed out by the other answers, the issue might be because of a bad ruby version in your Homebrew settings.

A quick fix might be updating your Homebrew:

cd /usr/local
git pull -q origin refs/heads/master:refs/remotes/origin/master

If this does not help, you might want to get your hands dirty and manually fix the problem by:

  1. Editing brew.rb from /user/local/Library/brew.rb
  2. Changing /1.8/ to /Current/ in the first line, which will cause the hashbang to point to the current Ruby version as the executor

If this does not help, either, you can also modify the MACOS check and change it from 10.5 to 10 to avoid the infamous "Homebrew requires Leopard or higher" error.

DISCLAIMER

A bunch of thanks to other contributors in the answers below and their commenters. I am not committing plagiarism, simply aggregating the answers into one integrated article to help others.

Milad Naseri
  • 4,053
  • 1
  • 27
  • 39
  • But I already have XCode installed, so that shouldn't be a problem. – Holger Sindbaek Jan 14 '12 at 19:25
  • 1
    Run `whereis ruby` and see if you get `/usr/bin/ruby`. – Milad Naseri Jan 14 '12 at 19:26
  • 1
    I can't run whereis ruby, then it doesn't do anything. I can run which ruby and then it gives me "/usr/local/bin/ruby". – Holger Sindbaek Jan 14 '12 at 19:29
  • Okay then. Homebrew is looking for it at `/usr/bin/ruby` while it actually is at `/usr/local/bin/ruby`. All you have to do is put a symlink to it at `/usr/bin`. Do this: `sudo ln -s /usr/local/bin/ruby /usr/bin/ruby` – Milad Naseri Jan 14 '12 at 19:31
  • Just tried it and it tells me that "ln: /usr/bin/ruby: File exists". When I try to do brew help after that it gives me the original error. – Holger Sindbaek Jan 14 '12 at 19:55
  • There might be a conflict of Ruby versions on your systems. Try the instructions [here](http://linguisticlogic.wordpress.com/2010/11/27/homebrew-mac-osx-and-ruby/) – Milad Naseri Jan 14 '12 at 19:57
  • That's the guide i followed to get brew installed in the first place. – Holger Sindbaek Jan 14 '12 at 20:01
  • So you have Ruby, but the system-wide ruby recognized by the PATH attribute is not the one installed in your `/usr/bin/`. I think you can change that by editing the place where Homebrew looks up for the Ruby installation. – Milad Naseri Jan 14 '12 at 20:22
  • Sounds like a great idea. How do I do that? – Holger Sindbaek Jan 14 '12 at 20:37
  • I'm currently looking into it. – Milad Naseri Jan 14 '12 at 20:40
  • Can you do me one favor, and run these two commands and give me the results: `/usr/bin/ruby -v` and `/usr/local/bin/ruby -v` – Milad Naseri Jan 14 '12 at 20:53
  • And also, have you tried [RVM on Mac](http://www.frederico-araujo.com/2011/07/30/installing-rails-on-os-x-lion-with-homebrew-rvm-and-mysql/)? – Milad Naseri Jan 14 '12 at 20:55
  • With /usr/bin/ruby -v it says: -bash: /usr/bin/ruby: No such file or directory With /usr/local/bin/ruby -v it says: ruby 1.9.2p290 (2011-07-09 revision 32553) [x86_64-darwin10.8.0] I already have RVM... does that make a difference? – Holger Sindbaek Jan 14 '12 at 20:59
  • This means that there is NO Ruby installation at `/usr/bin`, but there is a file system pointer there. Could you tell me if `file /usr/bin/ruby` returns the architecture for this binary file? – Milad Naseri Jan 14 '12 at 21:02
  • /usr/bin/ruby: broken symbolic link to /opt/local/bin/ruby returns: /usr/bin/ruby: broken symbolic link to /opt/local/bin/ruby – Holger Sindbaek Jan 14 '12 at 21:04
  • 1
    Okay. As I suspected, you have followed those instructions without adapting them to your own situation. Run `sudo rm -f /usr/bin/ruby` ro get rid of the broken link, and then run `sudo ln -s /usr/local/bin/ruby /usr/bin/ruby` to make the proper link to your installation of Ruby. – Milad Naseri Jan 14 '12 at 21:05
  • You are welcome. :) I now have to edit the answer to incorporate the comments into it. Thanks for being responsive and not leaving your question off for a week. I hope you can someday find the time to come back here and help out other users. – Milad Naseri Jan 14 '12 at 21:11
  • Okay, just edited the answer. Please see if it describes the solution correctly. – Milad Naseri Jan 14 '12 at 22:34
  • That described the problem and solution very well. I'll do my best to contribute, but I'm still very new to RoR and I really think it's got a steep learning curve. – Holger Sindbaek Jan 15 '12 at 18:54
  • Don't worry, you'll get on pretty quickly, once you get the hang of it. – Milad Naseri Jan 15 '12 at 18:55
  • Thanks for the confidence :-). – Holger Sindbaek Jan 15 '12 at 19:00
  • Sorry for the downvote but I think the answer should be more readable/concise, given how simple the answer below is. On my system it worked and I don't consider my Ruby installation broken, it has just been updated. (1., 2., 3. might therefore be confusing) – Philip Dec 19 '14 at 11:11
  • @Philip I don't think that many people come back and update the answer to the needs of everyone. My answer and procedure worked perfectly for the asking user, and you can follow that through the comments. I have even gone beyond that and incorporated all the other answers into my own, while adding disclaimers. I do consider you down vote a misconduct. – Milad Naseri Dec 19 '14 at 14:32
30

Fix:

sudo gem install cocoapods
Shobhit C
  • 828
  • 10
  • 15
9

At the risk of oversimplifying things, try running

gem install bundler

I was transitioning my Ruby environment from RBENV to RVM and it worked for me.

Nick Sarafa
  • 986
  • 10
  • 16
7

This happened because I needed to update brew - in the updated version it already uses Current ruby

cd /usr/local
git pull -q origin refs/heads/master:refs/remotes/origin/master

This solved the problem

mcfedr
  • 7,845
  • 3
  • 31
  • 27
6

You need to change the path for Ruby.Framework

I solved it with commands as mentioned.

  1. brew install cocoapods --build-from-source

  2. brew link --overwrite cocoapods

If you have a lower version below Xcode 11, you have to remove it before you use the above commands.

Reference: Ruby Framework issue

0

None of the above worked for me, so I kept browsing and found this answer, https://stackoverflow.com/a/24225960/1359088 which did fix brew for me. He says in step 1 to install XCode 6 command line tools, but doesn't say how; use this command:

xcode-select --install
Community
  • 1
  • 1
James Toomey
  • 5,635
  • 3
  • 37
  • 41
0

I got the same issue when updated to MacOSX High Sierra & using Xcode 9 with that. High Sierra update ruby gem to version 2.3 but xcpreety command of Xcode 9 still using Ruby 2.0 which is unable to find now & gives bad interpreter. Just go to Terminal & run

sudo gem install xcpretty

Restart Xcode & do fresh clean build it works for me. Hope it helps!!!

ndmeiri
  • 4,979
  • 12
  • 37
  • 45
Piyush
  • 119
  • 7
0

After upgrading to macOS High Sierra, get it fixed with following commands:

sudo gem install cocoapods

0

In my case seems like fastlane installed incorrectly with brew install fastlane system didn't write correct path to fastlane. I fixed it with alias fastlane=~/.fastlane/bin/fastlane

Serg Smyk
  • 613
  • 5
  • 11
0

I solved it with commands as mentioned.

1.) Uninstall your GEM.

gem unistall GEM

2.) Then Install your GEM.

sudo gem install GEM -n /usr/local/bin

Dipendra Sharma
  • 2,404
  • 2
  • 18
  • 35
0

I got bad interpreter: No such file or directory error when used xcpretty and xcpretty-travis-formatter on upgraded MacOS.

To solve it

gem install xcpretty
gem install xcpretty-travis-formatter

That is why I can recommend you to reinstall failed component gem install <name>

#For example error looks like
/usr/local/bin/xcpretty-travis-formatter: /System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/bin/ruby: bad interpreter: No such file or directory

#use
gem install xcpretty-travis-formatter
yoAlex5
  • 29,217
  • 8
  • 193
  • 205