Rails - ERROR: Error installing rails: ERROR: Failed to build gem native extension

Rails - ERROR: Error installing rails: ERROR: Failed to build gem native extension

我的机器使用 OSX El Capitan。我已经按照说明一一进行了。它工作正常,直到我需要安装 rails,发生了一些不希望发生的事情。顺便说一句,我已经安装了 Xcode 命令行工具,但即使我重新启动终端和机器仍然无法修复。我收到此错误消息。

gem install rails -v 4.2.4

错误信息

Last login: Sat Nov  7 15:07:53 on ttys000
Mohammads-MacBook-Air% gem install rails -v 4.2.4
Ignoring curb-0.8.8 because its extensions are not built.  Try: gem pristine curb --version 0.8.8
Building native extensions.  This could take a while...
ERROR:  Error installing rails:
    ERROR: Failed to build gem native extension.

    /Users/ZERO/.rvm/rubies/ruby-2.2.1/bin/ruby -r ./siteconf20151107-786-tg78i0.rb extconf.rb
checking if the C compiler accepts ... yes
checking if the C compiler accepts -Wno-error=unused-command-line-argument-hard-error-in-future... no
Building nokogiri using packaged libraries.
Ignoring curb-0.8.8 because its extensions are not built.  Try: gem pristine curb --version 0.8.8
-----
The file "/usr/include/iconv.h" is missing in your build environment,
which means you haven't installed Xcode Command Line Tools properly.

To install Command Line Tools, try running `xcode-select --install` on
terminal and follow the instructions.  If it fails, open Xcode.app,
select from the menu "Xcode" - "Open Developer Tool" - "More Developer
Tools" to open the developer site, download the installer for your OS
version and run it.
-----
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers.  Check the mkmf.log file for more details.  You may
need configuration options.

Provided configuration options:
    --with-opt-dir
    --without-opt-dir
    --with-opt-include
    --without-opt-include=${opt-dir}/include
    --with-opt-lib
    --without-opt-lib=${opt-dir}/lib
    --with-make-prog
    --without-make-prog
    --srcdir=.
    --curdir
    --ruby=/Users/ZERO/.rvm/rubies/ruby-2.2.1/bin/$(RUBY_BASE_NAME)
    --help
    --clean
    --use-system-libraries
    --enable-static
    --disable-static
    --with-zlib-dir
    --without-zlib-dir
    --with-zlib-include
    --without-zlib-include=${zlib-dir}/include
    --with-zlib-lib
    --without-zlib-lib=${zlib-dir}/lib
    --enable-cross-build
    --disable-cross-build

extconf failed, exit code 1

Gem files will remain installed in /Users/ZERO/.rvm/gems/ruby-2.2.1/gems/nokogiri-1.6.6.2 for inspection.
Results logged to /Users/ZERO/.rvm/gems/ruby-2.2.1/extensions/x86_64-darwin-14/2.2.0-static/nokogiri-1.6.6.2/gem_make.out
Mohammads-MacBook-Air% 

参考:https://gorails.com/setup/osx/10.11-el-capitan

现在可以了。使用 rvm 对其进行处理。

rvm install ruby-2.1.1
rvm 2.1.1
gem install rails -v 4.1.0

错误信息告诉你:

The file "/usr/include/iconv.h" is missing in your build environment, which means you haven't installed Xcode Command Line Tools properly.

To install Command Line Tools, try running xcode-select --install on terminal and follow the instructions. If it fails, open Xcode.app, select from the menu "Xcode" - "Open Developer Tool" - "More Developer Tools" to open the developer site, download the installer for your OS version and run it.

不幸的是,此错误消息有时会产生误导。该消息真正试图告诉您的是它只是无法使用命令行工具。这并不一定意味着它们没有安装 - 这可能意味着您还没有接受许可协议。

我注意到这是安装 Xcode 后的一个常见问题,并且在更新到 Xcode 的较新版本后再次出现。我建议在每次安装或升级 Xcode:

后执行以下步骤
  • 打开Xcode并接受许可协议
  • 尝试重新安装命令行工具

第二步似乎取决于您的 OS 版本。如果 xcode-select --install 不起作用,请尝试在 /System/Library/CoreServices.

中开始安装 Command Line Developer Tools 应用程序

重新安装命令行工具后尝试重新运行 bundle install。

打开 Xcode 并接受许可协议为我修复了这个错误。之后,能否运行sudo gem install rails成功。