问题
I'm stuck trying to install rails on my mac. I have OS X 10.6.8 and I have confirmed that I have Ruby, version 1.8.7
I ran sudo gem update
and sudo gem update --system
to get the latest versions of the software.
However, when I run sudo gem install rails
I get this error:
ERROR: Error installing rails:
ERROR: Failed to build gem native extension.
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby extconf.rb
mkmf.rb can't find header files for ruby at /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/ruby.h
Gem files will remain installed in /Library/Ruby/Gems/1.8/gems/json-1.6.3 for inspection.
Results logged to /Library/Ruby/Gems/1.8/gems/json-1.6.3/ext/json/ext/parser/gem_make.out
回答1:
If you have XCode 4 or later you will need to open it and go to Preferences -> Downloads -> Components and install the Command Line tools as they aren't installed by default. Couldn't install Rails until this happened.
回答2:
Im using osx 10.10. You can download from the command-line
xcode-select --install
回答3:
Not sure what it needs to compile, but OSX can't compile any native ruby extensions at all unless the Apple developer tools are installed. On 10.7 Lion you can download it free from the app store, or Download it here for 10.6: http://developer.apple.com/xcode/index.php
It may also be on your OSX install discs, though probably much more out of date.
回答4:
Just a follow up ...
it may be that you are on a mac and rails cannot find the right compiler for c headers.
just install xcode from apps store / homebrew or go to terminal ...
$ xcode-select --install
complete the installation and agree on the licensing etc, then ...
$ sudo gem install rails
回答5:
Did you install the OS X developer tools? You'll need to do this to be able to build native extensions
回答6:
There are two possible reasons for the fail:
- PRIMARY REASON: Missing Xcode Command Line Tools
Verifying Xcode Command Line Tools Installation manually:
Check for presence of
"/usr/include/iconv.h" (if absent=>Missing or improperly installed Xcode CLT)
Installing Xcode CLT:
Try running xcode-select --install
on terminal and follow the instructions. If it fails, open Xcode.app, select from menu "Xcode" - "Open Developer Tool" - "More Developer Tools" to open the developer site, download the installer for your OS version and run it.
- SECONDARY REASON(if 1. fails): Version issues Try upgrading the ruby version using rbenv.
Hope it helps!
回答7:
Try to install Ruby via RVM. I solved in this way
How to install ruby on Ubuntu with rvm
回答8:
The Command line tools didn't solve this issue for me. I upgraded ruby installation through rbenv to 2.2.0, made that the global default ruby installation, and this issue was fixed.
回答9:
Just had a similar issue. I can confirm that installing the command line tools fixes it.
来源:https://stackoverflow.com/questions/8389301/os-x-rails-failed-to-build-gem-native-extension