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.
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Maze
  • 453
  • 2
  • 5
  • 9

13 Answers13

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
Glorfindel
  • 21,988
  • 13
  • 81
  • 109
ALoR
  • 4,904
  • 2
  • 23
  • 25
  • 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 '11 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 '11 at 21:21
  • This comes up: . ERROR! Manager of pid-file quit without updating file. – Maze Mar 23 '11 at 22:57
  • before any operation you have to execute the "mysql_install_db" command and follow the instructions – ALoR Mar 24 '11 at 09:46
  • 1
    Hi, I getting this problem: http://imageshack.us/photo/my-images/857/screenshot20110610at230.png/ Can you somebody please help me fix it? Thank You! :) – Maze Jun 10 '11 at 21:08
  • I had the same issue. 1) ruby <(curl -fsSkL raw.github.com/mxcl/homebrew/go) 2)brew install mysql 3)gem install mysql2 – alexndm Sep 07 '12 at 21:42
59

This command worked for me:

gem install mysql2 -- --srcdir=/usr/local/mysql/include
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Sushma Satish
  • 2,363
  • 2
  • 20
  • 23
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
Aamir
  • 16,329
  • 10
  • 59
  • 65
  • 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 '17 at 09:34
  • 2
    quick note for OSX 10.13.5 and mysql installed through homebrew: --srcdir is: `/usr/local/include/mysql` – fmquaglia Jun 22 '18 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
Igor Shubovych
  • 2,760
  • 1
  • 20
  • 11
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
Sonu Kumar
  • 376
  • 1
  • 3
  • 4
4

The following worked for me

brew install mysql 
brew install mysql-connector-c
gem install mysql2
Mayank Jain
  • 2,995
  • 2
  • 23
  • 19
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
anquegi
  • 11,125
  • 4
  • 51
  • 67
aruanoc
  • 817
  • 1
  • 7
  • 9
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"
Pan Thomakos
  • 34,082
  • 9
  • 88
  • 85
  • 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 '11 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. – Pan Thomakos Mar 23 '11 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 '11 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 '11 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).

Community
  • 1
  • 1
yellowaj
  • 465
  • 5
  • 10
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
Mayur Shah
  • 3,344
  • 1
  • 22
  • 41
BMA88
  • 329
  • 2
  • 2
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
fguillen
  • 36,125
  • 23
  • 149
  • 210
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/mysql@5.7/5.7.24/bin/mysql_config" to whatever your current valid path is from "/usr/local/Cellar/mysql"

Hope this helps!

DevGW
  • 703
  • 1
  • 8
  • 23
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
Jesper Grann Laursen
  • 2,357
  • 1
  • 20
  • 21