ffi: error: implicit declaration of function 'ffi_prep_closure' on M1 macOS Big Sur

❯ rbenv install 2.4.0
Installing ruby-2.4.0...

WARNING: ruby-2.4.0 is past its end of life and is now unsupported.
It no longer receives bug fixes or critical security updates.

ruby-build: using readline from homebrew

BUILD FAILED (macOS 11.1 using ruby-build 20201210-10-g5de6d5f)

Inspect or clean up the working tree at /var/folders/mz/q1f7_y852v7__rt9f3lm0yd40000gn/T/ruby-build.20201229181439.24184.CZQlN7
Results logged to /var/folders/mz/q1f7_y852v7__rt9f3lm0yd40000gn/T/ruby-build.20201229181439.24184.log

Last 10 log lines:
compiling fiber.c
linking shared-object fiber.bundle
compiling closure.c
closure.c:263:14: error: implicit declaration of function 'ffi_prep_closure' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
    result = ffi_prep_closure(pcl, cif, callback, (void *)self);
             ^
1 error generated.
make[2]: *** [closure.o] Error 1
make[1]: *** [ext/fiddle/all] Error 2
make: *** [build-ext] Error 2

logs

ruby-build.20201229181439.24184.log

install ruby 3.0.0 is ok

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 3
  • Comments: 30

Commits related to this issue

Most upvoted comments

Hi! I’m having this same problem using RVM, I tried all the top solutions but is not working, any ideas 🙏 ?

I am also using RVM and in my situation, the following worked for me:

Step 1 Run in terminal: brew info libffi which will output the following:

For compilers to find libffi you may need to set: export LDFLAGS=“-L/opt/homebrew/opt/libffi/lib” export CPPFLAGS=“-I/opt/homebrew/opt/libffi/include”

For pkg-config to find libffi you may need to set: export PKG_CONFIG_PATH=“/opt/homebrew/opt/libffi/lib/pkgconfig”

Step 2 Run all 3 export commands in terminal to set the env variables.

Step 3 Finally, run the ruby installation command. In my case, it was: rvm install ruby-2.5.3

Hope it helps.

By the way, in a later date, I thought to record a video to demo how to do this. Here it goes: https://www.youtube.com/watch?v=M6Y6VIFHAL4

I was able to install other versions by passing USE_FFI_CLOSURE_ALLOC, like this

RUBY_CFLAGS=-DUSE_FFI_CLOSURE_ALLOC rbenv install 2.6.6

I was able to install other versions by passing USE_FFI_CLOSURE_ALLOC, like this

RUBY_CFLAGS=-DUSE_FFI_CLOSURE_ALLOC rbenv install 2.6.6

New M1 macbook, this also worked for me!

In case it helps people using ruby-install, the following worked for that:

 ruby-install ruby-2.4.3 -- CFLAGS=-DUSE_FFI_CLOSURE_ALLOC

Hi! I’m having this same problem using RVM, I tried all the top solutions but is not working, any ideas 🙏 ?

For rvm users: all 3 of these work-arounds worked for me:

  1. Install new version and let compiler know:
brew install libffi
export PKG_CONFIG_PATH="/opt/homebrew/opt/libffi/lib/pkgconfig"
LDFLAGS="-L/opt/homebrew/opt/libffi/lib" CPPFLAGS="-I/opt/homebrew/opt/libffi/include" rvm install 2.5.1
  1. Ignore deprecated syntax:
CPPFLAGS=-DUSE_FFI_CLOSURE_ALLOC rvm install 2.5.1 
  1. Don’t install fiddle:
rvm install 2.5.1 --with-out-ext=fiddle

(see rvm issue #5153)

I had the same problem on macOS 11.2.3 trying to install ruby 2.6.6 with RVM. @shakilearl’s solution helped with the installation problem. 2.6.6 works fine but when rvm list, I get:

=> ruby-2.6.6 [ broken ]
 * ruby-3.0.0 [ arm64 ]

What does your rvm list log?

Same problem on ventura 13.1, fixed like this:

eval $(brew info libffi|grep ' export ')

rvm install ruby-2.4.10

would recommend trying the upvoted solutions from rvm/rvm#5047 or alternatively run

rvm install x.x.x -C --with-openssl-dir=$HOME/.rvm/usr --with-out-ext=fiddle

tested on Mac OSX Monterey v12.6.6

For anyone else that is not using the homebrew-bootstrap script that is reference above, I believe the line to execute should simply be

export RUBY_CFLAGS=-DUSE_FFI_CLOSURE_ALLOC

So for me, I have the M1 Mac and using rbenv and seeing this issue when trying to use rbenv install. When I export that flag, the issue was resolved and I can freely install ruby versions.

In the end, we decided to migrate everything to ruby 2.7.2 which seems like the earliest compatible version with M1s.

For me also same issue, it shows as broken.

  • ruby-2.5.8 [ broken ] => ruby-2.6.6 [ broken ]

Will Ruby work when it’s broken?

I have this as well, but so far everything seems to work as expected 🤷‍♂️

The same error happens to me. I can install

• 3.0.0 • 2.7.2

But when I try to install 2.7.1 or 2.6.6 I have the exact same issue. I’m using ffi 1.14.2.