Ruby, Leopard and gems

In case you have been sleeping in the same cave as Osama Bin Laden, Apple’s new OS X Leopard includes Ruby as a first-class language.

But Apple’s effort to make the language and all it’s extensions universal binaries can cause you some trouble when installing gems that require compilation.

If you’re installing on an Intel machine and see an error like “ld: symbol(s) not found for architecture ppc”, you probably are installing a gem that requires an external library, for which you only have the i386 version. This is a typical situation when installing mysql (as noted in the troubleshooting page of the MacOSForge wiki for Ruby).

After trying several variations, I came out with this solution:

If your installation command was

sudo gem install mysql

you need to run it as

sudo bash -c "ARCHFLAGS='-arch i386' gem install mysql"

sudo env ARCHFLAGS="-arch i386" gem install mysql

There you go… that should be all you need to install the mysql gem on Leopard against MySQL’s prepackaged binaries.

UPDATE: The troubleshooting page has been updated to include an alternative: using “sudo -s” to start a root shell. I still like my one-liner better :-)

UPDATE 2: Using env instead of bash is slightly cleaner.

UPDATE 3: MySQL still has some problems, because the library is pointing to the wrong direction. The quick solution is to create a link to the right place:

sudo ln -s /usr/local/bin/mysql/lib /usr/local/bin/mysql/lib/mysql

18 Responses to “Ruby, Leopard and gems”

  1. Doggie Says:

    “sudo gem install mysql — -arch i386″ might also work.

  2. sd Says:

    Doggie, it doesn’t work, because ruby’s extconf still inserts it’s own flags, so you end up with -arch i386 –more-flags-here -arch ppc -arch i386

  3. lrz Says:

    Alternatively, you can configure the sudoers file to pass ARCHFLAGS ($ sudo visudo).

  4. Liz Says:

    I can’t seem to get mysql itself to work… how did you install mysql on leopard?

  5. ruby mysql gem on Leo, saved. « copy_paste Says:

    […] ruby mysql gem on Leo, saved. October 29th, 2007 http://trac.macosforge.org/projects/ruby/wiki/Troubleshooting http://www.notsostupid.com/blog/2007/10/25/ruby-leopard-and-gems/ […]

  6. Aashish Says:

    I tried using mysql module on Leopard but it still fails. Here is the output. Any thoughts ?

    localhost-3:early-prog $ uname -a
    Darwin localhost-3.local 9.0.0 Darwin Kernel Version 9.0.0: Tue Oct 9 21:35:55 PDT 2007; root:xnu-1228~1/RELEASE_I386 i386
    localhost-3:early-prog $ sudo bash -c “ARCHFLAGS=’-arch i386′ gem install mysql”
    Select which gem to install for your platform (universal-darwin9.0)
    1. mysql 2.7.3 (mswin32)
    2. mysql 2.7.1 (mswin32)
    3. mysql 2.7 (ruby)
    4. mysql 2.6 (ruby)
    5. Skip this gem
    6. Cancel installation
    > 3
    Building native extensions. This could take a while…
    ERROR: While executing gem … (Gem::Installer::ExtensionBuildError)
    ERROR: Failed to build gem native extension.

    ruby extconf.rb install mysql
    checking for mysql_query() in -lmysqlclient… no
    checking for main() in -lm… yes
    checking for mysql_query() in -lmysqlclient… no
    checking for main() in -lz… yes
    checking for mysql_query() in -lmysqlclient… no
    checking for main() in -lsocket… no
    checking for mysql_query() in -lmysqlclient… no
    checking for main() in -lnsl… no
    checking for mysql_query() in -lmysqlclient… no
    *** 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:
    –with-opt-dir
    –without-opt-dir
    –with-opt-include
    –without-opt-include=${opt-dir}/include
    –with-opt-lib
    –without-opt-lib=${opt-dir}/lib
    –with-make-prog
    –without-make-prog
    –srcdir=.
    –curdir
    –ruby=/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby
    –with-mysql-config
    –without-mysql-config
    –with-mysql-dir
    –without-mysql-dir
    –with-mysql-include
    –without-mysql-include=${mysql-dir}/include
    –with-mysql-lib
    –without-mysql-lib=${mysql-dir}/lib
    –with-mysqlclientlib
    –without-mysqlclientlib
    –with-mlib
    –without-mlib
    –with-mysqlclientlib
    –without-mysqlclientlib
    –with-zlib
    –without-zlib
    –with-mysqlclientlib
    –without-mysqlclientlib
    –with-socketlib
    –without-socketlib
    –with-mysqlclientlib
    –without-mysqlclientlib
    –with-nsllib
    –without-nsllib
    –with-mysqlclientlib
    –without-mysqlclientlib

    Gem files will remain installed in /Library/Ruby/Gems/1.8/gems/mysql-2.7 for inspection.
    Results logged to /Library/Ruby/Gems/1.8/gems/mysql-2.7/gem_make.out
    localhost-3:early-prog $

  7. Jon Gretar Says:

    Liz…. Check out the information at http://trac.macosforge.org/projects/ruby/wiki/Troubleshooting

  8. as Says:

    So I see my post was moderated. Any thoughts on why am I seeing those errors ? I don’t think I have to have mysql installed on my system to have ruby load up mysql module ? DO I have to ?

  9. Elliot Temple Says:

    regarding update 3: instead of making a link, why not tell it the right place? here is what i used (with macports install of mysql):

    sudo env ARCHFLAGS=”-arch ppc” gem install mysql — –with-mysql-include=/opt/local/include/mysql5/mysql/ –with-mysql-lib=/opt/local/lib/mysql5/mysql/

    note: if you copy this, change ‘ppc’ as needed

  10. Evan Drake Says:

    I am getting the same issue as Aashish. What to do?

  11. Evan Drake Says:

    Also, it is saying I don’t have permission to the /usr/bin directory. Should I change this, and if so how. Finally, it is telling me that extconf.rb cannot be found. I installed the XCode Tools. How do I get this file, and what happened to it?

  12. Joannou Ng Says:

    This will be helpful to some folks:
    Migrating MySQL 5.0.45 to Mac OS X 10 .5 Leopard http://blog.tomatocheese.com/archives/2007/11/1/migrating_mysql_to_mac_os_x_leopard/

  13. Ben Marklein Says:

    Aashish and Evan - I had the same problem, and this is what finally worked for me:

    sudo bash -c “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”

  14. Chris Chandler Says:

    Step By Step Reinstall, and startup command instructions for MySQL
    http://www.developingchris.com/2007/10/26/mysql-and-os-x-105-leopard/

  15. John Corrigan Says:

    For MacPorts install of MySQL, this worked for me (based on Ben and Elliot above):

    sudo bash -c “ARCHFLAGS=’-arch i386′ gem install mysql — –with-mysql-dir=/opt/local/lib/mysql5 –with-mysql-lib=/opt/local/lib/mysql5/mysql –with-mysql-include=/opt/local/include/mysql5/mysql”

  16. roger pack Says:

    f you have a PPC Mac (I hear some still exist), you’d enter:

    sudo env ARCHFLAGS=”-arch ppc” gem install mysql — –with-mysql-config=/usr/local/mysql/bin/mysql_config

  17. Pbur’s Adventures » Blog Archive » links for 2007-12-29 Says:

    […] Ruby, Leopard and gems A couple of notes about building native gems on leopard (tags: Leopard postgresql mysql ruby osx) Leave a Comment […]

  18. vlad Says:

    none of the above seemed to have worked for me, but this did:
    http://www.schmidp.com/2007/9/29/rails-mysql-and-leopard

Leave a Reply