Search code examples
mysqlrubyruby-on-rails-3mysql2

rails server command unable to find libraries


I've installed rails, the mysql2 gem, and mysql and have created a project called simple_cms using rails new simple_cms -d mysql. Now when I execute rails server to start WEBricks, I get this bucket of joy:

/Library/Ruby/Gems/1.8/gems/mysql2-0.3.11/lib/mysql2/mysql2.bundle: dlopen(/Library/Ruby/Gems/1.8/gems/mysql2-0.3.11/lib/mysql2/mysql2.bundle, 9): Library not loaded: /usr/local/mysql/lib/libmysqlclient.16.dylib (LoadError)
  Referenced from: /Library/Ruby/Gems/1.8/gems/mysql2-0.3.11/lib/mysql2/mysql2.bundle
  Reason: image not found - /Library/Ruby/Gems/1.8/gems/mysql2-0.3.11/lib/mysql2/mysql2.bundle
    from /Library/Ruby/Gems/1.8/gems/mysql2-0.3.11/lib/mysql2.rb:9
    from /Library/Ruby/Gems/1.8/gems/bundler-1.0.21/lib/bundler/runtime.rb:68:in `require'
    from /Library/Ruby/Gems/1.8/gems/bundler-1.0.21/lib/bundler/runtime.rb:68:in `require'
    from /Library/Ruby/Gems/1.8/gems/bundler-1.0.21/lib/bundler/runtime.rb:66:in `each'
    from /Library/Ruby/Gems/1.8/gems/bundler-1.0.21/lib/bundler/runtime.rb:66:in `require'
    from /Library/Ruby/Gems/1.8/gems/bundler-1.0.21/lib/bundler/runtime.rb:55:in `each'
    from /Library/Ruby/Gems/1.8/gems/bundler-1.0.21/lib/bundler/runtime.rb:55:in `require'
    from /Library/Ruby/Gems/1.8/gems/bundler-1.0.21/lib/bundler.rb:122:in `require'
    from /Users/tyre77/Dropbox/Broetry In Motion/RailsTestApp/simple_cms/config/application.rb:7
    from /Library/Ruby/Gems/1.8/gems/railties-3.2.1/lib/rails/commands.rb:53:in `require'
    from /Library/Ruby/Gems/1.8/gems/railties-3.2.1/lib/rails/commands.rb:53
    from /Library/Ruby/Gems/1.8/gems/railties-3.2.1/lib/rails/commands.rb:50:in `tap'
    from /Library/Ruby/Gems/1.8/gems/railties-3.2.1/lib/rails/commands.rb:50
    from script/rails:6:in `require'
    from script/rails:6

I have changed my .bash_profile to look like this: (not sure if relevant)

PS1="\u \w$ "
alias ll="ls -lahG"

export PATH="/usr/local/bin:/usr/local/sbin:/usr/local/mysql/bin:/$PATH"


# include .bashrc if it exists
if [ -f $HOME/.bashrc ]; then
    . $HOME/.bashrc
fi

# startup virtualenv-burrito
if [ -f $HOME/.venvburrito/startup.sh ]; then
    . $HOME/.venvburrito/startup.sh
fi

See: Unable to start rails server but my mysql version is different and that doesn't solve it even with changing 0.2.6 to 0.3.11


Solution

  • The issue is that Ruby can't find the libraries necessary to build the MySQL gem, there are many ways to fix this but since it looks like you're just starting up on Ruby, I think it's worth to prepare your environment so you don't see this kind of stuff again, here are the steps:

    First I would recommend you to install Homebrew, with Homebrew installed, install MySQL:

    brew install mysql
    

    This will install MySQL in your machine including the necessary libraries to build the MySQL gem when you need it.

    Then you should to install RVM to install your rubies (before installing RVM you need to run brew install git). Once you install RVM, you can start installing any rubies you would like to.

    And then you ask, why can't you use the built in Ruby? Rails will drop support for the Ruby 1.8.* branch on 4.0 and the community in general uses RVM (or rbenv) to install and manage rubies installed to your machine.