19 October 2010

gem install mysql [--should-be-easy [--on-a-mac-anyway]]

So after upgrading to Ruby 1.9.2 and Rails 3 all at once on my shiny new Snow Leopard macbook, (a foolish thing to do! One step at a time! Isolate errors!), after patching my rails app all over the place till rails_upgrade stops whining, and after a gentle reminder to sudo gem install mysql, I get this:

=> Booting WEBrick
=> Rails 3.0.0 application starting in development on
=> Call with -d to detach
=> Ctrl-C to shutdown server
[2010-10-18 23:29:34] INFO  WEBrick 1.3.1
[2010-10-18 23:29:34] INFO  ruby 1.9.2 (2010-08-18) [x86_64-darwin10]
[2010-10-18 23:29:34] INFO  WEBrick::HTTPServer#start: pid=8426 port=3000
dyld: lazy symbol binding failed: Symbol not found: _mysql_init
  Referenced from: /opt/local/lib/ruby1.9/gems/1.9.1/gems/mysql-2.8.1/lib/mysql_api.bundle
  Expected in: flat namespace

dyld: Symbol not found: _mysql_init
  Referenced from: /opt/local/lib/ruby1.9/gems/1.9.1/gems/mysql-2.8.1/lib/mysql_api.bundle
  Expected in: flat namespace

Trace/BPT trap

A search for "lazy symbol binding failed: Symbol not found: _mysql_init" yields this, this, and this ... pointing out that you don't get the wonderful mysql gem without a *little* bit of effort ... but:

$ sudo env ARCHFLAGS="-arch i386" gem install mysql -- --with-mysql-dir=/usr/local/mysql --with-mysql-lib=/usr/local/mysql/lib --with-mysql-include=/usr/local/mysql/include --with-mysql-config=/usr/local/mysql/bin/mysql_config
Building native extensions.  This could take a while...
ERROR:  Error installing mysql:
 ERROR: Failed to build gem native extension.

/opt/local/bin/ruby extconf.rb --with-mysql-dir=/usr/local/mysql --with-mysql-lib=/usr/local/mysql/lib --with-mysql-include=/usr/local/mysql/include --with-mysql-config=/usr/local/mysql/bin/mysql_config
checking for mysql_ssl_set()... *** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of
necessary libraries and/or headers.  Check the mkmf.log file for more
details.  You may need configuration options.

Provided configuration options:
/opt/local/lib/ruby1.9/1.9.1/mkmf.rb:368:in `try_do': The complier failed to generate an executable file. (RuntimeError)
You have to install development tools first.
 from /opt/local/lib/ruby1.9/1.9.1/mkmf.rb:435:in `try_link0'
 from /opt/local/lib/ruby1.9/1.9.1/mkmf.rb:440:in `try_link'
 from /opt/local/lib/ruby1.9/1.9.1/mkmf.rb:552:in `try_func'
 from /opt/local/lib/ruby1.9/1.9.1/mkmf.rb:797:in `block in have_func'
 from /opt/local/lib/ruby1.9/1.9.1/mkmf.rb:693:in `block in checking_for'
 from /opt/local/lib/ruby1.9/1.9.1/mkmf.rb:280:in `block (2 levels) in postpone'
 from /opt/local/lib/ruby1.9/1.9.1/mkmf.rb:254:in `open'
 from /opt/local/lib/ruby1.9/1.9.1/mkmf.rb:280:in `block in postpone'
 from /opt/local/lib/ruby1.9/1.9.1/mkmf.rb:254:in `open'
 from /opt/local/lib/ruby1.9/1.9.1/mkmf.rb:276:in `postpone'
 from /opt/local/lib/ruby1.9/1.9.1/mkmf.rb:692:in `checking_for'
 from /opt/local/lib/ruby1.9/1.9.1/mkmf.rb:796:in `have_func'
 from extconf.rb:50:in `
' Gem files will remain installed in /opt/local/lib/ruby1.9/gems/1.9.1/gems/mysql-2.8.1 for inspection. Results logged to /opt/local/lib/ruby1.9/gems/1.9.1/gems/mysql-2.8.1/ext/mysql_api/gem_make.out

"You have to install development tools first." ... thanks dudes ... but sudo port instal mysql-devel tells me Error: Port mysql_devel not found. What are these mysterious development tools?

Someone recommends copying the output of mysql_config --cflags to the ARCHFLAGS env var, but still no go:

$ mysql_config
Usage: /usr/local/mysql/bin/mysql_config [OPTIONS]
        --cflags         [-I/usr/local/mysql/include -Os -arch i386 -fno-common]
        --include        [-I/usr/local/mysql/include]
        --libs           [-L/usr/local/mysql/lib -lmysqlclient -lz -lm]
        --libs_r         [-L/usr/local/mysql/lib -lmysqlclient_r -lz -lm]
        --socket         [/tmp/mysql.sock]
        --port           [3306]
        --version        [5.0.37]
        --libmysqld-libs [-L/usr/local/mysql/lib -lmysqld -lz -lm]

$ sudo env ARCHFLAGS="-Os -arch i386 -fno-common" gem install mysql -- --with-mysql-dir=/usr/local/mysql --with-mysql-lib=/usr/local/mysql/lib --with-mysql-include=/usr/local/mysql/include --with-mysql-config=/usr/local/mysql/bin/mysql_config
all the same errors, all over again

The usual drill, I run these commands a few times to make sure they don't change their minds if I insist politely ... no bacon. But what's the deal with mysql_config5? ... it turns out that it's *not* the same as mysql_config ... it's not the same thing at all, and it has completely different output:

$ mysql_config5
Usage: /opt/local/bin/mysql_config5 [OPTIONS]
        --cflags         [-I/opt/local/include/mysql5/mysql  -pipe -fPIC    -D_P1003_1B_VISIBLE -DSIGNAL_WITH_VIO_CLOSE -DSIGNALS_DONT_BREAK_READ -DIGNORE_SIGHUP_SIGQUIT  -DDONT_DECLARE_CXA_PURE_VIRTUAL]
        --include        [-I/opt/local/include/mysql5/mysql]
        --libs           [-L/opt/local/lib   -L/opt/local/lib/mysql5/mysql -lmysqlclient -L/opt/local/lib -lz -lm  -L/opt/local/lib/ -lssl -lcrypto]
        --libs_r         [-L/opt/local/lib   -L/opt/local/lib/mysql5/mysql -lmysqlclient_r -L/opt/local/lib -lz -lm   -L/opt/local/lib/ -lssl -lcrypto]
        --plugindir      [/opt/local/lib/mysql5/mysql/plugin]
        --socket         [/opt/local/var/run/mysql5/mysqld.sock]
        --port           [0]
        --version        [5.1.51]
        --libmysqld-libs [-L/opt/local/lib   -L/opt/local/lib/mysql5/mysql -lmysqld -ldl  -L/opt/local/lib -lz -lm     -L/opt/local/lib/ -lssl -lcrypto]

This looks rather serious. Let's plug these values into our gem install command and see what happens:

$ sudo env ARCHFLAGS="-I/opt/local/include/mysql5/mysql  -pipe -fPIC    -D_P1003_1B_VISIBLE -DSIGNAL_WITH_VIO_CLOSE -DSIGNALS_DONT_BREAK_READ -DIGNORE_SIGHUP_SIGQUIT  -DDONT_DECLARE_CXA_PURE_VIRTUAL" gem install mysql -- --with-mysql-dir=/usr/local/mysql --with-mysql-lib=/opt/local/lib/mysql5/mysql --with-mysql-include=/opt/local/include/mysql5/mysql --with-mysql-config=/opt/local/bin/mysql_config5
Building native extensions.  This could take a while...
Successfully installed mysql-2.8.1
1 gem installed
Installing ri documentation for mysql-2.8.1...
Installing RDoc documentation for mysql-2.8.1...

Wow! Bingo! Bring on the beer!

Well there you go I hope this helps, or at least gives you some ideas, if you're stuck with Symbol not found: _mysql_init or extconf.rb failed when you can't get your mysql gem to install.


  1. Hi, i'am having the same problem but two things are diferent:
    - i'm using RVM
    $ mysql_config5
    -bash: mysql_config5: command not found

    Any help please.

  2. BINGOO!!!! Wow you saved me my life!
    Simply silky smooth XD

  3. Wow, Mah nice!!! HelpFull!!! Greato des!!! tanku!

  4. Indeed, this works. On my system there was no mysql5, but Fink had installed the mysql-dev under /sw/ and apparently we just need to link the lib, include and config to that folder (/sw/lib/mysql, /sw/include/mysql/ and /sw/bin/mysql_config respectively). In retrospect easy - but it took me almost a full day of work to install this gem.
    Thanks for the tips.