dark-panda / ffi-geos

ffi-geos is an implementation of the GEOS Ruby bindings in Ruby via FFI.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

"Couldn't load the GEOS CAPI library" for ARM / M1 processor

noahfpf opened this issue · comments

On a computer with an ARM64 processor (M1 / Apple silicon) I get the following when using ffi-geos (2.2.0) in rails:

Traceback (most recent call last):
	[...]
	 2: from /usr/local/bundle/gems/ffi-geos-2.1.0/lib/ffi-geos.rb:7:in `<top (required)>'
	 1: from /usr/local/bundle/gems/ffi-geos-2.1.0/lib/ffi-geos.rb:51:in `<module:Geos>'
/usr/local/bundle/gems/ffi-geos-2.1.0/lib/ffi-geos.rb:1139:in `<module:FFIGeos>': undefined method `GEOSversion' for Geos::FFIGeos:Module (NoMethodError)
	[...]
	 3: from /usr/local/bundle/gems/ffi-geos-2.1.0/lib/ffi-geos.rb:7:in `<top (required)>'
	 2: from /usr/local/bundle/gems/ffi-geos-2.1.0/lib/ffi-geos.rb:51:in `<module:Geos>'
	 1: from /usr/local/bundle/gems/ffi-geos-2.1.0/lib/ffi-geos.rb:1124:in `<module:FFIGeos>'
/usr/local/bundle/gems/ffi-geos-2.1.0/lib/ffi-geos.rb:1141:in `rescue in <module:FFIGeos>': Couldn't load the GEOS CAPI library. (LoadError)

This is caused by Geos:: FFIGeos.search_paths not including the path /usr/lib/aarch64-linux-gnu, which is where Docker / apt-get installed libgeos-dev:

# ls -l /usr/lib/aarch64-linux-gnu/libgeos*
-rw-r--r-- 1 root root 2103440 Dec 10  2020 /usr/lib/aarch64-linux-gnu/libgeos-3.9.0.so
-rw-r--r-- 1 root root  437146 Dec 10  2020 /usr/lib/aarch64-linux-gnu/libgeos_c.a
lrwxrwxrwx 1 root root      19 Dec 10  2020 /usr/lib/aarch64-linux-gnu/libgeos_c.so -> libgeos_c.so.1.16.2
lrwxrwxrwx 1 root root      19 Dec 10  2020 /usr/lib/aarch64-linux-gnu/libgeos_c.so.1 -> libgeos_c.so.1.16.2
-rw-r--r-- 1 root root  227280 Dec 10  2020 /usr/lib/aarch64-linux-gnu/libgeos_c.so.1.16.2

Two workaround options:

  1. require rgeo before ffi-geos, which I guess provides the library to ffi-geos?
  2. set the GEOS_LIBRARY_PATH env var to /usr/lib/aarch64-linux-gnu

I'm pretty sure the fix would be as simple as:

diff --git a/lib/ffi-geos.rb b/lib/ffi-geos.rb
index c31a4fc..1dd9fd7 100644
--- a/lib/ffi-geos.rb
+++ b/lib/ffi-geos.rb
@@ -56,7 +56,7 @@ module Geos
         elsif FFI::Platform::IS_WINDOWS
           ENV['PATH'].split(File::PATH_SEPARATOR)
         else
-          ['/usr/local/{lib64,lib}', '/opt/local/{lib64,lib}', '/usr/{lib64,lib}', '/usr/lib/{x86_64,i386}-linux-gnu']
+          ['/usr/local/{lib64,lib}', '/opt/local/{lib64,lib}', '/usr/{lib64,lib}', '/usr/lib/{x86_64,i386,aarch64}-linux-gnu']
         end
     end

Let me know if you'd like me to submit a PR, and thanks for the library!

commented

Fixed in d48298f, to be released shortly.

@dark-panda unfortunately this would not be enough for Mac M1 processor, with brew, those libs are installed under /opt/homebrew/Cellar/geos/<version>/lib or /opt/homebrew/lib

A generic solution may be:

"#{`geos-config --prefix`.chomp}/lib"

But this assumes that geos-config is correctly installed..

However, just adding /opt/homebrew/lib to the list seems already good to me :)

FYI, maintainer of rgeo here, thanks for the lib!

Any luck on a clear solution for this?

@dorthwein while waiting you could just:

export GEOS_LIBRARY_PATH=/opt/homebrew/lib
commented

@BuonOmo @dorthwein Please try the homebrew branch at 4219b6d to see if that'll work for you. If it looks good I'll release immediately.

@dark-panda yes it works :)

I faced the same issue, but the homebrew branch worked for me too; thanks! Also on Macbook M1 with homebrew.

Until the fix is released, I use this in my Gemfile:

gem 'ffi-geos', git: 'https://github.com/dark-panda/ffi-geos', branch: 'homebrew'

@dorthwein, your workaround is missing the t in /opt/ but otherwise was a valid alternative to the homebrew branch option; thanks!

@olliebennett t added (was on me). FYI, in a gemfile you could also:

gem 'ffi-geos', github: 'dark-panda/ffi-geos', branch: 'homebrew'
commented

Fixed and released in 2.3.1.

Thanks - we've moved to a Javascript Proxy running turf.js and just fire http requests with payloads for the limited geo work we needed. Sorry! Cheers.