Can't use Nokogiri gem on M1 processor

Viewed 57

I have a problem with using the M1 processor and Nokogiri gem.

Gemfile

...
gem 'nokogiri', '>= 1.13.5'
...

When I run bundle install, all seems good, but when I try to run specs, for example, I see the error:

LoadError:
  dlopen(/Users/ruslan/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/nokogiri-1.13.6/lib/nokogiri/nokogiri.bundle, 0x0009): tried: '/Users/ruslan/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/nokogiri-1.13.6/lib/nokogiri/nokogiri.bundle' (mach-o file, but is an incompatible architecture (have 'x86_64', need 'arm64e')) - /Users/ruslan/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/nokogiri-1.13.6/lib/nokogiri/nokogiri.bundle

I've tried to install Nokogiri with gem install nokogiri -v 1.13.6 --platform arm64-darwin, and now it looks like that:

nokogiri -versions:

# Nokogiri (1.13.6)
    ---
    warnings: []
    nokogiri:
      version: 1.13.6
      cppflags:
      - "-I/Users/ruslan/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/nokogiri-1.13.6-arm64-darwin/ext/nokogiri"
      - "-I/Users/ruslan/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/nokogiri-1.13.6-arm64-darwin/ext/nokogiri/include"
      - "-I/Users/ruslan/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/nokogiri-1.13.6-arm64-darwin/ext/nokogiri/include/libxml2"
      ldflags: []
    ruby:
      version: 2.7.2
      platform: arm64-darwin21
      gem_platform: arm64-darwin-21
      description: ruby 2.7.2p137 (2020-10-01 revision 5445e04352) [arm64-darwin21]
      engine: ruby
    libxml:
      source: packaged
      precompiled: true
      patches:
      - 0001-Remove-script-macro-support.patch
      - 0002-Update-entities-to-remove-handling-of-ssi.patch
      - 0003-libxml2.la-is-in-top_builddir.patch
      - 0004-use-glibc-strlen.patch
      - 0005-avoid-isnan-isinf.patch
      - 0006-update-automake-files-for-arm64.patch
      - '0008-htmlParseComment-handle-abruptly-closed-comments.patch'
      - '0009-allow-wildcard-namespaces.patch'
      libxml2_path: "/Users/ruslan/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/nokogiri-1.13.6-arm64-darwin/ext/nokogiri"
      memory_management: ruby
      iconv_enabled: true
      compiled: 2.9.14
      loaded: 2.9.14
    libxslt:
      source: packaged
      precompiled: true
      patches:
      - 0001-update-automake-files-for-arm64.patch
      datetime_enabled: true
      compiled: 1.1.35
      loaded: 1.1.35
    other_libraries:
      zlib: 1.2.12
      libiconv: '1.16'
      libgumbo: 1.0.0-nokogiri

But when I try to run specs with this version only, I have an error:

Could not find nokogiri-1.13.6 in any of the sources
Run `bundle install` to install missing gems.

So to avoid this error, I have to install Nokogiri through bundle install, but it installs the wrong version, not for the arm platform, and then I have a problem with the platform. If I install Nokogiri through the gem install command, the bundler doesn't see it.

My bundle config:

Settings are listed in order of priority. The top value will be used.
build.libv8
Set for the current user (/Users/ruslan/.bundle/config): "--with-system-v8"

build.libxml-ruby
Set for the current user (/Users/ruslan/.bundle/config): "--with-xml2-config=/usr/local/opt/libxml2/bin/xml2-config"

build.nokogiri
Set for your local app (/Users/ruslan/3commas/3commas/.bundle/config): "--use-system-libraries"

default
Set for the current user (/Users/ruslan/.bundle/config): "2.2.21"

force_ruby_platform
Set for your local app (/Users/ruslan/3commas/3commas/.bundle/config): true
Set for the current user (/Users/ruslan/.bundle/config): true

github.https
Set for the current user (/Users/ruslan/.bundle/config): "true"

Maybe somebody knows how to figure out this problem? Many thanks in advance!

1 Answers

I wonder if this is an issue related to the Gemfile.lock file, which can have explicit platforms to bundle for. You can check by looking for PLATFORMS within that file.

If you add Apple's ARM platform to your setup via bundler, that may help the correct version of nokogiri to be bundled? Try running the following on the command line:

bundle lock --add-platform arm64-darwin-21
Related