2010-08-31 10:54
采纳率: 25%
浏览 697

安装 mysql2时出错: 未能构建 gem 原生扩展

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?


  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

25条回答 默认 最新

  • csdnceshi55
    ~Onlooker 2010-08-31 12:00

    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
    点赞 评论
  • csdnceshi66

    Have you tried using

     gem install mysql -- --with-mysql-lib=/usr/lib/mysql/lib 

    to specify the location of thebase directory as well as the path to the MySQL libraries that are necessary to complete the gem installation?

    Sources: MySQL Gem Install ERROR: Failed to build gem native extension MySQL Forums :: Ruby :: Help needed with installing MySQL binding for Ruby

    点赞 评论
  • csdnceshi79
    python小菜 2010-09-14 15:59

    I was running into this error on my mac and found that I needed to upgrade from mysql 32bit to mysql 64 bit to get this error to go away. I was running OSX 10.6 on an intel macbook pro with ruby 1.9.2 and rails3.0.0

    I also needed to install xcode in order to get unix utilities like "make" that are required to compile the gem.

    once this was done I was able to run gem install mysql and gem install mysql2 without error.

    点赞 评论
  • weixin_41568126
    乱世@小熊 2010-10-16 12:40

    I got this error too. Solved by installing development packages. I'm using arch and it was:

    sudo pacman -S base-devel

    which installed:

    m4, autoconf, automake, bison, fakeroot, flex, libmpc, ppl, cloog-ppl, elfutils, gcc,
    libtool, make, patch, pkg-config

    but I think it actually needed make and gcc. Error output said (on my machine, among other):

    "You have to install development tools first."

    So it was an obvious decision and it helped.

    点赞 评论
  • weixin_41568127
    ?yb? 2011-01-13 08:22

    I got the gem built on Mac OS X 10.6.6 by 1) Ensuring the Developer tools package is installed 2) Downloading the current MySQL package (5.5.8 in my case) from Source 3) Installing the cmake tool from cmake.org 4) Following the instructions in section 2.11 of INSTALL-SOURCE from the mysql distribution files 5) sudo gem install mysql2 -- --srcdir=/usr/local/mysql/include The gem built successfully, but there are two errors in the documentation that rdoc and ri complain about. But now when I try to require 'mysql2' I get a

    LoadError: no such file to load -- mysql2/mysql2

    I was hoping the error I would get was that the libmysqlclient.16.dylib couldn't be found because we figured that out in another post (search for install_name_tool).

    My $PATH has /usr/local/mysql in it (that's where my source and built files are located), so I'm a little stumped. If anyone has any thoughts, I'll check back after a few hours of sleep.

    点赞 评论
  • weixin_41568131
    10.24 2011-01-23 07:44

    I have several computers, 32 and 64 bits processor, they run on Ubuntu Linux, Maverick (10.10) release.

    I had the same problem, and for me, the sudo apt-get install libmysql-ruby libmysqlclient-dev did the job!!!

    点赞 评论
  • csdnceshi75
    衫裤跑路 2011-02-20 09:03

    I had this issue with my windows 7. This is apparently an incompatibility issue and the solution is as follows: Download mysql.dll file from an older InstantRails installer. The Link http://tinyurl.com/24ocp8u will download only the mysql.dll file.(It's link to svn repository of InstantRails). Next, copy that filw into your Ruby/bin folder. Now, you are good to go ;)

    点赞 评论
  • csdnceshi80
    胖鸭 2011-05-25 22:48

    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.

    点赞 评论
  • csdnceshi74
    7*4 2011-05-30 18:45

    If still getting error then follow the steps of mysql2 gem installation on Rails 3 on -


    where most of the user were able to install mysql2 gem.

    点赞 评论
  • csdnceshi79
    python小菜 2011-08-09 01:40

    This solved my problem once in Windows:

    subst X: "C:\Program files\MySQL\MySQL Server 5.5" 
    gem install mysql2 -v 0.x.x --platform=ruby -- --with-mysql-dir=X: --with-mysql-lib=X:\lib\opt 
    subst X: /D
    点赞 评论
  • csdnceshi67
    bug^君 2011-10-20 19:57

    If you are using yum try:

    sudo yum install mysql-devel
    点赞 评论
  • weixin_41568126
    乱世@小熊 2011-12-13 02:49

    After you get the mysql-dev issues corrected, you may need to remove the bad mysql2 install. Look carefully at the messages after $ bundle install. You may need to

    rm -rf vendor/cache/
    rm -rf ./Zentest

    This will clear out the bad mysql2 installation so that a final $ bundle install can create a good one.

    点赞 评论
  • csdnceshi59
    ℙℕℤℝ 2012-02-16 19:44

    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

    --- 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"

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

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

    点赞 评论
  • csdnceshi53
    Lotus@ 2012-10-03 09:36

    download the right version of mysqllib.dll then copy it to ruby bin really works for me. Follow this link plases mysql2 gem compiled for wrong mysql client library

    点赞 评论
  • weixin_41568196
    撒拉嘿哟木头 2013-02-12 02:10

    Got the "You have to install development tools first." error when trying to install the mysql2 gem after upgrading to Mac OS X Mountain Lion. Apparently doing this upgrade removes the command line compilers.

    To fix:

    • I uninstalled my very old version of Xcode (ran the uninstall script in /Developer/Library). Then deleted the /Developer directory.
    • Went to the AppStore and downloaded Xcode.
    • Launched Xcode and went into the Preferences -> Downloads, and installed the command line tools.
    点赞 评论
  • weixin_41568208
    北城已荒凉 2013-08-05 11:32

    If you are still having trouble….

    Try installing

       sudo apt-get install ruby1.9.1-dev
    点赞 评论
  • weixin_41568134
    MAO-EYE 2014-03-18 21:07

    You are getting this problem because you have not install MySql. Before install mysql2 gem. Install MySQL. After that mysql2 gem will install.

    点赞 评论
  • csdnceshi63
    elliott.david 2014-10-01 07:29

    I just wanted to add this answer specifically for Mac Users.

    My server was running perfectly fine until I updated my xcode. The while starting my rails server the error was shown like this

    Gem::Installer::ExtensionBuildError: ERROR: Failed to build gem native extension.
        /Users/user/.rvm/rubies/ruby-1.9.3-p448/bin/ruby extconf.rb --with-mysql-
        checking for rb_thread_blocking_region()... /Users/user/.rvm/rubies/ruby-1.9.3-  
        p448/lib/ruby/1.9.1/mkmf.rb:381:in `try_do': The compiler failed to generate an    
        executable file. (RuntimeError)

    And there was suggestion to install mysql2 gem at the end of the error message. So when i tried installing it I got the error as above mentioned in this question. The error I got is as follows

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

    So as suggested in this post I tried 'brew install mysql' and that quitted saying that mysql version so and so already installed. But there was warning before it saying

    Warning: You have not agreed to the Xcode license.
    Builds will fail! Agree to the license by opening Xcode.app or running:
    xcodebuild -license

    Then I tried this sudo xcodebuild -license and type 'agree' at the end. You have to be root to agree to the license.

    After this, I again tried bundle install and then everything is working fine as normal. [ Even due to this xcode updation, I had problem with my tower also.]

    点赞 评论
  • csdnceshi67
    bug^君 2014-11-29 11:17

    For those using Windows 7 and 64-bit MySQL

    If you want to use 64-bit MySQL, you should follow the instructions here (also here).

    There is also a custom built 64-bit connector that works without hassle.

    点赞 评论
  • csdnceshi79
    python小菜 2015-08-13 05:39

    For windows user: You set the lib and include path of your mysql, for instance, if youre using xampp you can have like this:

    gem install mysql2 -- '--with-mysql-lib="C:\xampp\mysql\lib" --withmysql-include="C:\xampp\mysql\include"'
    点赞 评论
  • csdnceshi53
    Lotus@ 2015-09-30 15:15

    Another way for MacOS users

    If you used "brew" to install mysql:

    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.

    点赞 评论
  • csdnceshi57
    perhaps? 2015-12-28 13:29

    You have to Install some dependencies

    sudo apt-get install libmysql-ruby libmysqlclient-dev
    点赞 评论
  • csdnceshi64
    游.程 2016-12-02 01:51

    Solution only works on Mac OS X

    If you've installed MySQL with homebrew, what worked for me was uninstalling MySQL, and installing MySQL Community Edition via the MySQL website (https://www.mysql.com/).

    After installed, just re-enter the command to gem install mysql2 or if necessary, sudo gem install mysql2, if you are getting permission denied problems.

    点赞 评论
  • csdnceshi62
    csdnceshi62 2018-03-12 10:41

    libmysql-ruby has been phased out and replaced. New command:

     sudo apt-get install ruby-mysql libmysqlclient-dev
    点赞 评论
  • csdnceshi73
    喵-见缝插针 2018-05-30 19:57

    On Debian Stretch the package that worked for me was default-libmysqlclient-dev

    sudo apt-get update && apt-get install -y default-libmysqlclient-dev

    点赞 评论