如何使用 mkmf.log 说找不到 libiconv 安装 Nokogiri Ruby gem?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12336160/
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-06 05:24:56  来源:igfitidea点击:

How to install Nokogiri Ruby gem with mkmf.log saying libiconv not found?

rubygeminstallnokogiri

提问by joelparkerhenderson

I'm installing the Ruby Nokogiri gem and finding the error below.

我正在安装 Ruby Nokogiri gem 并发现以下错误。

How to diagnose this and solve it?

如何诊断并解决它?

# gem install nokogiri
Building native extensions.  This could take a while...
ERROR:  Error installing nokogiri:
ERROR: Failed to build gem native extension.
...
/opt/ruby/1.9.3-p194/bin/ruby extconf.rb
checking for libxml/parser.h... *** 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.
...
/opt/ruby/1.9.3-p194/lib/ruby/1.9.1/mkmf.rb:381:in `try_do': 
The compiler failed to generate an executable file. (RuntimeError)
You have to install development tools first.
from /opt/ruby/1.9.3-p194/lib/ruby/1.9.1/mkmf.rb:506:in `try_cpp'
...

回答by joelparkerhenderson

To diagnose and solve, here's what worked for me.

为了诊断和解决,这对我有用。

To find out what failed, go to your ruby gems directory.

要找出失败的原因,请转到您的 ruby​​ gems 目录。

For example:

例如:

$ cd <MY RUBY DIRECTORY>/lib/ruby/gems/2.0.0/gems

If you don't know your gem directory, try this:

如果你不知道你的 gem 目录,试试这个:

$ echo $GEM_HOME
/opt/gems/2.0.0
$ cd /opt/gems/2.0.0/gems

What version of nokogiri am I installing?

我要安装什么版本的 nokogiri?

$ ls -ladg nokogiri-*
nokogiri-1.5.5

Go to the installer directory:

进入安装目录:

$ cd nokogiri-1.5.5/ext/nokogiri

Try installing manually:

尝试手动安装:

$ ruby extconf.rb

Result:

结果:

checking for libxml/parser.h... *** extconf.rb failed ***
...

I'm using Ubuntu so I search for any similar packages:

我正在使用 Ubuntu,所以我搜索任何类似的包:

$ aptitude search libxml

Results:

结果:

p libxml2 - GNOME XML library
p libxml2-dev - Development files for the GNOME XML library
...

I believe that libxml2 will work fine.

我相信 libxml2 会正常工作。

$ apt-get install libxml2

Ruby native gems often need the *-dev packages for headers so install them:

Ruby 原生 gems 通常需要 *-dev 包作为头文件,所以安装它们:

$ apt-get install libxml2-dev

Now do I have the parser file?

现在我有解析器文件吗?

$ find / | grep libxml/parser.h

Yes, the result shows the parser:

是的,结果显示解析器:

/usr/include/libxml2/libxml/parser.h

Now try installing again, this time providing the libxml2 path:

现在再次尝试安装,这次提供 libxml2 路径:

$ gem install nokogiri -- --with-xml2-dir=/usr/include/libxml2

It still fails, so read the mkmf error log:

它仍然失败,因此请阅读 mkmf 错误日志:

$ more mkmf.log 

The error log shows what failed and has these lines that look promising:

错误日志显示了失败的内容,并且这些行看起来很有希望:

package configuration for libxslt is not found

Is there a package for it?

有它的包吗?

$ aptitude search libxslt

Results:

结果:

v   libxslt-dev
i   libxslt-ruby
...

Install the dev package:

安装开发包:

$ apt-get install libxslt-dev

Now try installing again, and also put xslt on the path:

现在再次尝试安装,并将 xslt 放在路径上:

$ gem install nokogiri -- \
--with-xml2-dir=/usr/include/libxml2 \
--with-xslt-dir=/usr/include/libxslt

Success!

成功!

回答by notapatch

Installing Nokogiri Website

安装 Nokogiri 网站

'Installing Nokogiri' is a website dedicated to installing Nokogiri on the major platforms - Here is an excerpt about Installing Nokogiri on Ubuntu:

“安装 Nokogiri”是一个专门用于在主要平台上安装 Nokogiri 的网站 - 以下是关于在 Ubuntu 上安装 Nokogiri 的摘录:

Because Nokogiri needs to be compiled and dynamically linked against both libxml2 and libxslt, it has gained a reputation for being complicated to install.

As of Nokogiri 1.6, libxml2 and libxslt source code is bundled with Nokogiri, and compiled at gem-install-time. The instructions in this document should work for all versions 1.6.4 and later.

Ubuntu / Debian

Installation should Just Work? on Ubuntu and Debian using Nokogiri's vendored libxml2 and libxslt:

gem install nokogiri

[...]

Using Your System Libraries

If, instead of Nokogiri's vendored libraries, you'd like to use your system's libxml2, libxslt and related libraries, please first understand that you may be asking Nokogiri to work with an unsupported version of libxml2.

sudo apt-get install pkg-config
gem install nokogiri -- --use-system-libraries

由于 Nokogiri 需要针对 libxml2 和 libxslt 进行编译和动态链接,因此它以安装复杂而闻名。

从 Nokogiri 1.6 开始,libxml2 和 libxslt 源代码与 Nokogiri 捆绑在一起,并在 gem-install-time 编译。本文档中的说明适用于所有 1.6.4 及更高版本。

Ubuntu / Debian

安装应该可以正常工作吗?在 Ubuntu 和 Debian 上使用 Nokogiri 的供应商 libxml2 和 libxslt:

gem install nokogiri

[...]

使用您的系统库

如果您想使用系统的 libxml2、libxslt 和相关库而不是 Nokogiri 的供应商库,请首先了解您可能要求 Nokogiri 使用不受支持的 libxml2 版本。

sudo apt-get install pkg-config
gem install nokogiri -- --use-system-libraries


FYI - I am using Nokogiri 1.6.6.2 and it didn't 'just work'. I got it going with the --use-system-libraries.

仅供参考 - 我正在使用 Nokogiri 1.6.6.2 并且它并没有“正常工作”。我明白了--use-system-libraries



Mac OS X

Mac OS X

The website's advice also covers OS X - this worked for me:
gem update --system xcode-select --install gem install nokogiri

该网站的建议还涵盖 OS X - 这对我有用:
gem update --system xcode-select --install gem install nokogiri



Conclusion

结论

If you have a Nokogiri problem on any platform you should check out the website.

如果您在任何平台上遇到 Nokogiri 问题,您应该查看该网站

回答by DaShaun

On CentOS here is what I needed to do:

在 CentOS 上,这是我需要做的:

gem update --system
yum install libxml2-devel libxslt-devel ruby-devel
gem install nokogiri -- --use-system-libraries

回答by kenorb

Could not create Makefile due to some reason, probably lack of necessary libraries and/or headers.

由于某种原因无法创建 Makefile,可能缺少必要的库和/或头文件

You are probably missing zlib headers which are required for -lzflag in order to compile the sources correctly. Install on Linux by:

您可能缺少-lz标志所需的 zlib 头文件,以便正确编译源代码。在 Linux 上安装:

sudo apt-get install libz-dev

For missing libiconv, try installing libiconv-hook-devpackage which has header files of libiconv-hook, e.g.

对于缺少libiconv的,尝试安装libiconv-hook-dev包,它具有的头文件libiconv-hook,如

sudo apt-get install libiconv-hook1 libiconv-hook-dev


On OS X, try installing development tools via: xcode-select --install.

在 OS X 上,尝试通过以下方式安装开发工具:xcode-select --install

If there is still problem, check mkmf.logfile for more specific details about your error.

如果仍有问题,请检查mkmf.log文件以获取有关您的错误的更多具体详细信息。



On Ubuntu, try the following dependency fix combo:

在 Ubuntu 上,尝试以下依赖修复组合:

sudo apt-get install gcc ruby-dev libxslt-dev libxml2-dev zlib1g-dev

回答by Bryan Finlayson

The 'could not create Makefile' error you're seeing could also be because you haven't agreed to the Xcode license (you have to agree to it after each time you update Xcode). Running sudo xcodebuild -license acceptshould eliminate this error for you and allow you to then run gem install nokogirisuccessfully.

您看到的“无法创建 Makefile”错误也可能是因为您尚未同意 Xcode 许可证(每次更新 Xcode 后都必须同意)。运行sudo xcodebuild -license accept应该为您消除此错误,并允许您gem install nokogiri成功运行。