Ruby-on-rails OS-X,Rails:“无法构建 gem 本机扩展”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8389301/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 02:26:40  来源:igfitidea点击:

OS-X, Rails: "Failed to build gem native extension"

ruby-on-railsrubymacosgem

提问by user884913

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

我一直在尝试在我的 mac 上安装 rails。我有 OS X 10.6.8 并且我已经确认我有 Ruby 版本 1.8.7

I ran sudo gem updateand sudo gem update --systemto get the latest versions of the software.

我运行sudo gem updatesudo gem update --system获取该软件的最新版本。

However, when I run sudo gem install railsI get this error:

但是,当我运行时出现sudo gem install rails此错误:

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

回答by Rob

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.

如果您有 XCode 4 或更高版本,则需要打开它并转到首选项 -> 下载 -> 组件并安装命令行工具,因为默认情况下未安装它们。在发生这种情况之前无法安装 Rails。

回答by longJOURNEY

Im using osx 10.10. You can download from the command-line

我使用的是 osx 10.10。您可以从命令行下载

xcode-select --install

回答by Alex Wayne

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

不确定它需要编译什么,但除非安装了 Apple 开发人员工具,否则 OSX 根本无法编译任何原生 ruby​​ 扩展。在 10.7 Lion 上,您可以从应用商店免费下载,或在此处下载 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.

它也可能在您的 OSX 安装光盘上,但可能已经过时了。

回答by Jimmy MG Lim

Just a follow up ...

只是跟进...

it may be that you are on a mac and rails cannot find the right compiler for c headers.

可能是您使用的是 mac,而 rails 找不到适合 c 头文件的编译器。

just install xcode from apps store / homebrew or go to terminal ...

只需从应用商店/自制软件安装 xcode 或转到终端...

$ xcode-select --install

$ xcode-select --install

complete the installation and agree on the licensing etc, then ...

完成安装并同意许可等,然后...

$ sudo gem install rails

$ sudo gem 安装导轨

回答by RyanWilcox

Did you install the OS X developer tools? You'll need to do this to be able to build native extensions

您是否安装了 OS X 开发人员工具?您需要这样做才能构建本机扩展

回答by Mohit Chawla

There are two possible reasons for the fail:

失败有两种可能的原因:

  1. PRIMARY REASON: Missing Xcode Command Line Tools
  1. 主要原因:缺少 Xcode 命令行工具

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 --installon 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.

手动验证 Xcode 命令行工具安装:检查是否存在“/usr/include/iconv.h”(如果不存在=> 缺少或未正确安装 Xcode CLT)

安装 Xcode CLT:尝试xcode-select --install在终端上运行并按照说明进行操作。如果失败,打开Xcode.app,从菜单“Xcode”-“打开开发者工具”-“更多开发者工具”中选择打开开发者站点,下载适用于您操作系统版本的安装程序并运行它。

  1. SECONDARY REASON(if 1. fails): Version issues Try upgrading the ruby version using rbenv.
  1. 次要原因(如果 1. 失败):版本问题 尝试使用 rbenv 升级 ruby​​ 版本。

Hope it helps!

希望能帮助到你!

回答by sparkle

Try to install Ruby via RVM. I solved in this way

尝试通过 RVM 安装 Ruby。我是这样解决的

How to install ruby on Ubuntu with rvm

如何使用 rvm 在 Ubuntu 上安装 ruby

回答by tzharg

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.

命令行工具没有为我解决这个问题。我通过 rbenv 将 ruby​​ 安装升级到 2.2.0,将其设为全局默认 ruby​​ 安装,此问题已修复。

回答by Jaspreet Singh Lidder

Just had a similar issue. I can confirm that installing the command line tools fixes it.

刚刚有一个类似的问题。我可以确认安装命令行工具可以修复它。

回答by Christopher

Switch Ruby to Homebrew version:

将 Ruby 切换到 Homebrew 版本:

$ brew install ruby
$ brew link --overwrite ruby

$ echo 'export PATH="/usr/local/opt/ruby/bin:$PATH"' >> ~/.bash_profile
$ echo 'export LDFLAGS="-L/usr/local/opt/ruby/lib"' >> ~/.bash_profile
$ echo 'export CPPFLAGS="-I/usr/local/opt/ruby/include"' >> ~/.bash_profile