How to install MySQL and Rails on Osx Snow Leopard
11:53 AMDevelopment, Metodologies, RubyStefano
Hello everyone.
These days I’ve bought a MacBook Pro with Osx Snow Leopard 64-bit pre installed.
Having to develop mainly in Rails and Django on MySQL and PostgreSQL, I’ve found some difficulties to install and properly configure all packages.
The main problems were due to the presence of 32bit libraries and other at 64bit. For example, Python or Ruby (already installed by default) were compiled at 32bit. Everything seems to work best, however, until you try to install the gem “mysql” or the eggs “psycopg2” or “MySQL_python”.
These database drivers, having to be “compiled”, generate many problems of incompatibility between different architectures.
In this first tutorial, we’ll see how to install MySQL and Rails 3.0. In the next we’ll see how to proceed with Django and PostgreSQL.
The approaches taken by me are simply the result of googling and other online tutorials. So I’ll link every resource I’ve used.
Regarding the installation of MySQL, I’ve mainly followed the Hivelogic tutorial.
See below the key points that allowed a successful installation.
Step 1: Set the $PATH environment variable
Open a terminal and set the $PATH environment variable in order to link the correct folders in /usr/local/
1 | mate ~/.profile |
Add, if it does not exist, this line at the bottom of the .profile file:
1 | export PATH="~/bin;/usr/local/bin:/usr/local/sbin:/usr/local/mysql/bin:$PATH" |
and reload the $PATH in this way:
1 | source ~/.profile |
To verify that our $PATH contains the paths set above, type the following command:
1 | echo $PATH |
Step 2: Download MySQL
Create a new folder to download the sources and compile them:
1 2 | mkdir ~/src cd ~/src |
Download the latest version available at the time of writing this tutorial:
1 | curl -O http://mysql.mirrors.pair.com/Downloads/MySQL-5.1/mysql-5.1.47.tar.gz |
Step 3: Compile and Install
Build and install MySQL with the following commands:
1 2 3 4 5 6 7 8 | tar xzvf mysql-5.1.47.tar.gz cd mysql-5.1.47 ./configure --prefix=/usr/local/mysql --with-extra-charsets=complex \ --enable-thread-safe-client --enable-local-infile --enable-shared \ --with-plugins=innobase make sudo make install |
At this point we create a mysql user and a default database.
1 2 3 4 | cd /usr/local/mysql sudo ./bin/mysql_install_db --user=mysql sudo chown -R mysql ./var cd .. |
Then we can create scripts to start and stop the MySQL server using the command line.
We create a bin folder in our home and the following file, also giving execute permissions:
1 2 3 | mkdir bin touch mysqlscript chmod +x mysqlscript |
Edit the file you’ve just created by inserting the following body:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | #!/bin/bash start() { echo -n "Starting MysSQL server" cd /usr/local/mysql ; sudo /usr/local/mysql/bin/mysqld_safe return } stop() { echo -n "Stopping MySQL server" cd /usr/local/mysql ; sudo /usr/local/mysql/bin/mysqladmin -u root -p shutdown return } case "$1" in start) start ;; stop) stop ;; restart) stop start ;; *) echo "Usage: {start|stop|restart}" exit 1 ;; esac exit $? |
At this point you can start the server by typing “mysqlsript start” in the console. To stop the server just type “mysqlscript stop”.
We are now able to install Ruby on Rails. As mentioned at the beginning of the article, system Ruby is compiled at 32bit.
If you want to compile from source at 64bit and place it in /usr/local/ ruby just follow this tutorial.
Since it is my intention to use instead rvm, I’ll settle for a system level Ruby at 32bit and I prefer to compile every new virtual environment at 64bit (from this tutorial)
Therefore proceed as follows:
Step 1: Install rvm
Launch in a console this command and follow the instructions:
1 | bash < <( curl http://rvm.beginrescueend.com/releases/rvm-install-head ) |
Step 2: Install Ruby 1.9.2
Type the following commands and verify that you have version 1.9.2 installed.
1 2 3 | rvm install 1.9.2 -C --with-readline-dir=/opt/local,--build=x86_64-apple-darwin10 rvm 1.9.2 ruby -v |
Step 3: Create a Rails3 gemset
Create a Rails3 gemset and activate it as default:
1 2 | rvm use --create 1.9.2@rails3 rvm 1.9.2@rails3 --default |
Step 4: Install Rails3 and database drivers for SQLite and MySQL
1 2 3 | gem install sqlite3-ruby env ARCHFLAGS="-arch x86_64" gem install mysql -- --with-mysql-config=/usr/local/mysql/bin/mysql_config gem install rails |
In this tutorial we’ve then installed MySQL and Ruby 1.9.2 from source. Now that all the packages have been compiled to 64bit we should not find errors while installing other application’s gems.
Tags: mysql, osx, rails, ruby, snow leopard

















[...] ORIGINAL POST [...]
[...] This post was mentioned on Twitter by Stefano Mancini, DevInterface. DevInterface said: "How to install MySQL and Rails on Osx Snow LeopardCome installare MySQL e Rails su Osx Snow Leopard" – http://bit.ly/9RZqxG #rubytweets [...]
I don’t really like the script that starts and stops MySQL.
The problem is that the terminal console must be kept open.
If someone has a better script (to start and stop manually MySQL) please share.
I would recommend you much easier way to install mysql via Homebrew (http://mxcl.github.com/homebrew/). It also solves your problem with startup script with the help of Mac OS’s launchctl.
why not homebrew? works like a charm
only line to install mysql gem actual
mysql.server start
@Alexey, @Runmen: thanks for your suggestion.
I’ve took a look at MacPorts and Fink but not Homebrew yet.
How can I find a list of supported packages that can be installed? Is this tool as powerful as MacPorts for example?
Thanks!
[...] URL: How to install MySQL and Rails on Osx Snow Leopard | DevInterface Blog leopard, mentioned-on-twitter, post, rails, rails-on-osx, stefano, stefano-mancini, twitter- [...]
@Stefano
Supported “formulas” (packages) can be viewed in its repository: http://github.com/mxcl/homebrew/tree/master/Library/Formula/
What do you mean by saying “powerful” about Macports? I think it depends on what tasks you want to do with its help. Homebrew is just lighter. They are both helpful if you want just to install some packages on Mac. However, Homebrew will do this installing and compiling everything (if possible) with native Mac libraries. Macports will compile everything from sources. And looking at ruby Homebrew formulas is more pleasant than Macports’ make-scripts.
Check out cinderella, it’s sets up ruby (with rvm), mysql, postgres, memcached, node, and a bunch more stuff. It’s awesome:
http://www.atmos.org/cinderella/intro.html
It’s a one liner to install the whole lot. It uses homebrew so when you are done you can still install a bunch of extra homebrew stuff.
To answer your question about homebrew v macports: I think homebrew has less installable software.
You can’t mix and match the architecture like you have with the mysql gem, it doesn’t work. If you have 64 bit MySQL you need 64 bit ruby, 32 bit MySQL requires 32 bit ruby. Stick with one or the other, you’ll run into problems if you try to use both.
@Alexey: thanks, I’ll take a deeper look to homebrew.
@Ciaran: yeah, this should be a nice package to quickly install everything.
@Jamie: I’m not mixing architectures:
at the end of the tutorial I have an 64bit MySQL and a 64bit Ruby (installed with rvm).
I’ve only left the 32bit Ruby provided by the system, but I don’t care because I’ll never use it.
Snow leopard comes with 64 bit ruby out of the box. You can just upgrade rubygems and go. That being said – it’s always a good idea to switch to RVM asap because it makes managing your ruby installs pretty painless.
You also could have grabbed MySQL from the binary on mysql.com. It comes with a preference pane to control it, and it even installs in /usr/local. Homebrew is a nice way to go, but path of least resistence is just installing with the package from mysql.com
@Patrick: In a first time I’ve installed MySQL from 64bit binaries but I’ve got lot of errors while compiling the mysql gem.
So that’s why I’ve copiled MySQL from scratch.
Salve,
sono roberto amministratore del blog
http://linuxfreedomforlive.blogspot.com
intanto complimenti per il blog
sono appena diventato fans del tuo sito nella tua pagina facebook
volevo chiederti se potevi contraccambiare
la mia pagina la trovi
http://www.facebook.com/pages/LinuX-Freedom-for-Live/290943601160
ciao