升级成macOS Mojave后,gem update失败

20
Building native extensions. This could take a while...
ERROR:  Error installing libxml-ruby:
    ERROR: Failed to build gem native extension.

    current directory: /Library/Ruby/Gems/2.3.0/gems/libxml-ruby-3.1.0/ext/libxml
/System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/bin/ruby -r ./siteconf20180926-80978-eoqfjh.rb extconf.rb
checking for libxml/xmlversion.h in /opt/include/libxml2,/opt/local/include/libxml2,/usr/local/include/libxml2,/usr/include/libxml2... no
*** 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=/System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/bin/$(RUBY_BASE_NAME)
    --with-xml2-config
    --without-xml2-config
    --with-xml2-dir
    --without-xml2-dir
    --with-xml2-include
    --without-xml2-include=${xml2-dir}/include
    --with-xml2-lib
    --without-xml2-lib=${xml2-dir}/lib
 extconf failure: need libxml2.

    Install the library or try one of the following options to extconf.rb:

      --with-xml2-config=/path/to/xml2-config
      --with-xml2-dir=/path/to/libxml2
      --with-xml2-lib=/path/to/libxml2/lib
      --with-xml2-include=/path/to/libxml2/include


To see why this extension failed to compile, please check the mkmf.log which can be found here:

  /Library/Ruby/Gems/2.3.0/extensions/universal-darwin-18/2.3.0/libxml-ruby-3.1.0/mkmf.log

extconf failed, exit code 1

Gem files will remain installed in /Library/Ruby/Gems/2.3.0/gems/libxml-ruby-3.1.0 for inspection.
Results logged to /Library/Ruby/Gems/2.3.0/extensions/universal-darwin-18/2.3.0/libxml-ruby-3.1.0/gem_make.out
Updating nokogiri
Building native extensions. This could take a while...
ERROR:  While executing gem ... (Gem::FilePermissionError)
    You don't have write permissions for the /usr/bin directory.
3个回答

56

出现这个错误是由于macOS Mojave停止将标准头文件放置在/usr/include中,而只将它们放在XCode目录中。

您可以通过运行ls /usr/include来验证这一点,最可能的情况是此目录不再存在。相反,通常的解决方法xcode-select --install将无法解决问题。

解决方法是运行由Apple提供的旧版安装程序,该安装程序将把头文件安装到/usr/include中。

sudo installer -pkg /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg -target /


谢谢,这确实有帮助,但Nokogiri仍然无法安装,问题在于我没有安装Nokogiri,似乎是因为系统路径中安装了Nokogiri gem,我似乎无法将其删除。 - Jozef Dransfield
这个解决方案对我在尝试在macOS Mojave 10.14.1上执行sudo gem update时非常有效。谢谢。 - CoBrA2168
我尝试了上述方法,但无法解决我的问题。然后我重新安装了 Ruby 2.3.3,问题得到了解决!谢谢。 - arthur bryant
解决了我的问题: 你的构建环境中缺少文件“/usr/include/iconv.h”,这意味着你没有正确安装Xcode命令行工具。 - colsen
虽然我相信有各种选择来解决这个问题,但这个方案最合适,因为我的猜测是其他库也会遇到与libxml相同的问题,但其他解决方案只关注于这个特定的库。 - Stan666
显示剩余2条评论

5
如果您正在使用Homebrew,则建议的方法是告诉pkg-config可在哪里找到libxml2库:
brew reinstall libxml2
export PKG_CONFIG_PATH="/usr/local/opt/libxml2/lib/pkgconfig"

我既沒有找到被接受的答案也沒有從這個答案中得到幫助。我通過在 https://github.com/twilio/twilio-ruby/issues/315#issuecomment-453697013 上提供的命令來解決問題。 - Vishal
这对我也起作用了,在Catalina 10.15.2上。谢谢伙计! - Iuliana Cosmina

3

此外,您可能拥有过时的捆绑配置,这会妨碍操作。运行bundle config build.nokogiri --use-system-libraries,它将给出一个警告并显示您当前所拥有的配置。我一直在遵循所有指南,并多次重新安装所有宝石甚至是 Ruby,只是发现配置设置为使用一个不再存在的路径。


谢谢!!!我已经有所有的部件,除了这一个!!!在Big Sur上工作。 - devjme

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接