Error installing mysql2: Failed to build gem nativ

2018-12-31 09:19发布

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条回答
回忆,回不去的记忆
2楼-- · 2018-12-31 10:12

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.

查看更多
几人难应
3楼-- · 2018-12-31 10:15

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

查看更多
余生请多指教
4楼-- · 2018-12-31 10:16

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
查看更多
柔情千种
5楼-- · 2018-12-31 10:16

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

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

查看更多
只若初见
6楼-- · 2018-12-31 10:16

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

查看更多
不流泪的眼
7楼-- · 2018-12-31 10:17

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.

查看更多
登录 后发表回答