RubyCrypto / rbnacl

Ruby FFI binding to the Networking and Cryptography (NaCl) library (a.k.a. libsodium)
MIT License
983 stars 85 forks source link

Could not open library 'sodium': dlopen(sodium, 5): image not found. (LoadError) on Apple Silicon M1 #216

Closed OleMchls closed 3 years ago

OleMchls commented 3 years ago

Hey there,

I'm trying to get rbnacl to function on my M1, but I am hitting the following error message:

ole@Oles-MBP-2 test % ruby -v                      
ruby 2.7.2p137 (2020-10-01 revision 5445e04352) [arm64-darwin20]
ole@Oles-MBP-2 test % ruby -e "require 'rbnacl'" 
/Users/ole/.asdf/installs/ruby/2.7.2/lib/ruby/gems/2.7.0/gems/rbnacl-7.1.1/lib/rbnacl/sodium/version.rb:10: warning: already initialized constant RbNaCl::Sodium::Version::MINIMUM_LIBSODIUM_VERSION
/Users/ole/.asdf/installs/ruby/2.7.2/lib/ruby/gems/2.7.0/gems/rbnacl-7.1.1/lib/rbnacl/sodium/version.rb:10: warning: previous definition of MINIMUM_LIBSODIUM_VERSION was here
/Users/ole/.asdf/installs/ruby/2.7.2/lib/ruby/gems/2.7.0/gems/rbnacl-7.1.1/lib/rbnacl/sodium/version.rb:11: warning: already initialized constant RbNaCl::Sodium::Version::MINIMUM_LIBSODIUM_VERSION_FOR_ARGON2
/Users/ole/.asdf/installs/ruby/2.7.2/lib/ruby/gems/2.7.0/gems/rbnacl-7.1.1/lib/rbnacl/sodium/version.rb:11: warning: previous definition of MINIMUM_LIBSODIUM_VERSION_FOR_ARGON2 was here
/Users/ole/.asdf/installs/ruby/2.7.2/lib/ruby/gems/2.7.0/gems/rbnacl-7.1.1/lib/rbnacl/sodium/version.rb:12: warning: already initialized constant RbNaCl::Sodium::Version::MINIMUM_LIBSODIUM_VERSION_FOR_ARGON2ID
/Users/ole/.asdf/installs/ruby/2.7.2/lib/ruby/gems/2.7.0/gems/rbnacl-7.1.1/lib/rbnacl/sodium/version.rb:12: warning: previous definition of MINIMUM_LIBSODIUM_VERSION_FOR_ARGON2ID was here
Traceback (most recent call last):
    2: from -e:1:in `<main>'
    1: from /Users/ole/.asdf/installs/ruby/2.7.2/lib/ruby/2.7.0/rubygems/core_ext/kernel_require.rb:92:in `require'
/Users/ole/.asdf/installs/ruby/2.7.2/lib/ruby/2.7.0/rubygems/core_ext/kernel_require.rb:92:in `require': cannot load such file -- rbnacl (LoadError)
    15: from -e:1:in `<main>'
    14: from /Users/ole/.asdf/installs/ruby/2.7.2/lib/ruby/2.7.0/rubygems/core_ext/kernel_require.rb:156:in `require'
    13: from /Users/ole/.asdf/installs/ruby/2.7.2/lib/ruby/2.7.0/rubygems/core_ext/kernel_require.rb:168:in `rescue in require'
    12: from /Users/ole/.asdf/installs/ruby/2.7.2/lib/ruby/2.7.0/rubygems/core_ext/kernel_require.rb:168:in `require'
    11: from /Users/ole/.asdf/installs/ruby/2.7.2/lib/ruby/gems/2.7.0/gems/rbnacl-7.1.1/lib/rbnacl.rb:12:in `<top (required)>'
    10: from /Users/ole/.asdf/installs/ruby/2.7.2/lib/ruby/2.7.0/rubygems/core_ext/kernel_require.rb:92:in `require'
     9: from /Users/ole/.asdf/installs/ruby/2.7.2/lib/ruby/2.7.0/rubygems/core_ext/kernel_require.rb:92:in `require'
     8: from /Users/ole/.asdf/installs/ruby/2.7.2/lib/ruby/gems/2.7.0/gems/rbnacl-7.1.1/lib/rbnacl/sodium/version.rb:6:in `<top (required)>'
     7: from /Users/ole/.asdf/installs/ruby/2.7.2/lib/ruby/gems/2.7.0/gems/rbnacl-7.1.1/lib/rbnacl/sodium/version.rb:7:in `<module:RbNaCl>'
     6: from /Users/ole/.asdf/installs/ruby/2.7.2/lib/ruby/gems/2.7.0/gems/rbnacl-7.1.1/lib/rbnacl/sodium/version.rb:9:in `<module:Sodium>'
     5: from /Users/ole/.asdf/installs/ruby/2.7.2/lib/ruby/gems/2.7.0/gems/rbnacl-7.1.1/lib/rbnacl/sodium/version.rb:14:in `<module:Version>'
     4: from /Users/ole/.asdf/installs/ruby/2.7.2/lib/ruby/gems/2.7.0/gems/rbnacl-7.1.1/lib/rbnacl/sodium/version.rb:14:in `extend'
     3: from /Users/ole/.asdf/installs/ruby/2.7.2/lib/ruby/gems/2.7.0/gems/rbnacl-7.1.1/lib/rbnacl/sodium.rb:11:in `extended'
     2: from /Users/ole/.asdf/installs/ruby/2.7.2/lib/ruby/gems/2.7.0/gems/ffi-1.14.2/lib/ffi/library.rb:99:in `ffi_lib'
     1: from /Users/ole/.asdf/installs/ruby/2.7.2/lib/ruby/gems/2.7.0/gems/ffi-1.14.2/lib/ffi/library.rb:99:in `map'
/Users/ole/.asdf/installs/ruby/2.7.2/lib/ruby/gems/2.7.0/gems/ffi-1.14.2/lib/ffi/library.rb:145:in `block in ffi_lib': Could not open library 'sodium': dlopen(sodium, 5): image not found. (LoadError)
Could not open library 'libsodium.dylib': dlopen(libsodium.dylib, 5): image not found.
Could not open library 'libsodium.so.18': dlopen(libsodium.so.18, 5): image not found.
Could not open library 'libsodium.so.18.dylib': dlopen(libsodium.so.18.dylib, 5): image not found.
Could not open library 'libsodium.so.23': dlopen(libsodium.so.23, 5): image not found.
Could not open library 'libsodium.so.23.dylib': dlopen(libsodium.so.23.dylib, 5): image not found

While at first, this looks like I don't have libsodium installed, I want to point out that I do have it installed via brew.

ole@Oles-MBP-2 test % brew test libsodium          
==> Testing libsodium
==> /usr/bin/clang test.c -I/opt/homebrew/Cellar/libsodium/1.0.18_1/include -L/opt/homebrew/Cellar/libsodium/1.0.18_1/lib -lsodium -o test
==> ./test

Any pointers or workarounds would be appreciated šŸ‘

tarcieri commented 3 years ago

Haha, oh joy, this is like the third M1-related issue I've seen today šŸ˜‚

The other ones were C extensions with the system ruby so I'm not really sure what the state of things is with FFI on the M1.

Are you using the system ruby or one you installed with e.g. ruby-install? The latter seems way less problematic for C extensions.

OleMchls commented 3 years ago

@tarcieri thanks for your prompt reply. I am afraid the M1 won't go away that fast :D That ruby comes from asdf, which I assume builds it using ruby-build.

tarcieri commented 3 years ago

Huh, interesting. I wonder if there are any relevant upstream issues on FFI itself.

Can you locate the actual .dylib on your filesystem and check that it's named one of the things FFI is trying:

Could not open library 'libsodium.dylib': dlopen(libsodium.dylib, 5): image not found.
Could not open library 'libsodium.so.18': dlopen(libsodium.so.18, 5): image not found.
Could not open library 'libsodium.so.18.dylib': dlopen(libsodium.so.18.dylib, 5): image not found.
Could not open library 'libsodium.so.23': dlopen(libsodium.so.23, 5): image not found.
Could not open library 'libsodium.so.23.dylib': dlopen(libsodium.so.23.dylib, 5): image not found
OleMchls commented 3 years ago

Can you locate the actual .dylib on your filesystem and check that it's named one of the things FFI is trying:

What's the best way to locate it? That's not my particular area of expertise šŸ˜…

OleMchls commented 3 years ago

I found those files

/opt/homebrew/lib/libsodium.dylib
/opt/homebrew/Cellar/libsodium/1.0.18_1/lib/libsodium.dylib

There are even more:

ole@Oles-MBP-2 dnsimple % ls -lhas /opt/homebrew/lib/libsod*  
0 lrwxr-xr-x  1 ole  admin    51B Feb  4 23:04 /opt/homebrew/lib/libsodium.23.dylib -> ../Cellar/libsodium/1.0.18_1/lib/libsodium.23.dylib
0 lrwxr-xr-x  1 ole  admin    44B Feb  4 23:04 /opt/homebrew/lib/libsodium.a -> ../Cellar/libsodium/1.0.18_1/lib/libsodium.a
0 lrwxr-xr-x  1 ole  admin    48B Feb  4 23:04 /opt/homebrew/lib/libsodium.dylib -> ../Cellar/libsodium/1.0.18_1/lib/libsodium.dylib

But I assume I am missing the bit that connects ruby to those, I assume that is .dylib? I just don't know what's next

tarcieri commented 3 years ago

Those look fine, so this seems like it might be some sort of FFI (or homebrew, or FFI + homebrew) issue on the M1. Unfortunately I can't help debug.

OleMchls commented 3 years ago

@tarcieri thanks for putting in the effort. It gave me the first pointers <3