安装 json gem 时出错“mkmf.rb 找不到 ruby 的头文件”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20559255/
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
Error while installing json gem 'mkmf.rb can't find header files for ruby'
提问by Navneet
For context, it on a remote server which has a firewall. I'm setting up my environment through a proxy. I have ruby 1.8.7. When I try to gem install..
对于上下文,它位于具有防火墙的远程服务器上。我正在通过代理设置我的环境。我有ruby 1.8.7。当我尝试安装 gem 时..
sudo gem install --http-proxy <host address>:<port> json
I get the following error:
我收到以下错误:
Building native extensions. This could take a while...
ERROR: Error installing json:
ERROR: Failed to build gem native extension.
/usr/bin/ruby extconf.rb
mkmf.rb can't find header files for ruby at /usr/lib/ruby/ruby.h
Gem files will remain installed in /usr/lib64/ruby/gems/1.8/gems/json-1.8.1 for inspection.
Results logged to /usr/lib64/ruby/gems/1.8/gems/json-1.8.1/ext/json/ext/generator/gem_make.out
Since I was unsure what the problem is, I googled and found these
由于我不确定问题是什么,我用谷歌搜索并找到了这些
gem install: Failed to build gem native extension (can't find header files)- the instructions here seem to be specific to the gem being installed.
How to install json gem - Failed to build gem native extensionThis seems to be slightly different error.
gem install: 无法构建 gem 本机扩展(找不到头文件)- 这里的说明似乎特定于正在安装的 gem。
如何安装 json gem - 无法构建 gem 本机扩展这似乎是略有不同的错误。
Any hints? Thanks!
任何提示?谢谢!
回答by Aleksei Matiushkin
Modern era update, as stated by mimoralea:
现代更新,如mimoralea 所述:
In case that you are using ruby 2.0 or 2.2 (thanks @patrick-davey).
sudo apt-get install ruby2.0-dev sudo apt-get install ruby2.2-dev sudo apt-get install ruby2.3-dev
如果您使用的是 ruby 2.0 或 2.2(感谢@patrick-davey)。
sudo apt-get install ruby2.0-dev sudo apt-get install ruby2.2-dev sudo apt-get install ruby2.3-dev
or, generic way:
或者,通用方式:
sudo apt-get install ruby-devor
sudo apt-get install ruby`ruby -e 'puts RUBY_VERSION[/\d+\.\d+/]'`-dev
sudo apt-get install ruby-dev或者
sudo apt-get install ruby`ruby -e 'puts RUBY_VERSION[/\d+\.\d+/]'`-dev
The first link you've posted is exactlyyour case: there is no ruby development environment installed. Development env is needed to compile ruby extensions, which are mostly written in C. Proxy has nothing to do with the problem: everything is downloaded fine, just compilation fails.
您发布的第一个链接正是您的情况:没有安装 ruby 开发环境。需要开发环境来编译 ruby 扩展,这些扩展大多是用C. 代理与问题无关:一切都下载正常,只是编译失败。
I would suggest you to install ruby-dev(ruby-develfor rpm-based distros) package onto you target machine.
我建议您将ruby-dev(ruby-devel对于基于 rpm 的发行版)软件包安装到您的目标机器上。
gccpackage might be needed as well.
gcc也可能需要包。
Try:
尝试:
$ sudo apt-get install ruby-dev
Or, for Redhat distro:
或者,对于 Redhat 发行版:
$ sudo yum install ruby-devel
Or, for [open]SuSE:
或者,对于 [open] SuSE:
$ sudo zypper install ruby-devel
回答by joki
For Xcode 11 on macOS 10.14, this can happen even after installing Xcode and installing command-line tools and accepting the license with
对于macOS 10.14 上的 Xcode 11,即使在安装 Xcode 和安装命令行工具并接受许可证后,也会发生这种情况
sudo xcode-select --install
sudo xcodebuild -license accept
The issue is that Xcode 11 ships the macOS 10.15 SDK which includes headers for ruby2.6, but not for macOS 10.14's ruby2.3. You can verify that this is your problem by running
问题是 Xcode 11 提供了 macOS 10.15 SDK,其中包括 ruby2.6 的头文件,但不包括 macOS 10.14 的 ruby2.3。您可以通过运行来验证这是您的问题
ruby -rrbconfig -e 'puts RbConfig::CONFIG["rubyhdrdir"]'
which on macOS 10.14 with Xcode 11 prints the non-existentpath
在 macOS 10.14 和 Xcode 11 上打印不存在的路径
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk/System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/include/ruby-2.3.0
However, Xcode 11 installs a macOS 10.14 SDK within /Library/Developer/CommandLineTools/SDKs/MacOS10.14.sdk. It isn't necessary to pollute the system directories by installing the old header files as suggested in other answers. Instead, by selecting that SDK, the appropriate ruby2.3 headers will be found:
但是,Xcode 11 在/Library/Developer/CommandLineTools/SDKs/MacOS10.14.sdk. 没有必要通过安装其他答案中建议的旧头文件来污染系统目录。相反,通过选择该 SDK,将找到适当的 ruby2.3 标头:
sudo xcode-select --switch /Library/Developer/CommandLineTools
ruby -rrbconfig -e 'puts RbConfig::CONFIG["rubyhdrdir"]'
This should now correctly print
这现在应该正确打印
/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/include/ruby-2.3.0
Likewise, gem installshould work while that SDK is selected.
同样,gem install应在选择该 SDK 时工作。
To switch back to the current Xcode SDK, use
要切换回当前的 Xcode SDK,请使用
sudo xcode-select --switch /Applications/Xcode.app
回答by mimoralea
In case that you are using ruby 2.0 or 2.2 (thanks @patrick-davey) or 2.3 (thanks @juanitofatas).
如果您使用的是 ruby 2.0 或 2.2(感谢@patrick-davey)或 2.3(感谢@juanitofatas)。
sudo apt-get install ruby-dev
sudo apt-get install ruby2.0-dev
sudo apt-get install ruby2.2-dev
sudo apt-get install ruby2.3-dev
And you get the pattern here...
你在这里得到了模式......
回答by Juanito Fatas
I also encountered this problem because I install Ruby on Ubuntu via brightbox, and I thought ruby-dev is the trunk of ruby. So I did not install. Install ruby2.3-dev fixes it:
我也遇到这个问题,因为我在Ubuntu上通过brightbox安装Ruby,我以为ruby-dev是ruby的主干。所以我没有安装。安装 ruby2.3-dev 修复它:
sudo apt-get install ruby2.3-dev
回答by Juan De León
For those who are getting this on Mac OS X you may need to run the following command to install the XCode command-line tools, even if you already have XCode installed:
对于那些在 Mac OS X 上获得此功能的用户,即使您已经安装了 XCode,您也可能需要运行以下命令来安装 XCode 命令行工具:
sudo xcode-select --install
Also you must agree the terms and conditions of XCode by running the following command:
您还必须通过运行以下命令来同意 XCode 的条款和条件:
sudo xcodebuild -license
回答by Miles Davis
Most voted solution didn't work on my machine (linux mint 18.04). After a careful look, i found that g++ was missing. Solved with
大多数投票的解决方案在我的机器上不起作用(linux mint 18.04)。仔细一看,发现g++不见了。解决了
sudo apt-get install g++
sudo apt-get install g++
回答by Kinifwyne
I had a similar problem using cygwin to run the following command:
我在使用 cygwin 运行以下命令时遇到了类似的问题:
$ gem install rerun
I solved it by installing the following cygwin packages:
我通过安装以下 cygwin 包解决了这个问题:
- ruby-devel
- libffi-devel
- gcc-core
- gcc-g++
- make
- automake1.15
- 红宝石开发
- libffi-devel
- gcc核心
- gcc-g++
- 制作
- automake1.15
回答by Prokur
in case you use SUSE
如果您使用 SUSE
sudo yast2 -i ruby-devel
回答by Aaron Brager
Xcode 11 / macOS Catalina
Xcode 11 / macOS Catalina
On Xcode 11 / macOS Catalina, the header files are no longer in the old location and the old /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkgfile is no longer available.
在 Xcode 11 / macOS Catalina 上,头文件不再位于旧位置,旧/Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg文件不再可用。
Instead, the headers are now installed to the /usr/includedirectory of the current SDK path:
相反,标头现在安装到/usr/include当前 SDK 路径的目录中:
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include
Most of this directory can be found by using the output of xcrun --show-sdk-path. And if you add this path to the CPATHenvironment variable, then build scripts (including those called via bundle) will generally be able to find it.
该目录的大部分内容都可以通过使用xcrun --show-sdk-path. 并且如果您将此路径添加到CPATH环境变量中,那么构建脚本(包括那些调用 via 的脚本bundle)通常能够找到它。
I resolved this by setting my CPATHin my .zshrcfile:
我通过CPATH在我的.zshrc文件中设置 my 解决了这个问题:
export CPATH="$(xcrun --show-sdk-path)/usr/include"
After opening a new shell (or running source .zshrc), I no longer receive the error message mkmf.rb can't find header files for ruby at /usr/lib/ruby/ruby.hand the rubygems install properly.
在打开一个新的 shell(或运行source .zshrc)后,我不再收到错误消息mkmf.rb 无法在 /usr/lib/ruby/ruby.h 中找到 ruby 的头文件并且 rubygems 安装正确。
Note on Building to Non-macOS Platforms
If you are building to non-macOS platforms, such as iOS/tvOS/watchOS, this change will attempt to include the macOS SDK in those platforms, causing build errors. To resolve, either don't set
CPATHenvironment variable on login, or temporarily set it to blank when runningxcodebuildlike so:CPATH="" xcodebuild --some-args
构建到非 macOS 平台的注意事项
如果您要构建到非 macOS 平台,例如 iOS/tvOS/watchOS,此更改将尝试在这些平台中包含 macOS SDK,从而导致构建错误。要解决,请不要
CPATH在登录时设置环境变量,或者在运行时暂时将其设置为空白,xcodebuild如下所示:CPATH="" xcodebuild --some-args
回答by emmojo
In Fedora 21 and up, you simply open a terminal and install the Ruby Development files as root.
在 Fedora 21 及更高版本中,您只需打开一个终端并以 root 身份安装 Ruby Development 文件。
dnf install ruby-devel

