Jason Swett
Jason Swett

Reputation: 45104

How do I install the Rails MySQL adapter?

There's not much more to my question than that. gem install mysql doesn't work and I haven't found anything by Googling.

When I try gem install mysql2, this is what I get. I don't know what to do now.

jason@buster:~/projects/mcif-rails$ gem install mysql2
Building native extensions.  This could take a while...
ERROR:  Error installing mysql2:
        ERROR: Failed to build gem native extension.

/home/jason/.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... yes
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=/home/jason/.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 /home/jason/.rvm/gems/ruby-1.9.2-p136/gems/mysql2-0.2.6 for inspection.
Results logged to /home/jason/.rvm/gems/ruby-1.9.2-p136/gems/mysql2-0.2.6/ext/mysql2/gem_make.out

Upvotes: 10

Views: 28037

Answers (5)

Jonathan Lonowski
Jonathan Lonowski

Reputation: 123493

It looks like you still need to install MySQL's development libraries. These are required for the gem to build successfully on your system.

[Edit] Seems the RoR Wiki is no longer available. But, Ubuntu has offered their own walkthrough which suggests:

sudo apt-get install mysql-server mysql-client
sudo apt-get install libmysql-ruby libmysqlclient-dev
sudo gem install mysql

See http://wiki.rubyonrails.org/database-support/mysql#installation for more detail.

Example: Ubuntu

sudo apt-get install mysql-server mysql-server-5.0 libmysqlclient15off \
    libmysqlclient15-dev mysql-client-5.0 mysql-common

sudo apt-get install libmysql++-dev

sudo gem install mysql

Upvotes: 17

Michael Durrant
Michael Durrant

Reputation: 96494

In 2013, Using Ubuntu 12.04, this worked for me:

sudo apt-get install mysql-client libmysqlclient-dev

bundle install

Upvotes: 1

jasonmklug
jasonmklug

Reputation: 1634

I'll just leave this here:

I ran into a similar problem, then realized I couldn't install the mysql2 gem without having MySQL installed on my development machine (even though I'm only using the mysql2 gem to connect to a remote MySQL server).

::forehead slap::

brew install mysql

then, in my Gemfile:

gem 'mysql2', '~> 0.3.11'

followed by a quick

bundle install

Success!

Upvotes: 10

Kevin Sylvestre
Kevin Sylvestre

Reputation: 38032

If you are running Rails 3 you should use the mysql2 gem. You can install it with:

gem install mysql2

You will need to first install MySQL and any development headers. This will vary across different operating systems. On Ubuntu, you can run:

aptitude install mysql-server 
aptitude install mysql-client
aptitude install mysql-common
aptitude install libmysql-ruby
aptitude install libmysqlclient-dev

If you are creating a new project, use:

rails new sample --database=mysql
cd sample
bundle install

For more details, check out the project repository.

Upvotes: 3

apneadiving
apneadiving

Reputation: 115541

I assume you are working with Rails.

In your Gemfile:

gem 'mysql2'

Then in your terminal:

bundle

Upvotes: 1

Related Questions