Search code examples
ruby-on-railsrubygemsterminalwebrick

Rails: looking for wrong sources in gemfile. how to fix?


Newbie alert...

Trying to fix a problem with my rails webrick server (i.e. get it started), I used an old Stackoverflow answer old answer and did the following command

sudo install_name_tool -change libmysqlclient.16.dylib /usr/local/mysql/lib/libmysqlclient.16.dylib /Library/Ruby/Gems/1.8/gems/mysql2-0.2.6/lib/mysql2/mysql2.bundle

However, I forgot to change the version numbers. For example, my mysql is 0.3.6 (as opposed to the 0.2.6 in the command), and it is also libmysqlclient.18 instead of .16.

Now when I try to start rails server it says

Could not find gem 'mysql2 (~> 0.2.6)' in any of the gem sources listed in your Gemfile.

I tried to rerun this command with the updated versions but it didn't change anything...Indeed, it doesn't even ask me for my root password... Any ideas how I can reverse this problem?

Update -- this is the list of gems produced when I do gem list. The first person who answered this question suggested (in his comments) that I uninstall mysql .0.2.6 but that gem was never installed. When I did the name_tool command described above, it told the system to look for mysql 0.2.6, when I should have told it to look for mysql2-0.3.6.

abstract (1.0.0)
actionmailer (3.0.9, 3.0.7)
actionpack (3.0.9, 3.0.7)
activemodel (3.0.9, 3.0.7)
activerecord (3.0.9, 3.0.7)
activeresource (3.0.9, 3.0.7)
activesupport (3.0.9, 3.0.7)
arel (2.0.10)
builder (2.1.2)
bundler (1.0.14)
erubis (2.6.6)
i18n (0.5.0)
mail (2.2.19)
mime-types (1.16)
mysql2 (0.3.6)
polyglot (0.3.1)
rack (1.2.3)
rack-mount (0.6.14)
rack-test (0.5.7)
rails (3.0.9, 3.0.7)
railties (3.0.9, 3.0.7)
rake (0.9.1)
rdoc (3.8)
rubygems-update (1.8.5)
thor (0.14.6)
treetop (1.4.9)
tzinfo (0.3.27)

Update --after following instructions in Eric Hu's answer, I got this result when I ran rails server from my project folder

2011-07-26 01:49:17 rails s
/Library/Ruby/Gems/1.8/gems/mysql2-0.2.11/lib/mysql2/mysql2.bundle: dlopen(/Library/Ruby/Gems/1.8/gems/mysql2-0.2.11/lib/mysql2/mysql2.bundle, 9): Library not loaded: libmysqlclient.18.dylib (LoadError)
  Referenced from: /Library/Ruby/Gems/1.8/gems/mysql2-0.2.11/lib/mysql2/mysql2.bundle
  Reason: image not found - /Library/Ruby/Gems/1.8/gems/mysql2-0.2.11/lib/mysql2/mysql2.bundle
    from /Library/Ruby/Gems/1.8/gems/mysql2-0.2.11/lib/mysql2.rb:9
    from /Library/Ruby/Gems/1.8/gems/bundler-1.0.14/lib/bundler/runtime.rb:68:in `require'
    from /Library/Ruby/Gems/1.8/gems/bundler-1.0.14/lib/bundler/runtime.rb:68:in `require'
    from /Library/Ruby/Gems/1.8/gems/bundler-1.0.14/lib/bundler/runtime.rb:66:in `each'
    from /Library/Ruby/Gems/1.8/gems/bundler-1.0.14/lib/bundler/runtime.rb:66:in `require'
    from /Library/Ruby/Gems/1.8/gems/bundler-1.0.14/lib/bundler/runtime.rb:55:in `each'
    from /Library/Ruby/Gems/1.8/gems/bundler-1.0.14/lib/bundler/runtime.rb:55:in `require'
    from /Library/Ruby/Gems/1.8/gems/bundler-1.0.14/lib/bundler.rb:120:in `require'
    from /Users/michaelmitchell/Sites/simple_cms/config/application.rb:7
    from /Library/Ruby/Gems/1.8/gems/railties-3.0.9/lib/rails/commands.rb:28:in `require'
    from /Library/Ruby/Gems/1.8/gems/railties-3.0.9/lib/rails/commands.rb:28
    from /Library/Ruby/Gems/1.8/gems/railties-3.0.9/lib/rails/commands.rb:27:in `tap'
    from /Library/Ruby/Gems/1.8/gems/railties-3.0.9/lib/rails/commands.rb:27
    from script/rails:6:in `require'
    from script/rails:6

Update -- I did bundle list as Eric suggested in his comment

Gems included by the bundle:
  * abstract (1.0.0)
  * actionmailer (3.0.9)
  * actionpack (3.0.9)
  * activemodel (3.0.9)
  * activerecord (3.0.9)
  * activeresource (3.0.9)
  * activesupport (3.0.9)
  * arel (2.0.10)
  * builder (2.1.2)
  * bundler (1.0.14)
  * erubis (2.6.6)
  * i18n (0.5.0)
  * mail (2.2.19)
  * mime-types (1.16)
  * mysql2 (0.2.11)
  * polyglot (0.3.1)
  * rack (1.2.3)
  * rack-mount (0.6.14)
  * rack-test (0.5.7)
  * rails (3.0.9)
  * railties (3.0.9)
  * rake (0.9.2)
  * rdoc (3.8)
  * thor (0.14.6)
  * treetop (1.4.9)
  * tzinfo (0.3.29)

Solution

  • I believe you're experiencing confusion over using bundler and RubyGems. When you gem install <gemname>, you're using a RubyGems commands to install the newest version of <gemname>.

    Rails projects come with a gem called bundler, which also manages gems. Bundler builds a separate copy of gems for each Rails project that you create a Gemfile for. If you don't recall making a Gemfile, that's because it's one of the files that rails new <projectname> will generate for you.

    Most likely, you haven't run a bundle install. Before you do, go to the folder for your current Rails project and open up Gemfile, just so you know what's going on. I'm willing to bet there's a line in there like this: gem 'mysql2', '~> 0.2.6'

    To install the right version, go to your project folder in the command line. Type bundle install. Give it a minute and that should be it--try running rails s again.

    If this seems confusing and unnecessary, just remember that when someone updates a gem, they could make it change in ways that would break your program. Bundler does you a service by ensuring that your program is running with specific versions of the gems needed (in this case, mysql2-0.2.6 instead of mysql2-0.3.6).