MAMP 1.9.5 / MySQL 5.5.9 and Ruby mysql2

Compiling ruby mysql2 native extension against MAMP mysql was always a non-trivial task. It’s solution is well-described in these posts: http://boonedocks.net/mike/… and http://www.beyondcoding.com/….

Unfortunately, the most reecent MAMP version (as of March 2011) has upgraded from MySQL 5.1 to MySQL 5.5. And MySQL, in turn, has switched from GNU Make (with ./configure) to CMake (with cmake .).

Now, the adapted solution goes (assuming Snow Leopard here, minor changes are needed for older versions).
1. Download MAMP sources from http://sourceforge.net/projects/mamp/files/mamp/
2. Mount dmg, copy mysql-5.5.9.tar.gz (the version may change in the future releases) somewhere to your disk (say, Desktop)
3.

$ cd /tmp
$ mv /Users/yourname/Desktop/mysql-5.5.9.tar.gz .
$ tar xf mysql-5.5.9.tar.gz
$ cd mysql-5.5.9
$ sudo port install cmake
$ cmake . -DMYSQL_UNIX_ADDR=/Applications/MAMP/tmp/mysql/mysql.sock -DCMAKE_INSTALL_PREFIX=/Applications/MAMP/Library
$ make -j 3
$ cp libmysql/*.dylib /Applications/MAMP/Library/lib/
$ mkdir -p /Applications/MAMP/Library/include/mysql
$ cp include/* /Applications/MAMP/Library/include/mysql
$ sudo env ARCHFLAGS="-arch x86_64" gem install mysql2 -- --with-mysql-config=/Applications/MAMP/Library/bin/mysql_config
$ sudo install_name_tool -change /tmp/mysql-5.5.9/libmysql/libmysqlclient.16.dylib /Applications/MAMP/Library/lib/libmysqlclient.16.dylib /opt/local/lib/ruby/gems/1.8/gems/mysql2-0.2.6/lib/mysql2/mysql2.bundle
$ rm -rf /tmp/mysql-5.5.9 /tmp/mysql-5.5.9.tar.gz
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
$ cd /tmp
$ mv /Users/yourname/Desktop/mysql-5.5.9.tar.gz .
$ tar xf mysql-5.5.9.tar.gz
$ cd mysql-5.5.9
$ sudo port install cmake
$ cmake . -DMYSQL_UNIX_ADDR=/Applications/MAMP/tmp/mysql/mysql.sock -DCMAKE_INSTALL_PREFIX=/Applications/MAMP/Library
$ make -j 3
$ cp libmysql/*.dylib /Applications/MAMP/Library/lib/
$ mkdir -p /Applications/MAMP/Library/include/mysql
$ cp include/* /Applications/MAMP/Library/include/mysql
$ sudo env ARCHFLAGS="-arch x86_64" gem install mysql2 -- --with-mysql-config=/Applications/MAMP/Library/bin/mysql_config
$ sudo install_name_tool -change /tmp/mysql-5.5.9/libmysql/libmysqlclient.16.dylib /Applications/MAMP/Library/lib/libmysqlclient.16.dylib /opt/local/lib/ruby/gems/1.8/gems/mysql2-0.2.6/lib/mysql2/mysql2.bundle
$ rm -rf /tmp/mysql-5.5.9 /tmp/mysql-5.5.9.tar.gz

Thanks YJ for the install_name_tool fix!

UPD from Torsten Becker: Please note that the latest version of mysql will not work. You need to use mysql (gem?) version 0.2.11

40 Responses to MAMP 1.9.5 / MySQL 5.5.9 and Ruby mysql2

  1. I finally managed to get the gem to refer to MAMP by running one last command:

    sudo install_name_tool -change /tmp/mysql-5.5.9/libmysql/libmysqlclient.16.dylib /Applications/MAMP/Library/lib/libmysqlclient.16.dylib /Library/Ruby/Gems/1.8/gems/mysql2-0.2.7/lib/mysql2/mysql2.bundle

    (or, if you’re using RVM, that last argument should be something like ~/.rvm/gems/ruby-1.9.2-p136/gems/mysql2-0.2.6/lib/mysql2/mysql2.bundle).

    Source: Stack Overflow.

  2. Pingback: Linking mysql gem to MAMP :: yjsoon

  3. Thank you!

  4. Thank you so much! This worked for me and ended a long search for answers.

  5. Hi,
    When I did “sudo port install cmake” it said
    sudo: port: command not found

    any ideas?

    I’m using Apache 2.0.64
    MySQL 5.5.9

  6. Thanks for this. However I ended up uninstalling MAMP and going with the MySQL server from mysql.com. How do I undo procedure you have listed above?

  7. Steven McClain

    This is the closest I’ve gotten to getting Rails 3 to work with MAMP…

    Any idea where mysql2 is being declared as Mysql2? Its my guess that my rake is failing because of the title case that Rails is not expecting. Here is what I see in terminal:

    $ rake db:create
    rake aborted!
    uninitialized constant Mysql2

    The only place I differed from your excellent instructions is with my “tmp” directory name (I used “src”) and I’m using rvm to hold my rails configuration so I did this command instead of your “install-name-tool –change”:

    sudo install_name_tool -change /src/mysql-5.5.9/libmysql/libmysqlclient.16.dylib /Applications/MAMP/Library/lib/libmysqlclient.16.dylib ~/.rvm/gems/ruby-1.9.2-p180/gems/mysql2-0.2.6/lib/mysql2/mysql2.bundle

  8. in the step ‘make -j 3′ I got the following error and has no *.dylib in libsql folder: OS X 10.6.7 MAMP 1.9.6 MySQL 5.5.9 (from MAMP Component 1.9.5)

    [ 1%] [ 1%] Built target gen_lex_hash
    Built target gen_dtrace_header
    [ 6%] Built target edit
    [ 7%] Built target vio
    [ 8%] Built target regex
    [ 8%] [ 15%] Built target mysqlservices
    Built target strings
    [ 15%] Built target mytap
    [ 15%] Built target innochecksum
    [ 15%] Built target comp_sql
    [ 15%] Built target my_safe_process
    [ 15%] Built target GenServerSource
    [ 15%] Built target GenFixPrivs
    [ 31%] Built target mysys
    [ 31%] Built target dbug
    [ 31%] [ 31%] Built target thr_lock
    Built target comp_err
    [ 31%] Built target base64-t
    [ 31%] [ 32%] [ 32%] Built target bitmap-t
    Built target lf-t
    Building C object unittest/mysys/CMakeFiles/my_atomic-t.dir/my_atomic-t.c.o
    [ 32%] [ 32%] Built target my_malloc-t
    Built target my_rdtsc-t
    Scanning dependencies of target my_vsnprintf-t
    Scanning dependencies of target my_print_defaults
    [ 32%] Building C object unittest/mysys/CMakeFiles/my_vsnprintf-t.dir/my_vsnprintf-t.c.o
    [ 32%] Building C object extra/CMakeFiles/my_print_defaults.dir/my_print_defaults.c.o
    /var/folders/VP/VPVgGuS1G1qNkDGP+F4nzk+++TU/-Tmp-//cc7qHbfe.s:596:suffix or operands invalid for add’
    make[2]: *** [unittest/mysys/CMakeFiles/my_atomic-t.dir/my_atomic-t.c.o] Error 1
    make[1]: *** [unittest/mysys/CMakeFiles/my_atomic-t.dir/all] Error 2
    make[1]: *** Waiting for unfinished jobs….
    Linking C executable my_vsnprintf-t
    Linking C executable my_print_defaults
    [ 32%] Built target my_vsnprintf-t
    [ 32%] Built target my_print_defaults
    make: *** [all] Error 2
    mac:~/tmp/mysql-5.5.9 $

  9. Torsten Becker

    Please note that the latest version of mysql will not work. You need to use version mysql version 0.2.11

  10. Worked great! (snow leopard, ruby 1.9.2 in RVM, mamp pro 1.9.5, rails 3.1rc4)
    I use brew instead of ports so:$ sudo port install cmake, is:$ sudo brew install cmake (for the guy with $ sudo port install cmake question, I recommend brew -> http://mxcl.github.com/homebrew/)
    And instead of downloading the whole mamp sources I downloaded mysql-5.5.9.tar.gz from http://downloads.mysql.com/archives.php?p=mysql-5.5&o=other (only 20M instead of 200M)
    What a headache this gem has always been for me!
    Thanks again.

  11. Pingback: Compiling the MySQL2 gem on OS X with any version of MAMP

  12. It doesn’t work for me on Lion and MAMP2.
    The step that fails is make -j 3 with or without -j 3.

    And this is the error I get:
    /var/folders/hb/nswvnhsj39df3gr137sfd78h0000gp/T//ccItA9Xq.s:596:suffix or operands invalid for add’
    make[2]: *** [unittest/mysys/CMakeFiles/my_atomic-t.dir/my_atomic-t.c.o] Error 1
    make[1]: *** [unittest/mysys/CMakeFiles/my_atomic-t.dir/all] Error 2
    make: *** [all] Error 2

    Can you update your article for MAMP2 and Lion?

  13. Beverly Guillermo

    I had the same problem with @Mirko. I am using MAMP 2.0.2 on Lion and I would also get the error on the build trying to run the unit tests for my_atomic-t.dir — so since they are only unit tests, I decided to comment out the Makefile trying to run it.

    Before running make command, Step 7, I edited ~/mysql-5.5.9/CMakeFiles/Makefile2 and commented every line that references the my_atomic-t.dir location. Then, I executed make and it went through all the way.

    Also, I use rvm, and I ran step 11 differently and skipped step 12.

    $ env ARCHFLAGS=”-arch x86_64″
    $ gem install mysql2 -v 0.2.11 — –with-mysql-config=/Applications/MAMP/Library/bin/mysql_config

    I hope that helpful to some people.

  14. Beverly Guillermo

    Nevermind, skipping step 12, I had to change it to match rvm location for the gem.

    $ install_name_tool -change /tmp/mysql-5.5.9/libmysql/libmysqlclient.16.dylib /Applications/MAMP/Library/lib/libmysqlclient.16.dylib ~/.rvm/gems/ruby-1.9.2-p290/gems/mysql2-0.2.11/lib/mysql2/

    • Did the same as Beverly, and it works! Yay, thank you Beverly!

      Step 11:
      $ env ARCHFLAGS=”-arch x86_64″
      $ gem install mysql2 -v 0.2.11 — –with-mysql-config=/Applications/MAMP/Library/bin/mysql_config

      Step 12:
      install_name_tool -change /tmp/mysql-5.5.9/libmysql/libmysqlclient.16.dylib /Applications/MAMP/Library/lib/libmysqlclient.16.dylib ~/.rvm/gems/ruby-1.9.2-p290/gems/mysql2-0.2.11/lib/mysql2/mysql2.bundle

      • Wrote this too soon.
        $ rails server
        => Booting WEBrick
        => Rails 3.0.7 application starting in development on http://0.0.0.0:3000
        => Call with -d to detach
        => Ctrl-C to shutdown server
        Exiting
        /Users/mirko/.rvm/gems/ruby-1.9.2-p290@global/gems/activerecord-3.0.7/lib/active_record/connection_adapters/mysql_adapter.rb:22:in `rescue in mysql_connection’: !!! Missing the mysql2 gem. Add it to your Gemfile: gem ‘mysql2′ (RuntimeError)

        And I do have it in my Gemfile and `bundle install` went ok.
        Never ending story with MAMP and MySQL gem…

        • Ok, the error was a stupid one. I had to specify ‘mysql2′ as adapter in database.yml. However, I get an error more after this.

          $ rails server
          => Booting WEBrick
          => Rails 3.0.7 application starting in development on http://0.0.0.0:3000
          => Call with -d to detach
          => Ctrl-C to shutdown server
          [FATAL] failed to allocate memory

          I’ve already lost a couple of weeks trying to set this up…

  15. Run in to the same issue as @Mirko & @Beverly, followed what they did but now I’m stuck with this:

    [76%] Building CXX object sql/CMakeFiles/sql.dir/sql_acl.cc.o
    make[2]: *** [sql/CMakeFiles/sql.dir/sql_acl.cc.o] Error 1
    make[1]: *** [sql/CMakeFiles/sql.dir/all] Error 2

    Need help.

  16. I installed cmake, if I run just ‘make’ it will build but when Ouse the command given in the initial post

    cmake . -DMYSQL_UNIX_ADDR=/Applications/MAMP/tmp/mysql/…

    I get ‘command not found’

    Anyone know what I’m missing? I’m sure it’s something simple but I’ve been trying for hours with no luck.

    Thanks

  17. I did all the steps above successfully however, I am using RVM so I had to change the path to some things like mentioned above. When I try to run
    $ rake db:create
    rake aborted!

    dlopen(/Users/useracct/.rvm/gems/ruby-1.9.2-p290/gems/mysql2-0.2.11/lib/mysql2/mysql2.bundle, 9): Library not loaded: libmysqlclient.18.dylib
    Referenced from: /Users/useracct/.rvm/gems/ruby-1.9.2-p290/gems/mysql2-0.2.11/lib/mysql2/mysql2.bundle
    Reason: image not found – /Users/useracct/.rvm/gems/ruby-1.9.2-p290/gems/mysql2-0.2.11/lib/mysql2/mysql2.bundle

    any ideas?

  18. When I run this command:
    cp libmysql/*.dylib /Applications/MAMP/Library/lib/

    I get this error:
    No such file or directory

    There are no files in the libmysql directory that end in .dylib. Can I download them somewhere, or is there another step I need to take?

  19. Everything has worked for me except the *.dylib files still reference the old directory “/Users/[name]/Desktop/mysql-5.5.9/libmysql/”. I execute the install_name_tool and it says it works (verify with otool), but when I go to use it, it says library is missing because the old directory is missing.

  20. First of all, thanks for putting time into this tutorial and sharing!

    When i run:
    $ sudo env ARCHFLAGS=”-arch x86_64″ gem install mysql2 — –with-mysql-config=/Applications/MAMP/Library/bin/mysql_config

    I get:
    ERROR: Error installing mysql2:
    ERROR: Failed to build gem native extension.

    with the outpot I attached below. I would be very greatful if anyone can help me with this as I am new to rails and have been trying for ages to get mysql2 installed locally..

    Thanks, Hannes

    OUTPUT:
    /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby extconf.rb –with-mysql-config=/Applications/MAMP/Library/bin/mysql_config
    checking for rb_thread_blocking_region()… no
    checking for rb_wait_for_single_fd()… no
    checking for mysql.h… yes
    checking for errmsg.h… yes
    checking for mysqld_error.h… yes
    creating Makefile

    make
    gcc -I. -I. -I/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/universal-darwin10.0 -I. -DHAVE_MYSQL_H -DHAVE_ERRMSG_H -DHAVE_MYSQLD_ERROR_H -D_XOPEN_SOURCE -D_DARWIN_C_SOURCE -I/Applications/MAMP/Library/include/mysql -fno-omit-frame-pointer -D_P1003_1B_VISIBLE -DSIGNAL_WITH_VIO_CLOSE -DSIGNALS_DONT_BREAK_READ -DIGNORE_SIGHUP_SIGQUIT -DDONT_DECLARE_CXA_PURE_VIRTUAL -fno-common -arch i386 -arch x86_64 -g -Os -pipe -fno-common -DENABLE_DTRACE -fno-common -pipe -fno-common -Wall -funroll-loops -c client.c
    gcc -I. -I. -I/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/universal-darwin10.0 -I. -DHAVE_MYSQL_H -DHAVE_ERRMSG_H -DHAVE_MYSQLD_ERROR_H -D_XOPEN_SOURCE -D_DARWIN_C_SOURCE -I/Applications/MAMP/Library/include/mysql -fno-omit-frame-pointer -D_P1003_1B_VISIBLE -DSIGNAL_WITH_VIO_CLOSE -DSIGNALS_DONT_BREAK_READ -DIGNORE_SIGHUP_SIGQUIT -DDONT_DECLARE_CXA_PURE_VIRTUAL -fno-common -arch i386 -arch x86_64 -g -Os -pipe -fno-common -DENABLE_DTRACE -fno-common -pipe -fno-common -Wall -funroll-loops -c mysql2_ext.c
    ./client.h:20: warning: ‘rb_thread_blocking_region’ defined but not used
    ./client.h:20: warning: ‘rb_thread_blocking_region’ defined but not used
    gcc -I. -I. -I/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/universal-darwin10.0 -I. -DHAVE_MYSQL_H -DHAVE_ERRMSG_H -DHAVE_MYSQLD_ERROR_H -D_XOPEN_SOURCE -D_DARWIN_C_SOURCE -I/Applications/MAMP/Library/include/mysql -fno-omit-frame-pointer -D_P1003_1B_VISIBLE -DSIGNAL_WITH_VIO_CLOSE -DSIGNALS_DONT_BREAK_READ -DIGNORE_SIGHUP_SIGQUIT -DDONT_DECLARE_CXA_PURE_VIRTUAL -fno-common -arch i386 -arch x86_64 -g -Os -pipe -fno-common -DENABLE_DTRACE -fno-common -pipe -fno-common -Wall -funroll-loops -c result.c
    cc -arch i386 -arch x86_64 -pipe -bundle -undefined dynamic_lookup -o mysql2.bundle client.o mysql2_ext.o result.o -L. -L/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib -L. -arch i386 -arch x86_64 -Wl,-rpath,/Applications/MAMP/Library/lib/mysql -lruby -L/Applications/MAMP/Library/lib/mysql -lmysqlclient_r -lz -lm -lpthread -ldl
    ld: library not found for -lmysqlclient_r
    collect2: ld returned 1 exit status
    ld: library not found for -lmysqlclient_r
    collect2: ld returned 1 exit status
    lipo: can’t open input file: /var/tmp//ccrTH7ik.out (No such file or directory)
    make: *** [mysql2.bundle] Error 1

  21. for fix all compile errors
    export CFLAGS=”-m64″ \
    export CXXFLAGS=”-m64″ \
    cmake . -DMYSQL_UNIX_ADDR=/Applications/MAMP/tmp/mysql/mysql.sock -DCMAKE_INSTALL_PREFIX=/Applications/MAMP/Library
    make -j 3

  22. one more
    tar -zxf mysql-5.5.9.tar.gz;
    cd mysql-5.5.9;
    mkdir CleanBuild;
    cd CleanBuild;
    cmake .. -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DCMAKE_OSX_ARCHITECTURES=’i386;x86_64′ -DMYSQL_UNIX_ADDR=/Applications/MAMP/tmp/mysql/mysql.sock -DCMAKE_INSTALL_PREFIX=/Applications/MAMP/Library;
    make;
    install_name_tool -id /Applications/MAMP/Library/lib/libmysqlclient.16.dylib libmysqlclient.16.dylib;
    cp -a libmysql/*.dylib /Applications/MAMP/Library/lib/
    done all works perfect!!!

  23. ohh i forget )
    -DWITH_UNIT_TESTS=OFF

  24. Hi everybody,

    i was following those steps and on ‘make’, it stops on 95%:

    [ 95%] Building C object unittest/mysys/CMakeFiles/my_atomic-t.dir/my_atomic-t.c.o
    /var/folders/vg/dfx75k9s5zj57x7v8f5k1kvc0000gn/T//ccW85g4o.s:596:suffix or operands invalid for `add’
    make[2]: *** [unittest/mysys/CMakeFiles/my_atomic-t.dir/my_atomic-t.c.o] Error 1
    make[1]: *** [unittest/mysys/CMakeFiles/my_atomic-t.dir/all] Error 2
    make: *** [all] Error 2

    could anyone help me?

  25. Hi, i’ve tried all the steps, and they work, apparently. But i’m using 1.9.2-p290, instead of 1.9.3, but keeps creating a 1.9.3 gemset, and the 1.9.2, can’t get mysql2 install.

    i’ll appreciate some help.
    thanks

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Notify me of followup comments via e-mail. You can also subscribe without commenting.