How to install and run ffi on M1 Mac
Asked Answered
R

4

11

I'm using 'ffi', '~> 1.9',

I'm getting this error when I try to run my rails project

rake aborted!
LoadError: dlopen(/Users/kanye.west/.rbenv/versions/2.6.9/lib/ruby/gems/2.6.0/gems/ffi-1.9.25/lib/ffi_c.bundle, 0x0009): tried: '/Users/kanye.west/.rbenv/versions/2.6.9/lib/ruby/gems/2.6.0/gems/ffi-1.9.25/lib/ffi_c.bundle' (mach-o file, but is an incompatible architecture (have (x86_64), need (arm64e))) - /Users/kanye.west/.rbenv/versions/2.6.9/lib/ruby/gems/2.6.0/gems/ffi-1.9.25/lib/ffi_c.bundle
/Users/kanye.west/dev/anenta/config/application.rb:14:in `<top (required)>'
/Users/kanye.west/dev/anenta/Rakefile:5:in `require'
/Users/kanye.west/dev/anenta/Rakefile:5:in `<top (required)>'
/Users/kanye.west/.rbenv/versions/2.6.9/bin/bundle:23:in `load'
/Users/kanye.west/.rbenv/versions/2.6.9/bin/bundle:23:in `<main>'

Caused by:
LoadError: cannot load such file -- 2.6/ffi_c
/Users/kanye.west/dev/anenta/config/application.rb:14:in `<top (required)>'
/Users/kanye.west/dev/anenta/Rakefile:5:in `require'
/Users/kanye.west/dev/anenta/Rakefile:5:in `<top (required)>'
/Users/kanye.west/.rbenv/versions/2.6.9/bin/bundle:23:in `load'
/Users/kanye.west/.rbenv/versions/2.6.9/bin/bundle:23:in `<main>'
(See full trace by running task with --trace)

Approaches tried

gem install --user-install ffi -v '1.9.25' -- --enable-libffi-alloc

gem install ffi -v '1.9.25' -- --with-cflags="-Wno-error=implicit-function-declaration"

gem install ffi -v '1.9.25' --platform=rub

alias gem="arch -x86_64 sudo gem"

CFLAGS="-Wno-error=implicit-function-declaration" RUBY_CONFIGURE_OPTS='--with-readline-dir=/opt/homebrew/opt/readline' gem install ffi -v '1.9.25' -- --with-cflags="-Wno-error=implicit-function-declaration"

Ribaldry answered 23/8, 2022 at 4:13 Comment(0)
L
14

You need to install libffi using homebrew:

brew install libffi

Then the installation should succeed with:

gem install ffi

I've tested this on ruby 2.7.2 on my m1.

Also make sure that both libffi and ruby/gem are running on the same architecture. You may have mixed some arm64 and intel binaries which will lead to lots of compiler errors in the long run. If you encounter "wrong architecture" errors, try to reinstall ruby/homebrew on arm/m1.

For reference: I'm using chruby installed through homebrew and installed ruby 2.7.2 via ruby-install. Both homebrew and ruby are not using rosetta emulation and are compiled on arm64. I've installed libffi using brew install and then installed ffi using gem install without problems.

pascal@0xc0fefe ~> which ruby
/Users/pascal/.rubies/ruby-2.7.2/bin/ruby

pascal@0xc0fefe ~> which ruby-install
/opt/homebrew/bin/ruby-install

pascal@0xc0fefe ~> file /Users/pascal/.rubies/ruby-2.7.2/bin/ruby
/Users/pascal/.rubies/ruby-2.7.2/bin/ruby: Mach-O 64-bit executable arm64

pascal@0xc0fefe ~> gem install ffi
Building native extensions. This could take a while...
Successfully installed ffi-1.15.5
Parsing documentation for ffi-1.15.5
Done installing documentation for ffi after 0 seconds
1 gem installed
Libration answered 26/8, 2022 at 7:6 Comment(3)
This did not work for me I think the base architecture for brew was exploding my terminalRibaldry
Have you installed brew using arm or intel? I'm running homebrew and ruby on m1 without any problems and also have ffi installed. Do you have rosetta emulation enabled for your terminal or homebrew?Libration
that 2nd part, rosetta emulation and reinstalling homebrew ruby environment and architecture is what fixed the problem for me.Ribaldry
L
5

The thing is that official support for M1 was added in ffi v 1.14.0, so try update to that version, should not break anything.

gem uninstall ffi

and change manually in Gemfile.lock to 1.14.0.

gem install ffi -v '1.14.0'

This is what worked for me.

Lizbeth answered 10/11, 2022 at 22:58 Comment(0)
B
1

I've been trying to install a demo React Native App that innocently asked to run pod install and the went down this ffi rabbit hole.

On my M2 / Monterey, this article worked well except it was missing the instruction to install the ruby plugin for asdf

asdf plugin add ruby https://github.com/asdf-vm/asdf-ruby.git

Billibilliard answered 26/11, 2022 at 12:52 Comment(1)
hello can you update the link? its not working. I'm using M2 also. Thank you!Leon
R
0

Here's the only workaround I found working, download Rosette, https://support.apple.com/en-us/HT211861, and uninstall (you need to do this in order for anything not compatible with the current architecture):

  • ruby versions
  • homebrew
  • rbenv

and select Get Info on Terminal, and check the Open using Rosetta.

Now reinstall

  • homebrew
  • rbenv
  • ruby Run ruby -v Check and verify that your version has ... [universal.x86_64-darwin21]
Ribaldry answered 30/8, 2022 at 19:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.