A mind that is stretched by a new experience can never go back to its old dimensions.

Installing mysql2 gem on an EC2 instance

January 24th, 2016 Posted in geek out

I wanted to use the mysql2 ruby gem on an EC2 instance and was having a LOT of trouble. The logs just weren’t useful.

Here is what it looked like:

[ec2-user@ip-172-31-44-229 /]$ sudo gem install mysql2
Building native extensions. This could take a while…
ERROR: Error installing mysql2:
ERROR: Failed to build gem native extension.

/usr/bin/ruby2.0 extconf.rb
checking for ruby/thread.h… *** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers. Check the mkmf.log file for more details. You may
need configuration options.

Provided configuration options:
–with-opt-dir
–without-opt-dir
–with-opt-include
–without-opt-include=${opt-dir}/include
–with-opt-lib
–without-opt-lib=${opt-dir}/lib64
–with-make-prog
–without-make-prog
–srcdir=.
–curdir
–ruby=/usr/bin/ruby2.0
/usr/share/ruby/2.0/mkmf.rb:434:in `try_do’: The compiler failed to generate an executable file. (RuntimeError)
You have to install development tools first.
from /usr/share/ruby/2.0/mkmf.rb:565:in `try_cpp’
from /usr/share/ruby/2.0/mkmf.rb:1044:in `block in have_header’
from /usr/share/ruby/2.0/mkmf.rb:895:in `block in checking_for’
from /usr/share/ruby/2.0/mkmf.rb:340:in `block (2 levels) in postpone’
from /usr/share/ruby/2.0/mkmf.rb:310:in `open’
from /usr/share/ruby/2.0/mkmf.rb:340:in `block in postpone’
from /usr/share/ruby/2.0/mkmf.rb:310:in `open’
from /usr/share/ruby/2.0/mkmf.rb:336:in `postpone’
from /usr/share/ruby/2.0/mkmf.rb:894:in `checking_for’
from /usr/share/ruby/2.0/mkmf.rb:1043:in `have_header’
from extconf.rb:16:in `

Gem files will remain installed in /usr/local/share/ruby/gems/2.0/gems/mysql2-0.4.2 for inspection.
Results logged to /usr/local/share/ruby/gems/2.0/gems/mysql2-0.4.2/ext/mysql2/gem_make.out


The real error is well hidden:

The compiler failed to generate an executable file. (RuntimeError)
You have to install development tools first.

Solution that works for Amazon Linux:
sudo yum install mysql-devel build-essential bison openssl libreadline6 libreadline6-dev curl git-core zlib1g zlib1g-dev libssl-dev libyaml-dev libxml2-dev autoconf libc6-dev ncurses-dev automake libtool
sudo gem install mysql2

Basically install every dev related gem under the sun.

Et voila:

[ec2-user@ip-172-31-44-229 /]$ sudo gem install mysql2Building native extensions. This could take a while…
Successfully installed mysql2-0.4.2
Parsing documentation for mysql2-0.4.2
Installing ri documentation for mysql2-0.4.2
Done installing documentation for mysql2 after 0 seconds
1 gem installed

Post a Comment

CommentLuv badge