Error installing mysql2: Failed to build gem native extension

here is a solution for the windows users, hope it helps!

Using MySQL with Rails 3 on Windows

  • Install railsinstaller -> www.railsinstaller.org (I installed it to c:\Rails)

  • Install MySQL (I used MySQL 5.5) -> dev.mysql.com/downloads/installer/

--- for mySQL installation ---

If you dont already have these two files installed you might need them to get your MySQL going

vcredist_x86.exe -> http://www.microsoft.com/download/en/details.aspx?id=5555 dotNetFx40_Full_x86_x64.exe -> http://www.microsoft.com/download/en/details.aspx?id=17718

Use default install Developer Machine

-MySQL Server Config-
port: 3306
windows service name: MySQL55
mysql root pass: root (you can change this later)
(username: root)
-MySQL Server Config-

--- for mySQL installation ---


--- Install the mysql2 Gem ---

Important: Do this with Git Bash Command Line(this was installed with railsinstaller) -> start/Git Bash

gem install mysql2 -- '--with-mysql-lib="c:\Program Files\MySQL\MySQL Server 5.5\lib" --with-mysql-include="c:\Program Files\MySQL\MySQL Server 5.5\include"'

Now the gem should have installed correctly

Lastly copy the libmysql.dll file from
C:\Program Files\MySQL\MySQL Server 5.5\lib
to
C:\Rails\Ruby1.9.2\bin

--- Install the mysql2 Gem ---


You will now be able to use your Rails app with MySQL, if you are not sure how to create a Rails 3 app with MySQL read on...


--- Get a Rails 3 app going with MySQL ---

Open command prompt(not Git Bash) -> start/cmd
Navigate to your folder (c:\Sites)
Create new rails app

rails new world

Delete the file c:\Sites\world\public\index.html
Edit the file c:\Sites\world\config\routes.rb
add this line -> root :to => 'cities#index'

Open command prompt (generate views and controllers)

rails generate scaffold city ID:integer Name:string CountryCode:string District:string Population:integer



Edit the file c:\Sites\world\app\models\city.rb to look like this

class City < ActiveRecord::Base
 set_table_name "city"
end

Edit the file c:\Sites\world\config\database.yml to look like this

development:
adapter: mysql2
encoding: utf8
database: world
pool: 5
username: root
password: root
socket: /tmp/mysql.sock

add to gemfile

gem 'mysql2'

Open command prompt windows cmd, not Git Bash(run your app!)
Navigate to your app folder (c:\Sites\world)

rails s

Open your browser here -> http://localhost:3000

--- Get a Rails 3 app going with MySQL ---


I'm on a mac and use homebrew to install open source programs. I did have to install mac Dev tools in order to install homebrew, but after that it was a simple:

brew install mysql

to install mysql. I haven't had a mysql gem problem since.


On Ubuntu/Debian and other distributions using aptitude:

sudo apt-get install libmysql-ruby libmysqlclient-dev

Package libmysql-ruby has been phased out and replaced by ruby-mysql. This is where I found the solution.

If the above command doesn't work because libmysql-ruby cannot be found, the following should be sufficient:

sudo apt-get install libmysqlclient-dev

On Red Hat/CentOS and other distributions using yum:

sudo yum install mysql-devel

On Mac OS X with Homebrew:

brew install mysql