Error installing mysql2: Failed to build gem native extension

I am having some problems when trying to install mysql2 gem for Rails. When I try to install it by running bundle install or gem install mysql2 it gives me the following error:

Error installing mysql2: ERROR: Failed to build gem native extension.

How can I fix this and successfully install mysql2 ?


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

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.


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 FilesMySQLMySQL Server 5.5lib" --with-mysql-include="c:Program FilesMySQLMySQL Server 5.5include"'

    Now the gem should have installed correctly

    Lastly copy the libmysql.dll file from
    C:Program FilesMySQLMySQL Server 5.5lib
    to
    C:RailsRuby1.9.2bin

    --- 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:Sitesworldpublicindex.html
    Edit the file c:Sitesworldconfigroutes.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:Sitesworldappmodelscity.rb to look like this

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

    Edit the file c:Sitesworldconfigdatabase.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:Sitesworld)

    rails s
    

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

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

    链接地址: http://www.djcxy.com/p/35926.html

    上一篇: Bundler可以配置为安装gems rdoc吗?

    下一篇: 安装mysql2时出错:无法构建gem本机扩展