我有一些问题时,试图安装mysql2宝石为Rails。当我试图通过运行bundle install或gem install mysql2来安装它时,它会给我以下错误:

安装mysql2错误:错误:未能建立gem本地扩展。

我如何解决这个问题并成功安装mysql2?


当前回答

我只是想特别为Mac用户添加这个答案。

在我更新xcode之前,我的服务器运行得非常好。当启动我的rails服务器时,错误显示如下

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)

并且在错误消息的末尾有安装mysql2 gem的建议。所以当我尝试安装它时,我得到了这个问题中提到的错误。我得到的错误如下

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

因此,正如在这篇文章中所建议的那样,我尝试了“酿造安装mysql”,并退出说mysql版本已经安装。但之前有警告说

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

然后我尝试了这个sudo xcodebuild -license,并在最后键入'agree'。您必须是root用户才能同意该许可。

在此之后,我再次尝试bundle安装,然后一切都正常工作。[甚至由于这次xcode更新,我的塔也有问题。]

其他回答

如果你仍然有问题....

试着安装

   sudo apt-get install ruby1.9.1-dev

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

我希望我得到的错误是libmysqlclient.16。Dylib无法找到,因为我们在另一篇文章中找到了(搜索install_name_tool)。

我的$PATH有/usr/local/mysql在里面(这是我的源代码和构建文件所在的位置),所以我有点难住了。如果有人有什么想法,我会在睡了几个小时后再来看看。

我在Windows 7上遇到了这个问题。这显然是一个不兼容的问题,解决方案如下:

从旧的InstantRails安装程序下载libmySQL.dll文件。它可以从InstantRails GitHub回购中获得。

接下来,将该文件复制到Ruby/bin文件夹中。

现在,你可以开始了;)

windows用户: 你可以设置lib和mysql的路径,例如,如果你使用xampp,你可以这样:

gem install mysql2 -- '--with-mysql-lib="C:\xampp\mysql\lib" --withmysql-include="C:\xampp\mysql\include"'

我只是想特别为Mac用户添加这个答案。

在我更新xcode之前,我的服务器运行得非常好。当启动我的rails服务器时,错误显示如下

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)

并且在错误消息的末尾有安装mysql2 gem的建议。所以当我尝试安装它时,我得到了这个问题中提到的错误。我得到的错误如下

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

因此,正如在这篇文章中所建议的那样,我尝试了“酿造安装mysql”,并退出说mysql版本已经安装。但之前有警告说

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

然后我尝试了这个sudo xcodebuild -license,并在最后键入'agree'。您必须是root用户才能同意该许可。

在此之后,我再次尝试bundle安装,然后一切都正常工作。[甚至由于这次xcode更新,我的塔也有问题。]