Using Homebrew OpenSSL with Elixir

If you’re using the latest OpenSSL package from Homebrew, you might come across an error similar to this one from time to time:

==> fast_tls (compile)
Compiled src/fast_tls_app.erl
Compiled src/p1_sha.erl
Compiled src/fast_tls_sup.erl
Compiled src/fast_tls.erl
Compiling c_src/fast_tls_drv.c
c_src/fast_tls_drv.c:21:10: fatal error: 'openssl/err.h' file not found
#include <openssl/err.h>
         ^
1 error generated.
ERROR: compile failed while processing /Users/john/Public/fap/api/api-2.1/deps/fast_tls: rebar_abort
==> api
** (Mix) Could not compile dependency :fast_tls, "/Users/john/.asdf/installs/elixir/1.2.3/.mix/rebar compile skip_deps=true deps_dir="/Users/john/Public/fap/api/api-2.1/_build/dev/lib"" command failed. You can recompile this dependency with "mix deps.compile fast_tls", update it with "mix deps.update fast_tls" or clean it with "mix deps.clean fast_tls"

The reason why this error is happening is because OpenSSL header files required to compile this (in this case) rebar module are in homebrew’s /usr/local/opt directory. We can simply fix this by setting LDFLAGS and CFLAGS before compiling:

env LDFLAGS="-L$(brew --prefix openssl)/lib" CFLAGS="-I$(brew --prefix openssl)/include" mix compile

Simple!

The brew --prefix command will return the correct base directory where openSSL has been installed.