Maze
Maze

Reputation: 463

Installing mysql2 gem for Ruby on Rails with Mac OSX 10.6

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.

Upvotes: 36

Views: 50833

Answers (14)

Sushma Satish
Sushma Satish

Reputation: 2433

This command worked for me:

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

Better to find the location of mysql and try command above

$ which mysql                                                                                                                                                                     1 ↵
/usr/local/mysql/include

Upvotes: 65

Shiva
Shiva

Reputation: 12514

This worked for me, try this if other options do not work for you.

  • System config:

    Mac M1 Pro, Mac OS 14.4.1
    
  • First make sure, mysql is installed using brew install mysql

  • Find location of mysql

    $ brew info mysql|grep Cellar                                                 
    
    /opt/homebrew/Cellar/mysql/8.3.0_1 (324 files, 312.9MB) *
    
    
  • Install mysql2 gem manually

    
    gem install mysql2 -v '0.5.6' -- \
    --with-mysql-lib=/opt/homebrew/opt/[email protected]/lib \
    --with-mysql-dir=/opt/homebrew/opt/[email protected] \
    --with-mysql-config=/opt/homebrew/opt/[email protected]/bin/mysql_config \
    --with-mysql-include=/opt/homebrew/opt/[email protected]/include
    
    

    or

    
    gem install mysql2 -v '0.5.6' -- \
    --with-mysql-lib=/opt/homebrew/Cellar/mysql/8.3.0_1/lib \
    --with-mysql-dir=/opt/homebrew/Cellar/mysql/8.3.0_1 \
    --with-mysql-config=/opt/homebrew/Cellar/mysql/8.3.0_1/bin/mysql_config \
    --with-mysql-include=/opt/homebrew/Cellar/mysql/8.3.0_1/include
    
    

Upvotes: 1

fguillen
fguillen

Reputation: 38772

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

Upvotes: 1

ALoR
ALoR

Reputation: 4914

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

Upvotes: 64

Sonu Kumar
Sonu Kumar

Reputation: 396

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

Upvotes: 7

Jesper Grann Laursen
Jesper Grann Laursen

Reputation: 2357

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

Upvotes: 0

DevGW
DevGW

Reputation: 703

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!

Upvotes: 0

BMA88
BMA88

Reputation: 339

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

Upvotes: 1

aruanoc
aruanoc

Reputation: 867

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

Upvotes: 4

Aamir
Aamir

Reputation: 16957

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

Upvotes: 29

Mayank Jain
Mayank Jain

Reputation: 3185

The following worked for me

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

Upvotes: 4

Igor Shubovych
Igor Shubovych

Reputation: 2820

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

Upvotes: 20

yellowaj
yellowaj

Reputation: 465

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).

Upvotes: 1

Pan Thomakos
Pan Thomakos

Reputation: 34340

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"

Upvotes: 3

Related Questions