Ruby-on-rails 'require': 无法加载此类文件 -- 运行 `rails server` 时出现 'nokogiri\nokogiri' (LoadError)

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

'require': cannot load such file -- 'nokogiri\nokogiri' (LoadError) when running `rails server`

ruby-on-railsrubynokogiri

提问by Shef

I'm running a clean install of Ruby 2.2.1 on Windows 8.1 with DevKit. After the installation I run:

我正在使用 DevKit 在 Windows 8.1 上运行全新安装的 Ruby 2.2.1。安装后我运行:

gem install rails
rails new testapp
cd testapp
rails server

leaving everything else at default.

将其他一切保留为默认值。

The process fails at the last line when, instead of running the server, I get the error message

当我收到错误消息而不是运行服务器时,该过程在最后一行失败

in 'require': cannot load such file -- 'nokogiri\nokogiri' (LoadError)

It happens every time and I've looked around and tried everything I found to fix it, but nothing so far has worked.

它每次都会发生,我环顾四周并尝试了我发现的所有方法来修复它,但到目前为止没有任何效果。

What is the problem here and how do I get a simple test Rails app to work?

这里有什么问题,我如何让一个简单的测试 Rails 应用程序工作?

回答by Mike Dalessio

Nokogiri doesn't support Ruby 2.2 on Windows yet. The next release will. See https://github.com/sparklemotion/nokogiri/issues/1256

Nokogiri 还不支持 Windows 上的 Ruby 2.2。下一个版本会。见https://github.com/sparklemotion/nokogiri/issues/1256

Nokogiri doesn't support native builds (e.g. with devkit) on Windows. Instead it provides gems containing prebuilt DLLs.

Nokogiri 不支持 Windows 上的本机构建(例如使用 devkit)。相反,它提供包含预构建 DLL 的 gem。

There's a discussion which you may want to join or watch on the topic of devkit build support here: https://github.com/sparklemotion/nokogiri/issues/1190

您可能想加入或观看有关 devkit 构建支持主题的讨论:https: //github.com/sparklemotion/nokogiri/issues/1190

回答by Julio Elixir

  1. First, uninstall the version of Nokogiri you currently have with:

    gem uninstall nokogiri
    
  2. Download Nokogiri 1.6.6.2 (x64)or Nokogiri 1.6.6.2 (x86)

  3. Install this version locally using:

    gem install --local C:\Users$user$\Downloads\nokogiri-1.6.6.2-x64-mingw32.gem
    

    or if you're running 32bit Ruby:

    gem install --local C:\Users$user$\Downloads\nokogiri-1.6.6.2-x86-mingw32.gem
    

    The path may differ depending on where you downloaded the file to.

  1. 首先,卸载您当前使用的 Nokogiri 版本:

    gem uninstall nokogiri
    
  2. 下载Nokogiri 1.6.6.2 (x64)Nokogiri 1.6.6.2 (x86)

  3. 使用以下方法在本地安装此版本:

    gem install --local C:\Users$user$\Downloads\nokogiri-1.6.6.2-x64-mingw32.gem
    

    或者如果您运行的是 32 位 Ruby:

    gem install --local C:\Users$user$\Downloads\nokogiri-1.6.6.2-x86-mingw32.gem
    

    路径可能会有所不同,具体取决于您将文件下载到的位置。

Try to start the server again using ruby bin\rails server, and it should work.

尝试使用 再次启动服务器ruby bin\rails server,它应该可以工作。

回答by Pascal

I got Nokogiri running with Ruby 2.2 on Windows 10 with a mix of Mike Dalessios and Julios answer:

我让 Nokogiri 在 Windows 10 上使用 Ruby 2.2 运行,混合了 Mike Dalessios 和 Julios 的回答:

  1. Look for the latest version of Nokogiri in Nokogiri's github repo.
  2. Run gem uninstall nokogiri.
  3. Add gem "nokogiri", ">= 1.6.7.rc"to your Gemfile.
  4. Run bundle install.
  5. Run bundle update nokogiriif bundle has locked Nokogiri at some version.
  1. Nokogiri 的 github repo 中查找最新版本的 Nokogiri 。
  2. 运行gem uninstall nokogiri
  3. 添加gem "nokogiri", ">= 1.6.7.rc"到您的 Gemfile。
  4. 运行bundle install
  5. bundle update nokogiri如果 bundle 在某个版本中锁定了 Nokogiri,则运行。

回答by Richard Peck

enter image description here

在此处输入图片说明

Fix

使固定

  1. Bundle install (gets Nokogiri files)
  2. Browse to ruby_dir\lib\ruby\gems\2.2.0\gems\nokogiri-1.6.6.2\ext\nokogiri
  3. Open extconf.rb
  4. Add dir_config('iconv').any? or pkg_config('libiconv')to #376
  5. Download MinGW64& MSYSfolders from Mega
  6. Add them to PATHin Windows (remove Devkitpath refs - it doesn't work)
  7. Download libxml2,libxslt, iconvlibraries(or here)
  8. Run ruby extconf.rb --platform=ruby --n --use-system-librariesreferencing downloaded libraries
  9. Run make
  10. Run make install
  1. 捆绑安装(获取 Nokogiri 文件)
  2. 浏览到 ruby_dir\lib\ruby\gems\2.2.0\gems\nokogiri-1.6.6.2\ext\nokogiri
  3. 打开 extconf.rb
  4. 添加dir_config('iconv').any? or pkg_config('libiconv')#376
  5. 从 Mega下载MinGW64MSYS文件夹
  6. 将它们添加到PATHWindows(删除Devkit路径引用 - 它不起作用)
  7. 下载libxml2libxslticonv(或此处
  8. 运行ruby extconf.rb --platform=ruby --n --use-system-libraries引用下载的库
  9. make
  10. make install


Steps

脚步

Bundle Install

捆绑安装

First step is to bundle.

第一步是捆绑.

This will put the nokogirigem on your machine without running the pre-packaged compiler (which mostly doesn't work in Windows).

这会将nokogirigem 放在您的机器上,而无需运行预先打包的编译器(这在 Windows 中通常不起作用)。

This will show Nokogiri as installed:

这将显示 Nokogiri 已安装

enter image description here

在此处输入图片说明

Browse

浏览

Browse to the nokogirifolder, to find ext/nokogiri/extconf.rb:

浏览到nokogiri文件夹,找到ext/nokogiri/extconf.rb

enter image description here

在此处输入图片说明

Open extconf.rb

打开 extconf.rb

... and add dir_config('iconv').any? or pkg_config('libiconv')to #376

...并添加dir_config('iconv').any? or pkg_config('libiconv')#376

enter image description here

在此处输入图片说明

Standard Nokogiri installs "rely" on the libxml2inclusion of iconv- we need to explicitly define it, otherwise iconv.h is missingerrors will occur.

标准 Nokogiri 安装“依赖”libxml2包含iconv——我们需要明确定义它,否则iconv.h is missing会发生错误。

Add Toolchain

添加工具链

Don't use devkitfor this - it doesn't work.

不要devkit用于此 - 它不起作用。

You need MinGW:

你需要MinGW

enter image description here

在此处输入图片说明

I have zipped my exact MinGW64and MSYS64folders on Mega(key: !FJtcq25l-QMsNltCxllMhc1IGqORvap8xv8gWxSUbDA):

我已经在 Mega 上压缩了我的确切MinGW64MSYS64文件夹(键:)!FJtcq25l-QMsNltCxllMhc1IGqORvap8xv8gWxSUbDA

enter image description here

在此处输入图片说明

Add to PATH

添加到路径

This gives access to gcc& make(both required):

这可以访问gcc& make(两者都需要):

enter image description here

在此处输入图片说明

Remove the devkitref from your path, and add the following:

devkit从您的路径中删除ref,并添加以下内容:

  • MINGW64_PATH/bin
  • MSYS64_PATH/bin
  • MINGW64_PATH/bin
  • MSYS64_PATH/bin

Download Libs

下载库

I have added the libs to Mega:

我已将库添加到 Mega:

enter image description here

在此处输入图片说明

You will unzip them here:

您将在此处解压缩它们:

enter image description here

在此处输入图片说明

All the libs are from this source.

所有的库都来自这个来源

Run extconf.rb

extconf.rb

Once libs are on your system, you can run ruby extconf.rbto configure the build:

一旦库在您的系统上,您就可以运行ruby extconf.rb以配置构建:

enter image description here

在此处输入图片说明

32bit

ruby extconf.rb --platform=ruby -N -- --use-system-libraries --with-xml2-dir=C:/Dev/Dependencies/Ruby/lib/nokogiri/32bit/libxml2-2.9.2-win32-x86 --with-xml2-include=C:/Dev/Dependencies/Ruby/lib/nokogiri/32bit/libxml2-2.9.2-win32-x86/include/libxml2 --with-iconv-dir=C:/Dev/Dependencies/Ruby/lib/nokogiri/32bit/iconv-1.14-win32-x86 --with-xslt-dir=C:/Dev/Dependencies/Ruby/lib/nokogiri/32bit/libxslt-1.1.28-win32-x86

64bit

#64 ruby extconf.rb --platform=ruby -N -- --use-system-libraries --with-xml2-dir=C:/Dev/Dependencies/Ruby/lib/nokogiri/64bit/libxml2-2.9.2-win32-x86_64 --with-xml2-include=C:/Dev/Dependencies/Ruby/lib/nokogiri/64bit/libxml2-2.9.2-win32-x86_64/include/libxml2 --with-iconv-dir=C:/Dev/Dependencies/Ruby/lib/nokogiri/64bit/iconv-1.14-win32-x86_64 --with-xslt-dir=C:/Dev/Dependencies/Ruby/lib/nokogiri/64bit/libxslt-1.1.28-win32-x86_64

32位

ruby extconf.rb --platform=ruby -N -- --use-system-libraries --with-xml2-dir=C:/Dev/Dependencies/Ruby/lib/nokogiri/32bit/libxml2-2.9.2-win32-x86 --with-xml2-include=C:/Dev/Dependencies/Ruby/lib/nokogiri/32bit/libxml2-2.9.2-win32-x86/include/libxml2 --with-iconv-dir=C:/Dev/Dependencies/Ruby/lib/nokogiri/32bit/iconv-1.14-win32-x86 --with-xslt-dir=C:/Dev/Dependencies/Ruby/lib/nokogiri/32bit/libxslt-1.1.28-win32-x86

64位

#64 ruby extconf.rb --platform=ruby -N -- --use-system-libraries --with-xml2-dir=C:/Dev/Dependencies/Ruby/lib/nokogiri/64bit/libxml2-2.9.2-win32-x86_64 --with-xml2-include=C:/Dev/Dependencies/Ruby/lib/nokogiri/64bit/libxml2-2.9.2-win32-x86_64/include/libxml2 --with-iconv-dir=C:/Dev/Dependencies/Ruby/lib/nokogiri/64bit/iconv-1.14-win32-x86_64 --with-xslt-dir=C:/Dev/Dependencies/Ruby/lib/nokogiri/64bit/libxslt-1.1.28-win32-x86_64

make

make

enter image description here

在此处输入图片说明

This may create errors / warnings, as long as it says "Error 1 (ignored)", it should be okay.

这可能会产生错误/警告,只要它说“ Error 1 (ignored)”,就应该没问题。

Following that, use make install:

之后,使用make install

enter image description here

在此处输入图片说明

Then browse to your Rails installation and run rails s:

然后浏览到您的 Rails 安装并运行rails s

enter image description here

在此处输入图片说明



Explanation

解释

To give context:

给出上下文:

Ruby 2.2+ on Windows doesn't compile the extensionsNokogiri requires.

Windows 上的 Ruby 2.2+ 不编译Nokogiri 所需的扩展

The extensionsof a gem are the extra dependencies (libraries) it uses.

gem的扩展是它使用的额外依赖项(库)。

They are built when you installthe gem:

它们是在您安装gem时构建的:

enter image description here

在此处输入图片说明



Extensions

扩展

Lack of extensions is preventing Nokogiri from running.

缺乏扩展阻止了 Nokogiri 运行。

Extensions exist in the extfolder of a gem (you can read about them here):

扩展存在于extgem的文件夹中(你可以在这里阅读它们):

enter image description here

在此处输入图片说明

Mysql2,RMagick,PGSQL, Nokogirietc alluse extensions/libraries.

Mysql2RMagickPGSQLNokogiri所有使用扩展/库。

This is why - on Windows - you have to use custom switches (--with-opt-dir) when installing the gem. This gives Ruby / the shell / (cmd) the required lib/ includedirectories required to build the gem's files (it's the equivalent of how PATHworks).

这就是为什么 - 在 Windows 上 ---with-opt-dir安装 gem 时必须使用自定义开关 ( )。这为 Ruby / shell / ( cmd)提供了构建 gem 文件所需的lib/include目录(它相当于PATH工作原理)。

On Linux/Mac, these directories are managed with the respective package managers (brew/apt-get). Windows does not have this, so you have to install the extensions manually.

Linux/ 上Mac,这些目录由各自的包管理器 ( brew/ apt-get) 管理。Windows 没有这个,所以你必须手动安装扩展。

Because Windows does not have a standard set of libraries, you have to download them yourself. You also have to buildthem yourself (which is tricky).

因为 Windows 没有一套标准的库,你必须自己下载它们。您还必须自己构建它们(这很棘手)。

The fix for Nokogiri install is to use the right libraries and build tools to get the gem installed.

Nokogiri 安装的修复是使用正确的库和构建工具来安装 gem。



Build

建造

The difference with Ruby 2.2+ is the gem will "install" without showing any exceptions. You think it has installed, only to find Rails does notload (hence the nokogiri/nokogiri.soerror).

与 Ruby 2.2+ 的不同之处在于 gem 将“安装”而不显示任何异常。你以为它已经安装了,才发现 Rails没有加载(因此出现nokogiri/nokogiri.so错误)。

This means you have to make sure you have the files on your system, and run the compiler to install them.

这意味着您必须确保系统上有这些文件,并运行编译器来安装它们。

The above documentation should show you how to do that.

上面的文档应该向您展示如何做到这一点。