我有一些问题时,试图安装mysql2宝石为Rails。当我试图通过运行bundle install或gem install mysql2来安装它时,它会给我以下错误:
安装mysql2错误:错误:未能建立gem本地扩展。
我如何解决这个问题并成功安装mysql2?
我有一些问题时,试图安装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更新,我的塔也有问题。]
其他回答
我在我的mac上遇到了这个错误,我发现我需要从mysql 32位升级到mysql 64位才能让这个错误消失。我在英特尔macbook pro上运行OSX 10.6, ruby 1.9.2和rails3.0.0
我还需要安装xcode,以获得编译gem所需的诸如“make”之类的unix实用程序。
一旦这样做,我能够运行gem安装mysql和gem安装mysql2没有错误。
在Ubuntu/Debian和其他使用aptitude的发行版上:
sudo apt-get install libmysql-ruby libmysqlclient-dev
libmysql-ruby包已经被ruby-mysql所取代。这就是我找到解决办法的地方。
如果上面的命令不能工作,因为libmysql-ruby无法找到,下面的命令应该足够了:
sudo apt-get install libmysqlclient-dev
在使用yum的Red Hat/CentOS和其他发行版上:
sudo yum install mysql-devel
在Mac OS X和Homebrew上:
brew install mysql
对我来说,这很有帮助:
$ export LDFLAGS="-L/usr/local/opt/openssl/lib"
$ export CPPFLAGS="-I/usr/local/opt/openssl/include"
然后:
gem install mysql2 -v '0.5.2' --source 'https://rubygems.org/' -- --with-cppflags=-I/usr/local/opt/openssl/include --with-ldflags=-L/usr/local/opt/openssl/lib
结果:
Building native extensions with: '--with-cppflags=-I/usr/local/opt/openssl/include --with-ldflags=-L/usr/local/opt/openssl/lib'
This could take a while...
Successfully installed mysql2-0.5.2
Parsing documentation for mysql2-0.5.2
Installing ri documentation for mysql2-0.5.2
Done installing documentation for mysql2 after 0 seconds
1 gem installed
请看这篇文章(警告:里面有日语)。
我有几台电脑,32位和64位处理器,它们运行在Ubuntu Linux, Maverick(10.10)发行版上。
我也有同样的问题,对我来说 安装libmysqlclient-dev 完成了任务!!
我可以看到大多数人已经找到了这个问题的解决方案,这主要是由于但不限于丢失的软件包,这发生在我身上后,我已经清除mysql并重新安装它。我必须运行这个命令来解决我的问题:
安装libmysqlclient-dev
这个命令帮助我解决了我的问题