35

I am having a problem installing the mysql2 gem.

This comes up when I do gem install mysql2:

Marks-MacBook-Pro:~ Mark$ gem install mysql2
Building native extensions.  This could take a while...
ERROR:  Error installing mysql2:
ERROR: Failed to build gem native extension.

/Users/useruser/.rvm/rubies/ruby-1.9.2-p136/bin/ruby extconf.rb
checking for rb_thread_blocking_region()... yes
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lm... yes
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lz... yes
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lsocket... no
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lnsl... no
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lmygcc... no
checking for mysql_query() in -lmysqlclient... no
*** 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}/lib
    --with-make-prog
    --without-make-prog
    --srcdir=.
    --curdir
    --ruby=/Users/useruser/.rvm/rubies/ruby-1.9.2-p136/bin/ruby
    --with-mysql-config
    --without-mysql-config
    --with-mysql-dir
    --without-mysql-dir
    --with-mysql-include
    --without-mysql-include=${mysql-dir}/include
    --with-mysql-lib
    --without-mysql-lib=${mysql-dir}/lib
    --with-mysqlclientlib
    --without-mysqlclientlib
    --with-mlib
    --without-mlib
    --with-mysqlclientlib
    --without-mysqlclientlib
    --with-zlib
    --without-zlib
    --with-mysqlclientlib
    --without-mysqlclientlib
    --with-socketlib
    --without-socketlib
    --with-mysqlclientlib
    --without-mysqlclientlib
    --with-nsllib
    --without-nsllib
    --with-mysqlclientlib
    --without-mysqlclientlib
    --with-mygcclib
    --without-mygcclib
    --with-mysqlclientlib
    --without-mysqlclientlib


Gem files will remain installed in /Users/useruser/.rvm/gems/ruby-1.9.2-p136/gems/mysql2-0.2.6 for inspection.
Results logged to /Users/useruser/.rvm/gems/ruby-1.9.2-p136/gems/mysql2-0.2.6/ext/mysql2/gem_make.out

Also when I use rails server this comes up:

Could not find gem 'mysql2 (>= 0, runtime)' in any of the gem sources listed in your Gemfile.

13 Answers 13

63

It seems that it cannot find the MySQL library. Have you installed MySQL?
Try installing it via homebrew:

brew install mysql

or install it via macports or fink or whatever you prefer.

Then try again with:

gem install mysql2
6
  • Thank You! This worked! Now when I start rails server and click "About your application's env..." This comes up: Mysql2::Error Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2) Rails.root: /Users/useruser/example Do you know what that means?
    – Maze
    Mar 23, 2011 at 21:12
  • have you setup the mysql server to run? "mysql.server start" will do the job, and don't forget to configure the access for the root user.
    – ALoR
    Mar 23, 2011 at 21:21
  • This comes up: . ERROR! Manager of pid-file quit without updating file.
    – Maze
    Mar 23, 2011 at 22:57
  • before any operation you have to execute the "mysql_install_db" command and follow the instructions
    – ALoR
    Mar 24, 2011 at 9:46
  • 1
    Hi, I getting this problem: imageshack.us/photo/my-images/857/screenshot20110610at230.png Can you somebody please help me fix it? Thank You! :)
    – Maze
    Jun 10, 2011 at 21:08
60

This command worked for me:

gem install mysql2 -- --srcdir=/usr/local/mysql/include
6
  • 2
    This one works if you've used the binaries supplied from the mysql website to install it on your Mac.
    – gmaliar
    Mar 27, 2013 at 10:15
  • thank you, as Guy stated above, this works if you used the mysql website to install
    – Brenden
    Apr 16, 2013 at 22:19
  • This worked for me too..but i have not used mysql website to install.
    – Siddharth
    Oct 11, 2016 at 21:08
  • 2
    This worked for me, but I used different directory. I used locate mysql | grep include and found another dir. The whole command: gem install mysql2 -v '0.3.21' -- --srcdir=/usr/local/Cellar/mysql/8.0.12/include
    – mario199
    Nov 15, 2018 at 9:44
  • i love you so much
    – cody
    Sep 20, 2021 at 23:32
28

Following command worked for me successfully.

x.x.x = version of mysql2 you want to install.

gem install mysql2 -v 'x.x.x' -- --srcdir=/usr/local/mysql/include
2
  • Thanks, worked for me me after I installed MariaDB 10.2 (via brew) on macOS 10.12.5, Ruby 2.3.4, Rails 5.0.3
    – smile2day
    May 25, 2017 at 9:34
  • 2
    quick note for OSX 10.13.5 and mysql installed through homebrew: --srcdir is: /usr/local/include/mysql
    – fmquaglia
    Jun 22, 2018 at 16:01
20

I don't need MySQL on Mac OS X, because I have MySQL installed on Vagrant box. Therefore, I just installed mysql-connector-c.

brew install mysql-connector-c
gem install mysql2
7

To fix for a manual gem install:

brew install openssl
gem install mysql2 -- --with-opt-dir="$(brew --prefix openssl)"

To fix for all bundle installs:

brew install openssl
bundle config --global build.mysql2 --with-opt-dir="$(brew --prefix openssl)"
bundle install
2
  • Only this one worked for me!
    – SahilW
    Dec 8, 2021 at 0:39
  • Today's life saver!
    – Hegwin
    Aug 27 at 13:44
4

The following worked for me

brew install mysql 
brew install mysql-connector-c
gem install mysql2
4

If you used homebrew to install mysql, brew install mysql,this worked for me:

gem install mysql2 -v 'x.x.x' -- --with-mysql-config=/usr/local/Cellar/mysql/y.y.y/bin/mysql_config

x.x.x = version of the mysql2 gem you want to install
y.y.y = the version of mysql you have installed ls /usr/local/Cellar/mysql to find it.

to get the version of mysql

brew info mysql                                                                                                                                                                                                     
mysql: stable 5.7.19 (bottled)
               ...
/usr/local/Cellar/mysql/5.7.19 (322 files, 233MB) *

               ...

then if you want to install with bundle:

bundle config build.mysql --with-mysql-config=/usr/local/Cellar/mysql/y.y.y/bin/mysql_config
1
  • I love you! 'u save my life :D Sep 11, 2017 at 20:52
3

You'll have to specify some additional environment variables to install this gem on the 64 bit architecture for Mac OSX 10.6:

env ARCHFLAGS="-arch x86_64" gem install mysql2

If you are using rvm you can add this as your default option in the ~/.rvmrc file:

rvm_archflags="-arch x86_64"
4
  • didn't work :( Maybe I am doing it wrong, can you explain a bit more in detail how to do it please?
    – Maze
    Mar 23, 2011 at 19:23
  • Did you get the same error or a different error? If you have your mysql installed somewhere other than the default you might also need to specify other flags like --with-opt-dir as well. Mar 23, 2011 at 19:25
  • I am getting the same error. It says: Gem files will remain installed in /Users/useruser/.rvm/gems/ruby-1.9.2-p136/gems/mysql-2.8.1 for inspection. Maybe that can help? and yes I am using rvm
    – Maze
    Mar 23, 2011 at 19:31
  • I have fixed the problem in the question with help from you and ALoR. Thank you for formatting my question! :)
    – Maze
    Mar 23, 2011 at 21:13
1

I had the exact same issue and errors trying to install mysql2. I thought my mamp install of MySQL would work fine and spent a few hours playing around with paths to get that to work - no success.

Finally came across this post from ALoR and installed a fresh version through homebrew - however - be sure to follow ALL the instructions from homebrew! I missed a few steps and wasted another hour tracking down that problem - here are those instructions: https://stackoverflow.com/a/11061487/1241271

After MySQL was successfully installed, I ran: sudo gem install mysql2 and it worked like a charm.

Hope this helps someone from wasting several hours because they neglected to read the docs (newb mistake).

1

Make sure openssl is installed on Mac via Homebrew.

brew install openssl

Install mysql2 gem.

gem install mysql2 -v '0.5.2' -- --with-ldflags=-L/usr/local/opt/openssl/lib --with-cppflags=-I/usr/local/opt/openssl/i
1

This is what worked for me:

gem install mysql2 -v 'x.x.x' -- --with-ldflags=-L/usr/local/opt/openssl/lib --with-cppflags=-I/usr/local/opt/openssl/include
  • x.x.x: replace it with the mysql2 gem version you want to install
0

I know this has been answered multiple times however, here's what's working for me with: May 20, 2019 OSX Mojave 10.14.4 MySQL Ver 14.14 Distrib 5.7.24, for osx10.14 (x86_64) using EditLine wrapper ruby 2.5.0p0 (2017-12-25 revision 61468) [x86_64-darwin17] Rails 5.2.1

export LIBRARY_PATH=$LIBRARY_PATH:/usr/local/opt/openssl/lib/
gem install mysql2 -- --with-mysql-config=/usr/local/Cellar/mysql\@5.7/5.7.24/bin/mysql_config

change "/usr/local/Cellar/[email protected]/5.7.24/bin/mysql_config" to whatever your current valid path is from "/usr/local/Cellar/mysql"

Hope this helps!

0

I had same problem after upgrading to Catalina (10.15.x)

My Gemfile.lock has defined mysql2-0.3.20, which I could not get installed. But it worked, when I upgraded it to mysql2-0.5.2

gem update mysql2 -- --srcdir=/usr/local/include/mysql/

Be sure that you have installed mysql with brew.

brew install mysql
brew link mysql                                                                                                               

(Linking /usr/local/Cellar/mysql/8.0.18... 95 symlinks created)

However, after this the gem mysql2 was installed, but the bundle update mysql2 didn't work.

But this other answer helped me https://stackoverflow.com/a/39628463/110214

bundle config --local build.mysql2 "--with-ldflags=-L/usr/local/opt/openssl/lib --with-cppflags=-I/usr/local/opt/openssl/include"

bundle update mysql2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.