Harish Gopalakrishnan
Harish Gopalakrishnan

Reputation: 1504

rails cannot load such file -- mysql2/mysql2 (LoadError)

I am newbie to ruby on rails i could not find the solution for this error:

rails s

/usr/local/share/gems/gems/mysql2-0.3.13/lib/mysql2.rb:8:in `require': cannot load such file -- mysql2/mysql2 (LoadError)
from /usr/local/share/gems/gems/mysql2-0.3.13/lib/mysql2.rb:8:in `<top (required)>'
from /usr/local/share/gems/gems/bundler-1.3.5/lib/bundler/runtime.rb:72:in `require'
from /usr/local/share/gems/gems/bundler-1.3.5/lib/bundler/runtime.rb:72:in `block (2 levels) in require'
from /usr/local/share/gems/gems/bundler-1.3.5/lib/bundler/runtime.rb:70:in `each'
from /usr/local/share/gems/gems/bundler-1.3.5/lib/bundler/runtime.rb:70:in `block in require'
from /usr/local/share/gems/gems/bundler-1.3.5/lib/bundler/runtime.rb:59:in `each'
from /usr/local/share/gems/gems/bundler-1.3.5/lib/bundler/runtime.rb:59:in `require'
from /usr/local/share/gems/gems/bundler-1.3.5/lib/bundler.rb:132:in `require'
from /home/Harish/Documents/simple_cms/config/application.rb:7:in `<top (required)>'
from /usr/local/share/gems/gems/railties-4.0.0/lib/rails/commands.rb:76:in `require'
from /usr/local/share/gems/gems/railties-4.0.0/lib/rails/commands.rb:76:in `block in <top (required)>'
from /usr/local/share/gems/gems/railties-4.0.0/lib/rails/commands.rb:73:in `tap'
from /usr/local/share/gems/gems/railties-4.0.0/lib/rails/commands.rb:73:in `<top (required)>'
from bin/rails:4:in `require'
from bin/rails:4:in `<main>'

Upvotes: 16

Views: 31918

Answers (11)

Vakmeth
Vakmeth

Reputation: 11

In my case during our migration from MySQL to PostgreSQL, I forgot to remove the thinking-sphinx dependency from the Gemfile. I resolved this by:

  1. Uninstalling the Sphinx gem.
  2. Restart the application.
  3. Verify and enjoy the smooth operation!

So be sure that no other gem depends on mysql2!

Upvotes: 0

Syafil
Syafil

Reputation: 49

Apple silicon M1:

gem install mysql2 -v '0.5' -- --with-opt-dir=$(brew --prefix openssl)

Upvotes: 0

dyang
dyang

Reputation: 473

I battled this for a day when running into it while trying to run bundle exec rake db:migrate after successfully bundle installing my project gems.

For me, mysql2 was trying to load a configuration file that didn't exist. I had already brew installed mysql on my system, and what mysql2 was looking for existed already in the mysql installation. I ran

gem install mysql2 -v 0.5.3 -- --with-mysql-config=/usr/local/Cellar/[email protected]/5.6.42/bin/mysql_config --with-ldflags=-L/usr/local/opt/openssl/lib --with-cppflags=-I/usr/local/opt/openssl/include

to point mysql2 to the correct configuration.

NOTE the version you have of mysql may be different and the version of the gem you want may also be different. ALSO NOTE: the gem install path may be different than your bundle install path. You can also the gem to your bundle directory with

gem install --install-dir /path/to/bundle/directory

OR you can set gem options with your bundle build configuration:

bundle config --local build.mysql2 --with-mysql-config="/usr/local/Cellar/[email protected]/5.6.42/bin/mysql_config" --with-ldflags=-"L/usr/local/opt/openssl/lib" --with-cppflags=-"I/usr/local/opt/openssl/include"

Upvotes: 0

Duke
Duke

Reputation: 7442

sudo installer -pkg /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg -target /

from https://gorails.com/setup/osx/10.14-mojave

Upvotes: 0

Hoang Tam Le
Hoang Tam Le

Reputation: 1

It work for me.

  • gem uninstall mysql2
  • sudo gem install mysql2
  • bundle

Upvotes: -1

Eric
Eric

Reputation: 2549

For Rails 3.2.17 and Ruby 1.9.3-p448 and mysql2 0.3.11, running bundle update mysql2 updated to 0.3.15 and got rid of the error for me.

Upvotes: 6

Neeraj Kumar
Neeraj Kumar

Reputation: 7559

just downgrade the mysql2 version. In my case, I used '0.2.6' version and it worked for me.

  gem 'mysql2', '0.2.6'

with ruby version 2.0.0 and gem version 1.8.25

Upvotes: 0

Neeraj Kumar
Neeraj Kumar

Reputation: 7559

gem uninstall mysql2

gem install mysql2 --platform=ruby

delete the mysql2 gem directory in your rails specific bundle directory and copy paste this newly install mysql2 gem

Upvotes: 16

Harish Gopalakrishnan
Harish Gopalakrishnan

Reputation: 1504

this solved my error:

[root@localhost cms]# su Harish

[Harish@localhost cms]$ bundle install

[Harish@localhost cms]$ rails s

Upvotes: -7

Shan Valleru
Shan Valleru

Reputation: 3121

Ya, It's happening with new version(0.3.13) of mysql2 gem with rails 4.0.0. Deleting the bundle folder under vendor directory, and rerunning bundle install, update fixed this issue for me

cd {Your_RailsApp_Root}/vendor/
rm -rf bundle/
cd {Your_RailsApp_Root}
bundle install
bundle update

Upvotes: 1

Matt McNaughton
Matt McNaughton

Reputation: 216

Do you have the mysql2 gem installed? Check that your gemfile has gem "mysql2" and then run bundle install from the root of your rails app. Also, ensure that you mysql downloaded in your development environment.

Upvotes: 0

Related Questions