10

I have this in the Gemfile:

gem 'mysql2'

But when I run bundle update, I get this error message:

An error occurred while installing mysql2 (0.3.16), and Bundler cannot continue.
Make sure that `gem install mysql2 -v '0.3.16'` succeeds before bundling.

I've tried to move this into the production section, like this:

group :production do
  gem 'mysql2'
end

But after running bundle update, the result is the same. This section is processed only in the production mode, or not?

How to get rid of this error message on localhost?

EDIT: The whole error message:

Gem::Installer::ExtensionBuildError: ERROR: Failed to build gem native extension.

        /Users/radek/.rvm/rubies/ruby-1.9.3-p385/bin/ruby extconf.rb
checking for ruby/thread.h... *** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of
necessary libraries and/or headers.  Check the mkmf.log file for more
details.  You may need configuration options.

Provided configuration options:
    --with-opt-dir
    --with-opt-include
    --without-opt-include=${opt-dir}/include
    --with-opt-lib
    --without-opt-lib=${opt-dir}/lib
    --with-make-prog
    --without-make-prog
    --srcdir=.
    --curdir
    --ruby=/Users/radek/.rvm/rubies/ruby-1.9.3-p385/bin/ruby
/Users/radek/.rvm/rubies/ruby-1.9.3-p385/lib/ruby/1.9.1/mkmf.rb:381:in `try_do': The compiler failed to generate an executable file. (RuntimeError)
You have to install development tools first.
    from /Users/radek/.rvm/rubies/ruby-1.9.3-p385/lib/ruby/1.9.1/mkmf.rb:506:in `try_cpp'
    from /Users/radek/.rvm/rubies/ruby-1.9.3-p385/lib/ruby/1.9.1/mkmf.rb:931:in `block in have_header'
    from /Users/radek/.rvm/rubies/ruby-1.9.3-p385/lib/ruby/1.9.1/mkmf.rb:790:in `block in checking_for'
    from /Users/radek/.rvm/rubies/ruby-1.9.3-p385/lib/ruby/1.9.1/mkmf.rb:284:in `block (2 levels) in postpone'
    from /Users/radek/.rvm/rubies/ruby-1.9.3-p385/lib/ruby/1.9.1/mkmf.rb:254:in `open'
    from /Users/radek/.rvm/rubies/ruby-1.9.3-p385/lib/ruby/1.9.1/mkmf.rb:284:in `block in postpone'
    from /Users/radek/.rvm/rubies/ruby-1.9.3-p385/lib/ruby/1.9.1/mkmf.rb:254:in `open'
    from /Users/radek/.rvm/rubies/ruby-1.9.3-p385/lib/ruby/1.9.1/mkmf.rb:280:in `postpone'
    from /Users/radek/.rvm/rubies/ruby-1.9.3-p385/lib/ruby/1.9.1/mkmf.rb:789:in `checking_for'
    from /Users/radek/.rvm/rubies/ruby-1.9.3-p385/lib/ruby/1.9.1/mkmf.rb:930:in `have_header'
    from extconf.rb:9:in `<main>'


Gem files will remain installed in /Users/radek/.rvm/gems/ruby-1.9.3-p385/gems/mysql2-0.3.16 for inspection.
Results logged to /Users/radek/.rvm/gems/ruby-1.9.3-p385/gems/mysql2-0.3.16/ext/mysql2/gem_make.out
An error occurred while installing mysql2 (0.3.16), and Bundler cannot continue.
Make sure that `gem install mysql2 -v '0.3.16'` succeeds before bundling.

Thank you

user984621
  • 46,344
  • 73
  • 224
  • 412

5 Answers5

26

On Ubuntu, run

sudo apt-get install libmysqlclient-dev

Or, if you use MariaDB, run

sudo apt-get install libmariadbclient-dev
Ri4a
  • 640
  • 7
  • 13
3

Looks like there is no mysql2 gem installed in your machine.As you are using Mac OSX,you should be installing mysql2 gem like this

gem install mysql2 -- --with-mysql-dir=/usr/local/opt/mysql/ 

If the above command did't work,try like this

sudo gem install mysql -- --with-mysql-config=/usr/local/mysql/bin/mysql_config

And after try

bundle install

This should fix the error.

Source

Community
  • 1
  • 1
Pavan
  • 33,316
  • 7
  • 50
  • 76
  • 1
    `brew install mysql` will not install mysql2 gem – Sergio Tulentsev Jun 03 '14 at 07:56
  • @SergioTulentsev What about this then http://stackoverflow.com/questions/11945425/unable-to-install-mysql2-gem-os-x-mountain-lion – Pavan Jun 03 '14 at 07:58
  • That one will not install the _gem_ too. It will install something else. – Sergio Tulentsev Jun 03 '14 at 08:00
  • @SergioTulentsev Yes!If it doesn't install `mysql2 gem`,what else it install? – Pavan Jun 03 '14 at 08:06
  • @SergioTulentsev My appologies! – Pavan Jun 03 '14 at 08:14
  • I keep getting this error /Users/Ben/.rvm/rubies/ruby-2.1.5/lib/ruby/2.1.0/mkmf.rb:456:in `try_do': The compiler failed to generate an executable file. (RuntimeError) You have to install development tools first. Any help possible what to check, I have tried many things suggested, it can't seem to build mysql2 or find these dev tools it needs. Is it possible my mysql setup just doestn' have them – bjm88 Dec 03 '15 at 22:06
  • I had an old mysql installation from home-brew lying around. I ran `brew update` then `brew upgrade mysql` and then was able to get mysql2 to install using your first suggestion. Thanks! – Troy Dec 18 '16 at 20:16
  • 1
    I keep coming back to this great answer. I finally realized I could add this to my bundle configuration by running `bundle config build.mysql2 --with-mysql-dir=/usr/local/opt/mysql/`. This stores it in the bundle config file at ~/.bundle/config for the next time I have to install the mysql gem with bundler. – Troy Sep 30 '17 at 19:46
1

Add this in Gemfile:-

gem 'mysql2', '0.3.16'

Then:-

bundle install

After that try:-

bundle update
Shamsul Haque
  • 2,441
  • 2
  • 18
  • 20
1

I just installed mysql via $ brew install mysql and it fixed the problem.

Catfish
  • 18,876
  • 54
  • 209
  • 353
0

I've ended up with a near-identical error message from attempting to build on a machine without gcc-c++ installed; mkmf is failing to compile a simple c program because the compiler flat out isn't there.

If you're on CentOS, you want sudo yum install gcc-c++. On Ubuntu, it's sudo apt-get install g++. It's a little disappointing that old versions of ruby-devel don't have a dependency on g++.

ymbirtt
  • 1,481
  • 2
  • 13
  • 24