How to install MySQL gem on Mac OS X

What do I need to install mysql gem on Mac OS X ? I’m getting error “failed to build native extensions”.


Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Install MySQL first, either by using binary or Homebrew.


gem install mysql2


gem install ruby-mysql

Just pick one and you are good to go.


In case you upgraded to MySQL version 5.6.12 on OS X, mysql2 will fail to compile. Solution is to downgrade MySQL as described here:


In case you don’t want to have mysql installed (maybe you use docker to manage your services) but do want to use mysql2 gem, you can instead install mysql-connector-c (using homebrew).

Method 2

Some time back I wrote some instructions on my blog on how to do it in 10.6, but I think it’s the same in 10.5.

I comes down to this command:

ARCHFLAGS="-arch x86_64" gem install mysql -- 

but you need to find the real value for /path/to/mysql_config. My blog post has a few tips, but there’s one very important thing: you must have installed MySQL with headers. The official .pkg installer from MySQL doesn’t IIRC. The best thing to do is to install MySQL with Homebrew (in the post I use examples from a MacPorts install, but that’s because Homebrew wasn’t mature when I wrote it).

If you install MySQL with Homebrew mysql_config will be located at /usr/local/bin/mysql_config and if you install it with MacPorts it will be at /opt/local/lib/mysql5/bin/mysql_config.

I concur with the other answer that suggest that you install the mysql2 gem instead of mysql. I think the command above should work for mysql2 too.

Method 3

if you already have MySQL server running elsewhere on the network and would like to still install the gem without having to install MySQL try the following

brew install mysql-connector-c

followed by

gem install mysql

Method 4

I’m running Mac OS X Lion with Homebrew.

The correct command to install a ruby gem is…

gem install mysql --  --with-mysql-config=/usr/local/bin/mysql_config

Method 5

I’m using Mac OS X Mountain Lion, and installed MySQL with DMG.

gem install do_mysql --  --with-mysql-dir=/usr/local/mysql --with-mysql-lib=/usr/local/mysql/lib --with-mysql-include=/usr/local/mysql/include


sudo ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylib

This works for me 😉

Method 6

For me it did the most easy:

brew install mysql
gem install mysql

Brew is a package manager for Mac OS, a must have. Using it to install mysql first brings everything that the gem command is missing.

Method 7

I’m using Mac OS X 10.10 (Yosemite), and to me worked with the command bellow:

gem install mysql2 --  --with-mysql-config=/usr/local/mysql/bin/mysql_config

Method 8

I installed MySQL with Homebrew and I had to use this to finally be able to upgrade the gem:

gem install mysql2 -- --with-mysql-dir=/usr/local/opt/mysql/

Method 9

I install MySQL with DWG mysql_config will be located at /usr/local/mysql/bin/mysql_config

gem install mysql --  --with mysql-config=/usr/local/mysql/bin/mysql_config

Method 10

I came across this issue while setting up MySQL gem on MacOs Mojave .This worked for me

1 Ensure Xcode command line tools are installed.
xcode-select --install

  1. For me the build was still failing with some header files missing which you have to install
    open /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg

This link pointed to right direction. Can’t compile C program on a Mac after upgrade to Mojave

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Notify of

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x