How to resolve LoadError: cannot load such file -- ffi_c
Asked Answered
P

4

19

I would like to know how to resolve next error seen when executed require command on the console after installed Ruby 2.2.1 Windows installer and Ruby gem 2.4.6.

LoadError: cannot load such file -- ffi_c
        from C:/Ruby22-x64/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_req
uire.rb:54:in `require'

Is this something like DLL?

Phytosociology answered 17/3, 2015 at 5:56 Comment(3)
whats the ffi gem version ?Nichol
Hi Shivam, ffi library version is 1.9.8 x64-mingw32.Phytosociology
@Dorian That's a problem from upgrading Ubuntu, which changes the version of libffi from 6 to 7. It is not related to this problem, which is using Windows.Treatise
N
16

If you read the requirement documentation for ffi, you can see:

You need a sane building environment in order to compile the extension. At a minimum, you will need:

  • A C compiler (e.g. Xcode on OSX, gcc on everything else)
  • libffi development library - this is commonly in the libffi-dev or libffi-devel

This means the gem is not pre-compiled, and has to compile code when it installs. Which in turn means if you are running on a PC you will need to install the Ruby development kit for windows, aka 'devkit', you can get it from the downloads page on the rubyinstaller site

Download and install devkit first, then open a new command line window followed by:

gem install ffi

Refer to this SO for details: https://mcmap.net/q/670208/-no-such-file-to-load-ffi_c-loaderror

Nichol answered 17/3, 2015 at 9:13 Comment(4)
Thanks, Shivam. However, during installation of v1.0.9 ffi after successfully having installed ffi, I encountered next error, which says; Fetching: ffi-1.0.9.gem (100%) ERROR: Error installing ffi: The 'ffi' native gem requires installed build tools. Please update your PATH to include build tools or download the DevKit from 'rubyinstaller.org/downloads' and follow the instructions at 'github.com/oneclick/rubyinstaller/wiki/Development-Kit'Phytosociology
So couldn't install ffi version 1.0.9 successfully yet unfortunately.Phytosociology
After avoiding the use of the latest version of ruby as of Ruby 2.2.1 (x64) but rather 2.0.0-p643 (x64), installation of Ruby, its gem and devkit were all succeeded.Phytosociology
Looks like a recent upgrade in Xcode caused this. Uninstalling and installing again fixed the problem. Thanks for the explanationRipp
H
12

In my case literally none of these helped, after a 6 hours debug and search ..

What actually worked for me :

sudo gem uninstall ffi && sudo gem install ffi -- --enable-libffi-alloc
Haire answered 6/4, 2023 at 13:32 Comment(1)
this worked for me, but only sort of: I still get a segmentation fault using Guard (but not '$rails test'). Go figure. Still, I can move forward, for now, and I found this answer after only 3 hours of struggling... so thanks! ;)Keverne
L
4

Entire error I got as below, but on Mac:

/Users/mayuresh.srivastava/.rvm/gems/ruby-2.7.3/gems/ffi-1.15.4/lib/ffi.rb:3:in `require': cannot load such file -- 2.7/ffi_c (LoadError)

/Users/mayuresh.srivastava/.rvm/gems/ruby-2.7.3/gems/ffi-1.15.4/lib/ffi.rb:5:in `require': dlopen(/Users/mayuresh.srivastava/.rvm/gems/ruby-2.7.3/gems/ffi-1.15.4/lib/ffi_c.bundle, 9): Library not loaded: /opt/homebrew/opt/libffi/lib/libffi.7.dylib (LoadError)
  Referenced from: /Users/mayuresh.srivastava/.rvm/gems/ruby-2.7.3/gems/ffi-1.15.4/lib/ffi_c.bundle
  Reason: image not found - /Users/mayuresh.srivastava/.rvm/gems/ruby-2.7.3/gems/ffi-1.15.4/lib/ffi_c.bundle

I checked ffi, it was already there:

mayuresh.srivastava$ gem list ffi

*** LOCAL GEMS ***

ffi (1.15.4)
public_suffix (4.0.6)

Still I installed ffi again, and it worked.

mayuresh.srivastava$ gem install ffi
Building native extensions. This could take a while...
Successfully installed ffi-1.15.4
Parsing documentation for ffi-1.15.4
Installing ri documentation for ffi-1.15.4
Done installing documentation for ffi after 11 seconds
1 gem installed
Lustful answered 28/12, 2021 at 8:27 Comment(0)
W
2

I think there is a minor mistake in ffi's regex in C:\Ruby22-x64\lib\ruby\gems\2.2.0\gems\ffi-1.9.8-x64-mingw32\lib\ffi.rb which makes it incorrectly think you are running ruby 2.1.x

it tests for RUBY_VERSION =~ /2.1/ which catches '2.2.1' whereas it should test for RUBY_VERSION =~ /^2.1/ with a start of line character in.

it should be:

if RUBY_VERSION =~ /^1\.8/
      require '1.8/ffi_c'
    elsif RUBY_VERSION =~ /^1\.9/
      require '1.9/ffi_c'
    elsif RUBY_VERSION =~ /^2\.0/
      require '2.0/ffi_c'
    elsif RUBY_VERSION =~ /^2\.1/
      require '2.1/ffi_c'
    elsif RUBY_VERSION =~ /^2\.2/
      require '2.2/ffi_c'
    else
      require 'ffi_c'
    end

I see that it has now been fixed https://github.com/ffi/ffi/commit/4168ef3dbd56a7b52978efb2ff7d0dc448f8f8f1

Winthrop answered 28/4, 2015 at 16:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.