Ruby gem mysql2 install failing

88,464

Solution 1

You have to have 64-bit MySQL installed on your machine, plus the build tools you get when you install xcode.

Solution 2

Ubuntu:

sudo apt-get install libmysqlclient-dev  #(mysql development headers)
sudo gem install mysql2 -- --with-mysql-dir=/etc/mysql/

That's it!

Result:

Building native extensions. This could take a while...
Successfully installed mysql2-0.2.6
1 gem installed
Installing ri documentation for mysql2-0.2.6...
Enclosing class/module 'mMysql2' for class Result not known
Enclosing class/module 'mMysql2' for class Client not known
Installing RDoc documentation for mysql2-0.2.6...
Enclosing class/module 'mMysql2' for class Result not known
Enclosing class/module 'mMysql2' for class Client not known

Solution 3

You need the MySQL development headers for mysql2 to properly compile. This is needed, because much of the functionality is written in C and subsequently linked against MySQL.

A solution is to either point the gem installer to your mysql install folder explicitly like so

gem install mysql2 -- --with-mysql-dir=/usr/local/mysql
# or where ever you installed your mysql server to

or by installing a mysql server to a known location (e.g. by using homebrew.) and then installing the gem

# install the mysql server locally
brew install mysql
# install the gem
gem install mysql2

Both approaches require you to have XCode installed to have the required GCC compiler.

Solution 4

With Brew and MySQL installed, I used the following to install the mysql2 gem

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

If you're using Bundler, you can tell bundler about this with command:

bundle config build.mysql2 --with-mysql-config=/usr/local//Cellar/mysql/5.5.10/bin/mysql_config

As documented here: http://gembundler.com/man/bundle-config.1.html

Solution 5

on Mac OSX Moutain Lion, below command worked for me:

gem install mysql2 -- --srcdir=/usr/local/mysql/include
Share:
88,464

Related videos on Youtube

Tanner
Author by

Tanner

Updated on July 05, 2022

Comments

  • Tanner
    Tanner almost 2 years

    When I try to install the mysql2 gem, it fails with no apparent errors. Does anyone know what to do to work around this so mysql2 installs?

    $ sudo gem install mysql2
    Building native extensions.  This could take a while...
    ERROR:  Error installing mysql2:
        ERROR: Failed to build gem native extension.
    
    /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby extconf.rb
    checking for rb_thread_blocking_region()... no
    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=/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/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 /Library/Ruby/Gems/1.8/gems/mysql2-0.2.6 for inspection.
    Results logged to /Library/Ruby/Gems/1.8/gems/mysql2-0.2.6/ext/mysql2/gem_make.out
    
  • Tanner
    Tanner over 13 years
    That solved it. Installed 64-bit MySQL on my computer via the link below and all worked fine. Thanks!
  • Jason
    Jason over 13 years
    The brew solution worked the best for me. If you install the Mac OS X MySQL binary make sure to use the 64bit version.
  • Holger Just
    Holger Just over 13 years
    Unfortunately, apt is not yet available on OSX. But your approach does indeed work on Debian/Ubuntu.
  • Tobias Cohen
    Tobias Cohen about 13 years
    Thanks, the --with-mysql-dir= option was just what I needed to solve my problem.
  • WattsInABox
    WattsInABox over 12 years
    I don't understand how this has so many upvotes when it does not correspond to the question.
  • John
    John over 12 years
    It has so many upvotes because others (like myself) have found this useful. It solved my issue.
  • Abe Petrillo
    Abe Petrillo over 12 years
    Issue is the same problem but on a different OS
  • froderik
    froderik over 11 years
    yep - brew install mysql and then gem install mysql without problems - thx!
  • nruth
    nruth almost 11 years
    This also works if you're using brew versions to install old versions, e.g. brew install mysql51, gem install mysql2 -v '0.3.13' -- --with-mysql-config=/usr/local/Cellar/mysql51/5.1.71/bin/mys‌​ql_config (on mountain lion)
  • hack3r
    hack3r over 10 years
    Install another useful gem - sudo gem install mysql2xxxx -- --with-mysql-dir=/etc/mysql/
  • gregn3
    gregn3 over 10 years
    For me the --with-mysql-dir was not enough, and instead I had to use sudo gem install mysql2 -- --with-mysql-include=/usr/include/mysql/ --with-mysql-lib=/usr/lib/mysql/
  • NullVoxPopuli
    NullVoxPopuli about 10 years
    this helped me install mysql2 on my chromebook. thanks @tunelko!
  • Josh M.
    Josh M. about 10 years
    Please not that on Ubuntu 13, at least, you don't have to specify with-mysql-dir. Just install libmysqlclient-dev and then run bundle install and it should work.
  • Peter Kazazes
    Peter Kazazes almost 10 years
    Ubuntu 14.01 as well!
  • AlexC
    AlexC over 9 years
    @JoshM.: Fantastic tip. Thanks a lot. Works much smoother that way, including on Debian Wheezy.
  • Denys Vitali
    Denys Vitali over 9 years
    libmysqlclient-dev solved my problem in Ubuntu 14.04. Thanks!
  • user2490003
    user2490003 almost 9 years
    For me the --with-mysql-dir was actually causing an issue. I just left that off and let it install to a default directory. I just direct installed a specific version with gem install mysql2 -v '0.3.18'
  • Arunabh Das
    Arunabh Das almost 7 years
    Thanks : This worked for me : ==> sudo gem install mysql2 -- --with-mysql-dir=/usr/local/mysql
  • evgpisarchik
    evgpisarchik over 5 years
    On macOS Mojave as well!
  • kevinluo201
    kevinluo201 about 4 years
    only solution to this strange problem in MacOS!