'Ruby bundle install fail

My system is macOS m1 chip. I'm using ruby to bundle install from gemfile, but it seems like the llhttp-ffi gem has conflict, I'm not really sure about the problem. So below is the output when I tried bundle install. I've tried to search about Failed to build gem native extension or just gem install ffi, both have not worked.

$ bundle install
Fetching gem metadata from https://rubygems.org/........
Resolving dependencies...
Using rake 13.0.6
Using public_suffix 4.0.6
Using dry-inflector 0.2.1
Using ast 2.4.2
Using ice_nine 0.11.2
Using ffi 1.15.4
Using thor 1.1.0
Using path_expander 1.1.0
Using sexp_processor 4.15.3
Using hashdiff 1.0.1
Using http-form_data 2.3.0
Using kwalify 0.7.2
Using rb-fsevent 0.11.0
Using method_source 1.0.0
Using minitest 5.14.4
Using nio4r 2.5.8
Using parallel 1.21.0
Using rack 2.2.3
Using rainbow 3.0.0
Using regexp_parser 2.1.1
Using ruby-progressbar 1.11.0
Using unicode-display_width 2.1.0
Using sequel 5.50.0
Using simplecov-html 0.12.3
Using simplecov_json_formatter 0.1.3
Using temple 0.8.2
Using tilt 2.0.10
Using bundler 2.2.22
Using vcr 6.0.0
Using addressable 2.8.0
Using ffi-compiler 1.0.1
Using figaro 1.2.0
Using ruby_parser 3.17.0
Using rb-inotify 0.10.1
Using minitest-rg 5.2.0
Using parser 3.0.2.0
Using puma 5.5.2
Using roda 3.49.0
Using slim 4.1.0
Using coderay 1.1.3
Using concurrent-ruby 1.1.9
Using rexml 3.2.5
Using docile 1.4.0
Using unf_ext 0.0.8
Fetching sqlite3 1.4.2
Using crack 0.4.5
Using unf 0.1.4
Using dry-core 0.7.1
Using flog 4.6.4
Fetching llhttp-ffi 0.4.0
Using listen 3.7.0
Using pry 0.14.1
Using reek 6.0.6
Using rubocop-ast 1.12.0
Using simplecov 0.21.2
Using domain_name 0.5.20190701
Using dry-configurable 0.13.0
Using dry-logic 1.2.0
Using rerun 0.13.1
Using rubocop 1.22.1
Using webmock 3.14.0
Using dry-container 0.9.0
Using http-cookie 1.0.4
Using dry-types 1.5.1
Using dry-struct 1.4.0
Installing llhttp-ffi 0.4.0 with native extensions
Installing sqlite3 1.4.2 with native extensions
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

    current directory: /opt/homebrew/lib/ruby/gems/3.0.0/gems/llhttp-ffi-0.4.0/ext
/opt/homebrew/opt/ruby/bin/ruby -I/opt/homebrew/Cellar/ruby/3.0.2_1/lib/ruby/3.0.0 -rrubygems /opt/homebrew/lib/ruby/gems/3.0.0/gems/rake-13.0.6/exe/rake
RUBYARCHDIR\=/opt/homebrew/lib/ruby/gems/3.0.0/extensions/arm64-darwin-20/3.0.0/llhttp-ffi-0.4.0
RUBYLIBDIR\=/opt/homebrew/lib/ruby/gems/3.0.0/extensions/arm64-darwin-20/3.0.0/llhttp-ffi-0.4.0
rake aborted!
LoadError: dlopen(/opt/homebrew/lib/ruby/gems/3.0.0/gems/ffi-1.15.4/lib/ffi_c.bundle, 9): no suitable image found.  Did find:
        /opt/homebrew/lib/ruby/gems/3.0.0/gems/ffi-1.15.4/lib/ffi_c.bundle: mach-o, but wrong architecture
        /opt/homebrew/lib/ruby/gems/3.0.0/gems/ffi-1.15.4/lib/ffi_c.bundle: mach-o, but wrong architecture - /opt/homebrew/lib/ruby/gems/3.0.0/gems/ffi-1.15.4/lib/ffi_c.bundle
<internal:/opt/homebrew/Cellar/ruby/3.0.2_1/lib/ruby/3.0.0/rubygems/core_ext/kernel_require.rb>:85:in `require'
<internal:/opt/homebrew/Cellar/ruby/3.0.2_1/lib/ruby/3.0.0/rubygems/core_ext/kernel_require.rb>:85:in `require'
/opt/homebrew/lib/ruby/gems/3.0.0/gems/ffi-1.15.4/lib/ffi.rb:5:in `rescue in <top (required)>'
/opt/homebrew/lib/ruby/gems/3.0.0/gems/ffi-1.15.4/lib/ffi.rb:2:in `<top (required)>'
<internal:/opt/homebrew/Cellar/ruby/3.0.2_1/lib/ruby/3.0.0/rubygems/core_ext/kernel_require.rb>:85:in `require'
<internal:/opt/homebrew/Cellar/ruby/3.0.2_1/lib/ruby/3.0.0/rubygems/core_ext/kernel_require.rb>:85:in `require'
/opt/homebrew/lib/ruby/gems/3.0.0/gems/ffi-compiler-1.0.1/lib/ffi-compiler/compile_task.rb:4:in `<top (required)>'
<internal:/opt/homebrew/Cellar/ruby/3.0.2_1/lib/ruby/3.0.0/rubygems/core_ext/kernel_require.rb>:160:in `require'
<internal:/opt/homebrew/Cellar/ruby/3.0.2_1/lib/ruby/3.0.0/rubygems/core_ext/kernel_require.rb>:160:in `rescue in require'
<internal:/opt/homebrew/Cellar/ruby/3.0.2_1/lib/ruby/3.0.0/rubygems/core_ext/kernel_require.rb>:149:in `require'
/opt/homebrew/lib/ruby/gems/3.0.0/gems/llhttp-ffi-0.4.0/ext/Rakefile:3:in `<top (required)>'
/opt/homebrew/lib/ruby/gems/3.0.0/gems/rake-13.0.6/exe/rake:27:in `<main>'

Caused by:
LoadError: cannot load such file -- 3.0/ffi_c
<internal:/opt/homebrew/Cellar/ruby/3.0.2_1/lib/ruby/3.0.0/rubygems/core_ext/kernel_require.rb>:85:in `require'
<internal:/opt/homebrew/Cellar/ruby/3.0.2_1/lib/ruby/3.0.0/rubygems/core_ext/kernel_require.rb>:85:in `require'
/opt/homebrew/lib/ruby/gems/3.0.0/gems/ffi-1.15.4/lib/ffi.rb:3:in `<top (required)>'
<internal:/opt/homebrew/Cellar/ruby/3.0.2_1/lib/ruby/3.0.0/rubygems/core_ext/kernel_require.rb>:85:in `require'
<internal:/opt/homebrew/Cellar/ruby/3.0.2_1/lib/ruby/3.0.0/rubygems/core_ext/kernel_require.rb>:85:in `require'
/opt/homebrew/lib/ruby/gems/3.0.0/gems/ffi-compiler-1.0.1/lib/ffi-compiler/compile_task.rb:4:in `<top (required)>'
<internal:/opt/homebrew/Cellar/ruby/3.0.2_1/lib/ruby/3.0.0/rubygems/core_ext/kernel_require.rb>:160:in `require'
<internal:/opt/homebrew/Cellar/ruby/3.0.2_1/lib/ruby/3.0.0/rubygems/core_ext/kernel_require.rb>:160:in `rescue in require'
<internal:/opt/homebrew/Cellar/ruby/3.0.2_1/lib/ruby/3.0.0/rubygems/core_ext/kernel_require.rb>:149:in `require'
/opt/homebrew/lib/ruby/gems/3.0.0/gems/llhttp-ffi-0.4.0/ext/Rakefile:3:in `<top (required)>'
/opt/homebrew/lib/ruby/gems/3.0.0/gems/rake-13.0.6/exe/rake:27:in `<main>'

Caused by:
LoadError: cannot load such file -- ffi-compiler/compile_task
<internal:/opt/homebrew/Cellar/ruby/3.0.2_1/lib/ruby/3.0.0/rubygems/core_ext/kernel_require.rb>:85:in `require'
<internal:/opt/homebrew/Cellar/ruby/3.0.2_1/lib/ruby/3.0.0/rubygems/core_ext/kernel_require.rb>:85:in `require'
/opt/homebrew/lib/ruby/gems/3.0.0/gems/llhttp-ffi-0.4.0/ext/Rakefile:3:in `<top (required)>'
/opt/homebrew/lib/ruby/gems/3.0.0/gems/rake-13.0.6/exe/rake:27:in `<main>'
(See full trace by running task with --trace)

rake failed, exit code 1

Gem files will remain installed in /opt/homebrew/lib/ruby/gems/3.0.0/gems/llhttp-ffi-0.4.0 for inspection.
Results logged to /opt/homebrew/lib/ruby/gems/3.0.0/extensions/arm64-darwin-20/3.0.0/llhttp-ffi-0.4.0/gem_make.out

An error occurred while installing llhttp-ffi (0.4.0), and Bundler cannot continue.
Make sure that `gem install llhttp-ffi -v '0.4.0' --source 'https://rubygems.org/'` succeeds before bundling.

In Gemfile:
  http was resolved to 5.0.4, which depends on
    llhttp-ffi


Solution 1:[1]

I was seeing the same problem while updating Ruby from 2.7.5 to 2.7.6. Big Sur 11.6.1, Intel i7, fwiw.

After many confusing errors and failed solutions, I deleted my entire bundle/vendor directory and bundled anew. This got me a clean bundle install.

I'm not sure what the conflict was, but a clean slate fixed it.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 David Hempy