Tough love between mysql2 Ruby gem and MacOS Mojave

Konrad Oleksiuk
3 min readNov 11, 2018

--

Do you know the worst Ruby developer fear? Here it is:

Fail on installing gem with C dependencies

Too long to read, I just want to install it (TL;DR):

brew tap frnhr/homebrew-mariadb-connector-c-2
brew install frnhr/mariadb-connector-c-2/mariadb-connector-c
env ARCHFLAGS="-arch x86_64" gem install mysql2 -v '0.3.20' -- --with-mysql-config=/usr/local/Cellar/mariadb-connector-c/2.2.2/bin/mariadb_config --with-ldflags=-L/usr/local/opt/openssl/lib --with-cppflags=-I/usr/local/opt/openssl/include

That’s right. Installing gems that do have native dependencies, either it’s mysql2, thrift, ffi or nokogiri, it’s a pain in Ruby.

In my case, I just have received a new laptop. With brand new MacOS Mojave. I set up my basic environment including brew, emacs and rbenv and then I proceeded to clone most important repositories from Github and prepare them.

When it comes to databases, I had no issues — we use all-in-one docker-compose strategy that sets up all services with correct versions that reflect production.

But then, every Ruby repo needs to install libraries. In our case, we use MySQL version 5, which requires mysql2 gem that is locked on 0.3.20. So what now, let’s quickly install MySQL and gem:

$ brew install mysql56
$ gem install mysql2 -v '0.3.20' -- --with-mysql-dir=/usr/local/Cellar/mysql@5.6/5.6.41/

Yay, error. errmsql.hmissing.

I started to dig a bit, if I ever need mysql installation my local machine at all. Why would I need the whole server just to install dependency library?

Then, I realised, there’s the following package available for us: mysql-connector-c . It simply includes all client libraries required by most of packages, such as mysql2 gem in Ruby.

Great, let’s install it via brew and install:

Works?!

I ensured I have MySQL running and then I tried:

😣 That’s unexpected. I googled a bit, but nothing was helpful, except for this thread on Github. Adding also

— with-mysql-config=/usr/local/Cellar/mysql-connector-c/6.1.11/bin/mysql_config

It did not help. Neither additional ldflags and cppflags. And I still could not run any app nor have working mysql2 gem.

In the end, I had one more idea. Try installing maria-db (which is a fork of MySQL) connector, that is also developed by some people and available in 3rd party repositories in brew. I found this information somewhere in deep ruby-related threads.

Of course, it wasn’t that simple. The most recent version of mariadb-connector-c did not support my MySQL version. But hey, I found out that there’s version 2 created by a fellow developer.

Let’s try it!

brew tap frnhr/homebrew-mariadb-connector-c-2
brew install frnhr/mariadb-connector-c-2/mariadb-connector-c
env ARCHFLAGS="-arch x86_64" gem install mysql2 -v '0.3.20' -- --with-mysql-config=/usr/local/Cellar/mariadb-connector-c/2.2.2/bin/mariadb_config --with-ldflags=-L/usr/local/opt/openssl/lib --with-cppflags=-I/usr/local/opt/openssl/include
Not a MySQL library, but mysql2 likes it
Success!

Yep! This worked. After 2–3 hours spent on a research, I finally managed to install mysql2 gem on MacOS Mojave without having MySQL installation at all. That’s something valuable to learn.

Then I had to install thrift, which… also crashed.

--

--

Konrad Oleksiuk

Passionate programmer, currently working for Leadfeeder. I’m trying to make myself comfortable when running 10km. Right now I mostly do Ruby for living.