28

gem install 在 MacOs Mojave 中失败。有什么可以帮助我解决这个问题的吗?我的红宝石版本是ruby 2.3.7p456.

➜ sudo gem install json -v '1.8.3'



current directory: /Library/Ruby/Gems/2.3.0/gems/json-1.8.3/ext/json/ext/generator
make "DESTDIR="
compiling generator.c
In file included from generator.c:1:
In file included from ./../fbuffer/fbuffer.h:5:
In file included from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk/System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/include/ruby-2.3.0/ruby.h:33:
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk/System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/include/ruby-2.3.0/ruby/ruby.h:24:10: fatal error: 'ruby/config.h' file not found
#include "ruby/config.h"
         ^~~~~~~~~~~~~~~
1 error generated.
make: *** [generator.o] Error 1

make failed, exit code 2
4

8 回答 8

48

2021 年 15 月 3 日更新

根据 Apple的说法,Ruby 将不再包含在系统的未来版本中,因此一旦这种情况真正发生,使用 Ruby 版本管理器将是唯一的方法。


原始答案

经过数小时的反复试验,我想更深入地了解这个错误的原因,我终于发现这是我认为解决这个问题的干净方法:

Catalina(NdMe:我认为这开始得更早,在 Mojave 上)不再支持在命令行工具中包含 macOS 标头,它们必须通过 Xcode SDK 显式使用。您可以通过运行gem以下xcrun工具来执行此操作:

xcrun gem install <your_gem>

引用自Github 问题评论

于 2020-12-28T15:17:50.517 回答
15

Xcode 12 升级让我搞砸了。

我安装并添加rubybrew我的 .zshrc

export PATH="/usr/local/opt/ruby/bin:$PATH"
export LDFLAGS="-L/usr/local/opt/ruby/lib"
export CPPFLAGS="-I/usr/local/opt/ruby/include"
export PKG_CONFIG_PATH="/usr/local/opt/ruby/lib/pkgconfig"
于 2020-12-01T17:37:27.590 回答
12

如果您正在运行 Xcode 10 测试版,这可能会修复它

sudo xcode-select -s /Applications/Xcode-beta.app/Contents/Developer
于 2018-07-10T14:12:25.330 回答
6

我在尝试使用自制软件安装 fastlane 时遇到了这个问题。前面提到的任何建议都没有帮助我。手动安装macOS_SDK_headers_for_macOS_10.14.pkg固定它。

sudo installer -pkg /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg -target /
于 2019-02-14T13:44:39.037 回答
4

在 macOS Monterey 中(至少在 Beta 版中),ruby 2.6.3p62 (2019-04-16 revision 67580)仍然包含 Ruby ( ),但它似乎不能很好地工作 ( fatal error: 'ruby/config.h' file not found),所以我安装了我自己的 Ruby 版本。您可以手动安装 ruby​​,但我更喜欢使用rbenvRuby 版本管理器。


使用安装红宝石rbenv

  • 安装 rbenv: brew install rbenv ruby-build
    • ruby-build是一个用于rbenv实际安装 ruby​​ 版本的插件,它提供ruby install命令。
  • 设置 rbenv: rbenv init , (并按照其推荐的说明进行操作,例如,如果您使用的是 zsh,它会显示 add eval "$(rbenv init -)"to ~/.zshrc
  • 安装红宝石: rbenv install 3.0.1 . 此处列出的最新版本
  • 做你想做的事: sudo gem install cocoapodsbundle install

或者手动安装 ruby ​​:

  • brew install ruby
  • 运行 echo 'export PATH="/opt/homebrew/opt/ruby/bin:$PATH"' >> ~/.zshrc以便使用此版本
  • 重启终端

asdf?

的替代方法rbenvasdf,但我发现asdf使用起来有点复杂,因为它支持的不仅仅是 ruby​​,NodeJS。我会争辩说,它们都不符合人体工程学。


注意:我有一台 M1 mac,它仍在工作。

于 2021-06-24T15:35:58.103 回答
2

我遇到了同样的问题,由于包含“ruby/config.h”时失败,我无法安装本机扩展。

我相信这个问题的原因是因为我使用的是随 Homebrew 安装的 ruby​​ 版本。在使用 Homebrew 升级到新版本的 Ruby 后,我看到了来自 Homebrew 的以下有用信息:

ruby is keg-only, which means it was not symlinked into /usr/local,
because macOS already provides this software and installing another version in
parallel can cause all kinds of trouble.

If you need to have ruby first in your PATH run:
 echo 'export PATH="/usr/local/opt/ruby/bin:$PATH"' >> ~/.bash_profile

For compilers to find ruby you may need to set:
 export LDFLAGS="-L/usr/local/opt/ruby/lib"
 export CPPFLAGS="-I/usr/local/opt/ruby/include"

For pkg-config to find ruby you may need to set:
 export PKG_CONFIG_PATH="/usr/local/opt/ruby/lib/pkgconfig"

将以下行添加到我的 ~/.bash_profile 为我解决了这个问题:

export LDFLAGS="-L/usr/local/opt/ruby/lib"
export CPPFLAGS="-I/usr/local/opt/ruby/include"
于 2018-12-27T16:05:11.627 回答
2

xcrun gem install <your_gem>为我工作,但我不得不多次这样做——每次都bundle install失败了。

最好的解决方案可能是设置与文件中相同的xcrun变量.zshrcxcrun env在我的系统上添加:

SDKROOT=/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk
CPATH=/usr/local/include
LIBRARY_PATH=/usr/local/lib
于 2021-01-26T01:26:08.207 回答
0

安装 Big Sur 后我遇到了这个问题。我发现在升级之前安装的 Xcode 版本的 Xcode 包中不存在 ruby​​/config.h 文件。我能够通过卸载 Xcode 版本并重新安装它们来纠正它。

于 2021-02-11T23:29:41.820 回答