Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby on Rails - cannot load such file -- mysql2/2.2/mysql2 (LoadError)

I was using SQLite for my Rails project but when I heard that Heroku doesn't support SQLite, I switched to MYSQL.

After switching my database, I started my rails server "rails s" but it gives me the following error:

    C:\Sites\simple_cms>rails server
C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/mysql2-0.3.18-x64-mingw32/lib/mysql2/mysql2.rb:2:in `require'
: cannot load such file -- mysql2/2.2/mysql2 (LoadError)
        from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/mysql2-0.3.18-x64-mingw32/lib/mysql2/mysql2.rb:2
:in `<top (required)>'
        from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/mysql2-0.3.18-x64-mingw32/lib/mysql2.rb:31:in `r
equire'
        from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/mysql2-0.3.18-x64-mingw32/lib/mysql2.rb:31:in `<
top (required)>'
        from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/bundler-1.8.4/lib/bundler/runtime.rb:76:in `requ
ire'
        from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/bundler-1.8.4/lib/bundler/runtime.rb:76:in `bloc
k (2 levels) in require'
        from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/bundler-1.8.4/lib/bundler/runtime.rb:72:in `each
'
        from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/bundler-1.8.4/lib/bundler/runtime.rb:72:in `bloc
k in require'
        from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/bundler-1.8.4/lib/bundler/runtime.rb:61:in `each
'
        from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/bundler-1.8.4/lib/bundler/runtime.rb:61:in `requ
ire'
        from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/bundler-1.8.4/lib/bundler.rb:134:in `require'
        from C:/Sites/simple_cms/config/application.rb:7:in `<top (required)>'
        from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/railties-4.2.0/lib/rails/commands/commands_tasks
.rb:78:in `require'
        from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/railties-4.2.0/lib/rails/commands/commands_tasks
.rb:78:in `block in server'
        from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/railties-4.2.0/lib/rails/commands/commands_tasks
.rb:75:in `tap'
        from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/railties-4.2.0/lib/rails/commands/commands_tasks
.rb:75:in `server'
        from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/railties-4.2.0/lib/rails/commands/commands_tasks
.rb:39:in `run_command!'
        from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/railties-4.2.0/lib/rails/commands.rb:17:in `<top
 (required)>'
        from bin/rails:4:in `require'
        from bin/rails:4:in `<main>'

This is my gemfile:

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.2.0'
# Use mysql as the database for Active Record
gem 'mysql2'

I searched thoroughly, found some answers but it didn't work.

I pasted my libmysql.dll file from Program Files/MySQL Server 5.6 to Ruby/bin but still no luck. I performed the following commands:

gem uninstall mysql2

gem install mysql2 --platform=ruby

I followed this tutorial and still got the same error: Error "...cannot load such file -- mysql2/2.0/mysql2 (LoadError)". On Windows XP with Ruby 2.0.0

I will highly appreciate it if someone will help me out, so that I can deploy it to Heroku.

Thanks.

like image 314
Hassan Yousuf Avatar asked Mar 08 '15 10:03

Hassan Yousuf


2 Answers

The problem is that the mysql2 gem is not installing with its native dependencies. In previous versions of Ruby, this would have triggered the unable to build native gem extension error:

enter image description here

Something has changed in Ruby 2.2 so that you can install the gem without it building the native extension. This does not mean the problem is resolved however - and so you have to ensure you install the gem with the appropriate dependencies.

Tutorial:

  1. Download C-Connector from MYSQL (note your version of Ruby -- if you're using 64 bit, download the 64 bit version)

  2. Unzip / Install the MYSQL C-Connector folder onto your HDD (don't have any spaces in the path)

  3. Run the following command: gem install mysql2 --platform=ruby -- '--with-mysql-dir="C:/your-mysql-connector-path"'

  4. This should install the gem with the files it needs

  5. Then you can copy libmysql.dll from C:/your-mysql-connector-path -> c:/your-ruby-install/bin`

The difference between this install and the previous one will be that this should mention that the native gem extensions are being installed (This could take a while), which suggests that Ruby is looking at the library.

This time, if it installs correctly, it should work for you.

--

If you're still seeing an error, you'll have to uninstall any existing versions of the mysql2 gem on your system:

How to install mysql2.

If you're getting an error, you just need to type gem uninstall mysql2, select all and then repeat the steps above.

like image 97
Richard Peck Avatar answered Sep 29 '22 06:09

Richard Peck


In step 3 above I had to use an extra pair of '-' and no quotes around the path.

gem install mysql2 -- --with-mysql-dir=C:\mysql-connector-dir

I'm using Win 7

like image 23
mvanio Avatar answered Sep 29 '22 04:09

mvanio