0

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)
Community
  • 1
  • 1
Leahcim
  • 40,649
  • 59
  • 195
  • 334
  • I believe @robert_rouse meant for you to uninstall the mysql2 gem, which you do have. Run `gem uninstall mysql2` – Eric Hu Jul 26 '11 at 02:33
  • o.k, I uninstalled mysql2 and then reinstalled it, and I got the same problem. It's still looking for mysql2(0.2.6), even though mysql2 0.3.6 is installed "Could not find gem 'mysql2 (~> 0.2.6)' in any of the gem sources listed in your Gemfile." – Leahcim Jul 26 '11 at 02:44
  • are you installing with gem install mysql2? You should install Rails project gems with bundle install (from the root of your project folder), __after__ they've been specified in your Gemfile – Eric Hu Jul 26 '11 at 03:04
  • I think I just did gem install mysql2...I'm a newbie, sorry. I don't understand the rest of your comment – Leahcim Jul 26 '11 at 03:33

2 Answers2

3

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).

Eric Hu
  • 18,048
  • 9
  • 51
  • 67
  • I went to my folder and did Gemfile and go no result. Then I did bundle install and it installed a few things, but when I ran "rails server" after it gave me the error message in the most recent update in my OP – Leahcim Jul 26 '11 at 05:52
  • By open `Gemfile`, I meant edit it, just to take a look at it. Could you update your original post with `bundle list`, please? It'll get mangled by the StackOverflow formatting if you paste it as a comment – Eric Hu Jul 26 '11 at 08:48
  • o.k, i did bundle list and pasted the results in the OP. – Leahcim Jul 26 '11 at 08:53
  • Apparently this is a common issue with MySQL and Rails--I've only used PostgreSQL (and the default sqlite), so I've been unexposed to the problem. However, [this is a great blog post](http://samuel.kadolph.com/2011/04/mysql2-gem-and-mac-os-x-image-not-found/) explaining why you get the error and what you should do. It's a short read and I highly recommend reading through it. It explains the `install_name_tool` approach you saw earlier and why that's a less-than-ideal solution – Eric Hu Jul 26 '11 at 09:17
  • 1
    sorry for the delay, I was travelling. – Leahcim Jul 30 '11 at 13:39
0

Clean out that gem and reinstall it.

Alternatively, RVM + Mac Homebrew for the MySQL installation works wonders.

Robert Rouse
  • 4,801
  • 1
  • 19
  • 19
  • thanks, how do I "clean out that gem". I'm a newbie...is it just "uninstall_name_tool" ? – Leahcim Jul 25 '11 at 05:46
  • Uninstall the gem and make sure the /Library/Ruby/Gems/1.8/gems/mysql2-0.2.6 is gone. – Robert Rouse Jul 25 '11 at 12:44
  • with all due respect, I don't think you understand the problem. The gem mysql2-0.2.6 doesn't exist. When I did the name_tool described in OP, it told the system to look for mysql2-0.2.6 but it doesn't exist so I can't uninstall it. See my updated OP for a list of currently installed gems... – Leahcim Jul 26 '11 at 02:01