Precompile GEMs with Native Extensions to use on Heroku
My team was having issues using a GEM that depended on ffi on Heroku due to the necessity of compiling native extensions since the live containers running on the dynos do not contain the right libraries (e.g., ffi headers).
One may be able to solve this problem building a special buildpack which compiles and installs the libraries at build time. Since the dynos images are fixed, I thought a simpler/cleaner solution was to pre-compile the GEMs so that they could be shipped with the code.
Here are the steps I took:
Installed Docker CE for Mac
Latest version did not work on my OS X 10.10
Thanks to the release notes I learned that v17.06 would
Thanks to this Github page I found a direct link to that version
Found and downloaded the container image of last version of the "build" Heroku Stack (heroku-18) with
docker pull heroku/heroku:18-build
Logged in the image as root mapping my system's /tmp to the container's /tmp to easily transfer files between the two
docker image ls docker run -it -v /tmp:/tmp c3b41bb65a22 /bin/bash
Installed gem-compiler
gem install gem-compiler
Fetched and compiled ffi gem
gem fetch ffi gem compile ffi-1.9.25.gem
To test, I copied the compiled library on my Dropbox folder, got a download link, logged on a dyno and fetched/installed it there
heroku run bash --app myapp curl -L -o vendor/ffi-1.9.25.gem 'https://www.dropbox.com/s/...' gem install --local vendor/ffi-1.9.25.gem
then I opened irb and tried it with some code
require 'ffi' module MyLib extend FFI::Library ffi_lib 'c' attach_function :puts, [ :string ], :int end MyLib.puts 'Hello, World using libc!'
Worked!











