Gem::LoadError for mysql2 gem, but it's already in Gemfile

Ruby on-RailsMysql2Gemfile

Ruby on-Rails Problem Overview


Gem::LoadError
Specified 'mysql2' for database adapter, but the gem is not loaded.
Add `gem 'mysql2'` to your Gemfile

This error occurred while loading the following files:

active_record/base
                             

This is the error I get on running rails server.

The mysql2 gem has been added to the Gemfile as well.

I've done bundle install, and tried restarting the server but still get the error.

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

If you have this error when upgrading to rails 4.2.4 (also with rails 4.1.5) try using this version of mysql2:

gem 'mysql2', '~> 0.3.18'

Apparently mysql2 isn't still compatible with newer version of rails because rails 4.2.4 is pretty new as the time of answering this question by me 8 September 2015 so use the above line in your Gem file and run:

bundle install 

You should be good to go

Solution 2 - Ruby on-Rails

It worked for me when I specified a mysql2 gem version before the newest one (0.4.0). For some reason there is a problem with Rails 4.2.4 and that gem 0.4.0. So, to solve the problem I just specified the previous gem released: 0.3.20 and it worked fine for me!

gem 'mysql2', '~> 0.3.20'
bundle install

You can check all the gems versions here: https://rubygems.org/gems/mysql2/versions

Solution 3 - Ruby on-Rails

Change to

gem 'mysql2', '~> 0.3.18'

in your Gemfile.

This thread on the official mysql2 Github says to do this. You need to declare that version number if you're rails version 4.x.x.

https://github.com/brianmario/mysql2/issues/675

Then run bundle update mysql2.

Solution 4 - Ruby on-Rails

I got the same error after an upgrade to Rails 4.1 and I managed to resolve it by updating mysql2. Run this in your rails app folder:

$ bundle update mysql2

Solution 5 - Ruby on-Rails

This issue may occur if you're using newer version of rails > 4

Do these two simple steps, it will work. Open your Gemfile and find the below line

gem 'mysql2'

replace that line with a specific mysql version like below

gem 'mysql2', '~> 0.3.18'

Now stop the server and run bundle

bundle install

Now restart your server. It should work.

rails s

Solution 6 - Ruby on-Rails

Being Beginner to the ruby i could not figure out the line gem 'mysql2', '~> 0.3.18'

it simply means go to your rails project folder and then there is line for mysql2 it will be like 0.4* so you can change it to gem 'mysql2', '~> 0.3.18'

and as we have new definition, we have to rebuild the dependency so to do that simple command as explained on the top bundle install

Solution 7 - Ruby on-Rails

It doesn't load mysql2 gem because new version of mysql2(0.4.1) gem unable to load the mysql2_adaptor. This is working for me.

gem 'mysql2', '~> 0.3.13'

and run

bundle install

Solution 8 - Ruby on-Rails

I had the same error and this is because Rails 4.1 requires minimum mysql2 version 0.3.13, and maximum compatible with Windows is version 0.3.11.

So I edited file c:\RailsInstaller\Ruby1.9.3\lib\ruby\gems\1.9.1\gems\activerecord-4.1.1\lib\active_record\connection_adapters\mysql2_adapter.rb and changed line gem 'mysql2', '~> 0.3.13' to gem 'mysql2', '~> 0.3.11', and it works so far.

Solution 9 - Ruby on-Rails

Here is how I fixed this:

 bundle config
 bundle config --delete without
 bundle install --deployment --without development test postgres

Credits: https://stackoverflow.com/questions/9765007/how-do-you-undo-bundle-install-without/15145462#15145462

Solution 10 - Ruby on-Rails

It doesn't load mysql2 gem because new version of mysql2 (>= 0.4.0) gem unable to load the mysql2_adaptor. Can you try this?

gem 'mysql2', '~> 0.3.13'

Hopefully, it should work.

Solution 11 - Ruby on-Rails

I solved the problem, installing the mysql2 gem local (gem install mysql2, bundle install) and adding the following line to the Gemfile:

gem 'mysql2'

Setting the mysql2 adapter in database.yml

adapter: mysql2

was also important!

Solution 12 - Ruby on-Rails

I'm brand spanking new to Ruby on Rails and websites but hears what worked for me.

I had to change my gemfile, gem 'mysql2' to gem 'mysql2', '~> 0.3.13' then in rails i typed bundle install then i tried rails s and got errors so then i tried bundle update mysql2 then in rails typed rails s, and it worked

Solution 13 - Ruby on-Rails

I solved the problem, installing the libmysqlclient-dev. sudo aptitude install libmysqlclient-dev and later run bundle.

Solution 14 - Ruby on-Rails

I have previously installed mysql2 0.4.5 but that was giving me this error so i have installed another version of mysql2 by:

gem install mysql2 --version 0.3.20

Hope this solves your problem.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMysoulisinseoulView Question on Stackoverflow
Solution 1 - Ruby on-RailsAmir Hassan AzimiView Answer on Stackoverflow
Solution 2 - Ruby on-Railsrom5jpView Answer on Stackoverflow
Solution 3 - Ruby on-Railssuga_shaneView Answer on Stackoverflow
Solution 4 - Ruby on-RailsvaloView Answer on Stackoverflow
Solution 5 - Ruby on-RailsPrabhakar UndurthiView Answer on Stackoverflow
Solution 6 - Ruby on-RailsMuaaz salagarView Answer on Stackoverflow
Solution 7 - Ruby on-RailssushilprjView Answer on Stackoverflow
Solution 8 - Ruby on-RailsLev LukomskyView Answer on Stackoverflow
Solution 9 - Ruby on-RailsIchView Answer on Stackoverflow
Solution 10 - Ruby on-RailsNeeraj KumarView Answer on Stackoverflow
Solution 11 - Ruby on-RailsatomiccoderView Answer on Stackoverflow
Solution 12 - Ruby on-Railsmatt napperView Answer on Stackoverflow
Solution 13 - Ruby on-RailsFábio CoelhoView Answer on Stackoverflow
Solution 14 - Ruby on-RailsPrashant ChoudharyView Answer on Stackoverflow