LinkedIn Facebook Twitter


Ruby on Rails With Apache/MySQL on Mac OS X (Snow Leopard)

January 18th, 2011 | Development , Ruby , Tips and Tricks , Tutorials

Install a Path Line Using Text Editor

Such as TextMate, TextWrangler, BBEdit or vi (My favorite is TextMate and “ mate” command or just use “vim” command).

Remove Mysql 5.5 if you have (Mysql 5.1 is the most stable version)

For some reason I faced some problems with MySQL 5.5 so I installed the most stable version : MySQL 5.1 64 Bit .
Use mysqldump to backup your databases to text files! After that :
sudo /usr/local/mysql/support-files/mysql.server stop
sudo rm /usr/local/mysql
sudo rm -rf /usr/local/mysql*
sudo rm -rf /Library/StartupItems/MySQLCOM
sudo rm -rf /Library/PreferencePanes/My*
rm -rf ~/Library/PreferencePanes/My*
sudo rm -rf /Library/Receipts/mysql*
sudo rm -rf /Library/Receipts/MySQL*
sudo rm -rf /var/db/receipts/com.mysql.*
mate /etc/hostconfig

Remove the line MYSQLCOM=-YES-

Download and install Mysql 5.1 [Mac OS X ver. 10.6 (x86, 64-bit), DMG Archive]


Configure Path , download and Install Macports

mate ~/.bash_profile
Add the following to the end of file and save
export PATH=/opt/local/bin:/opt/local/sbin:$PATH
export MANPATH=/opt/local/share/man:$MANPATH


Download and Install Ruby

sudo port -v install ruby

Download , Install and Update RubyGems

mkdir ~/src
cd ~/src
curl -O http://production.cf.rubygems.org/rubygems/rubygems-1.3.7.tgz
tar xzvf rubygems-1.3.7.tgz
cd rubygems-1.3.7
sudo ruby setup.rb
sudo gem update --system

Install rails, rake, bundle, rspec etc.

sudo gem install rake rails thin tzinfo capistrano ruby-debug rspec

Install Mysql Gem

sudo env ARCHFLAGS="-arch x86_64" gem install --no-rdoc --no-ri mysql -- --with-mysql-config=/usr/local/mysql/bin/mysql_config
You can use sudo gem uninstall mysql to uninstall

Test mysql gem

irb > require 'rubygems'
irb > require 'mysql'
irb > Mysql.new('localhost', 'root', 'password', 'test')

Install Sqlite gem

sudo gem install sqlite3-ruby

Install Passenger gem

sudo gem install passenger

Build passenger for apache

(Just hit enter on prompts)
sudo passenger-install-apache2-module

Include Passenger config file

mate /private/etc/apache2/httpd.conf
Add the following to the end of file and save
Include /private/etc/apache2/extra/httpd-passenger.conf

Create Passenger config file for Apache:

My Passenger version is : 3.0.2 so you should edit the code below if yours is a different version
mate /etc/apache2/extra/httpd-passenger.conf
Add the following to the file and save (Change passenger directory string if your have a different version. Learn it by typing passenger-config --root)
LoadModule passenger_module /opt/local/lib/ruby/gems/1.8/gems/passenger-3.0.2/ext/apache2/mod_passenger.so
PassengerRoot /opt/local/lib/ruby/gems/1.8/gems/passenger-3.0.2
PassengerRuby /opt/local/bin/ruby
PassengerMaxPoolSize 6
PassengerMaxInstancesPerApp 2
RailsFrameworkSpawnerIdleTime 1800
RailsAppSpawnerIdleTime 600
PassengerPoolIdleTime 600
PassengerMaxRequests 1000

NameVirtualHost *:80

<VirtualHost *:80>
DocumentRoot "/Users/goksel/Sites/rails_app/public"
ServerName rails_app
<Directory /Users/goksel/Sites/rails_app/public>
Options -MultiViews
AllowOverride All

Add your local domain to hosts:

mate /etc/hosts
Add the following to the end of file and save rails_app

Restart Apache

apachectl configtest
(If you see “Syntax OK” everything is done right)
sudo apachectl stop
sudo apachectl start

Create a Mysql User if you need one

mysql -u root -p
(If you dont have a root password you should create it)
mysql>CREATE USER 'railsuser'@'localhost' IDENTIFIED BY 'pass';

Create a Rails app

cd ~/sites
rails new rails_app

Configure database.yml

cd rails_app
mate config/database.yml

Remove the content and add the following into file
  adapter: mysql
  database: railsapp_dev
  username: root
  password: password
  host: localhost

  adapter: mysql
  database: railsapp_test
  username: root
  password: password
  host: localhost

  adapter: mysql
  database: railsapp_prod
  username: root
  password: password
  host: localhost

Create an empty database

cd ~/sites/rails_app
rake db:create

Test your application


Responses to "Ruby on Rails With Apache/MySQL on Mac OS X (Snow Leopard)"

  1. seb says:

    Clean & simple.
    BTW I experienced same issues with 5.5 :-/

  2. admin says:

    Thanks Sebastien!

  3. Jere says:

    Remove Mysql 5.5 if you have (Mysql 5.1 is the most stable version)

    Is not necesary!!

    [1] gem install mysql
    [2] you will see like error syntac but dont pay attention to it
    [3] find LIBMYSQL.DLL from your mysql/bin folder in your mysql folder
    not ruby folder.
    [4] copy & paste that file to ruby/bin
    [5] restart your server or console, if you are already run it.


  4. admin says:

    this is mac osx installation dude!

  5. rndrfero says:

    Same issuses with MySQL 5.9 on macosx 10.6 … tried everything to make ruby 1.8.7 + mysql gem and ruby 1.9.2 with mysql2 gem running …



  6. admin says:

    Glad that it helped bro…

  7. John says:

    This is awesome. You are awesome. Thanks. I have been trying to get this working for 5 hours and finally got it by following your detailed instructions.

  8. Amit says:

    Hi i tried so many time. i got this error. Please help me.

    ERROR: Error installing mysql:
    ERROR: Failed to build gem native extension.

    /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby extconf.rb –with-mysql-dir=/usr/local/mysql
    extconf.rb:4: warning: Insecure world writable dir /usr/local in PATH, mode 040777
    extconf.rb:4: warning: Insecure world writable dir /usr/local in PATH, mode 040777
    extconf.rb:7: warning: Insecure world writable dir /usr/local in PATH, mode 040777
    extconf.rb:17: warning: Insecure world writable dir /usr/local in PATH, mode 040777
    checking for mysql_query() in -lmysqlclient… no
    checking for main() in -lm… yes

  9. Perhaps I’m just showing my newbie feathers, but after following these directions I was still getting

    > rake db:create
    (in /Users/jrepenning/stats/codesion/blog)
    db/test.sqlite3 already exists
    rake aborted!
    uninitialized constant Mysql2

    I got past that by adding

    gem ‘mysql2’

    to Gemfile.

  10. admin says:

    All adapter fields should be set as mysql in database.yml

    adapter: mysql
    database: railsapp_dev
    username: root
    password: password
    host: localhost

    adapter: mysql
    database: railsapp_test
    username: root
    password: password
    host: localhost

    adapter: mysql
    database: railsapp_prod
    username: root
    password: password
    host: localhost

  11. Lena says:

    I followed the instructions above but I get an error at rake db:migrate:

    “Could not find gem ‘sqlite3 (>= 0)’ in any of the gem sources listed in your Gemfile.”

    I have SQLite version 3 so I’m unsure what I’m missing.

    Thanks in advance for your help!

  12. admin says:

    Did you sudo gem install sqlite3-ruby?.
    Check out this link

  13. Tobias says:

    First: Thank you for this tutorial.
    Second: I have a problem with “irb > Mysql.new(‘localhost’, ‘root’, ‘password’, ‘test’)”. It always tells me: “Mysql::Error: Access denied for user ‘root’@’localhost’ (using password: YES)”

    Do you have any idea how I can solve this problem? mysql server is running.


  14. diego says:

    Ok, thanks for this Post. I hope I’ll finally be able to create my first RoR test app.

    I’m getting problems at:

    $ rake db:create
    (in /Users/diego/Sites/rails_app)
    rake aborted!
    uninitialized constant Mysql

    What am I missing here?

  15. diego says:

    …still not being able to run rake db:create went to to see the app, then clicked on my app environment link and got this:

    !!! Missing the mysql2 gem. Add it to your Gemfile: gem ‘mysql2’
    Rails.root: /Users/diego/Sites/rails_app

    May you provide instructions on mysql2 gem?

    • admin says:

      Diego, you got to install mysql gem with the right flags. sudo env ARCHFLAGS="-arch x86_64" gem install --no-rdoc --no-ri mysql -- --with-mysql-config=/usr/local/mysql/bin/mysql_config

      • diego says:

        Thanks for the prompt answer, I uninstalled the mysql gem (using the posted command) and then used the given line… same result.

        I followed all the instructions given on this entry (even uninstalling Mysql 5.5 which I had) but still missing something.

        • admin says:

          Can you check if your mysql version is 5.1 sudo mysql --version

          • diego says:

            It seems the problem is with mysql, I installed mysql version 5.1.56 x86_64 (I checked that mysql server instance is running) but:

            $ sudo mysql –version
            sudo: mysql: command not found

          • admin says:

            Then something went wrong while you are installing mysql.

  16. diego says:

    Ok, mysql installation seem to be ok. I believe the problem is with the path… if I enter:

    $ /usr/local/mysql-5.1.56-osx10.6-x86_64/bin/mysql –version

    I get:

    /usr/local/mysql-5.1.56-osx10.6-x86_64/bin/mysql Ver 14.14 Distrib 5.1.56, for apple-darwin10.3.0 (i386) using readline 5.1

    So, I’ll need to find out how not to need to write the whole path to execute mysql commands.

    Any ideas?

  17. Awesome thanks – I was having loads of problems and downgrading from Mysql 5.5 to 5.1 fixed it!

    Many thanks


  18. Pradeep Kumar says:

    Thanks a lot !!
    previously I stuck at mysql….
    I step through your instruction….
    its working fine….

  19. Greg says:

    Hey – Thank you for this! I couldn’t get MySQL 5.5 to work either no matter what I tried on snow leopard. I read about the archflags and config path options stuff on other websites, but it didn’t work for me. MySQL 5.1 finally did. Cheers!

  20. You just saved my day! Thank you! I was having the same issues with MySQL 5.5!

  21. Vladislav says:

    Respect to you!

    It works.

  22. Brian Holtz says:

    Downgrading from MySQL 5.5 to 5.1 was the key for me. Thanks!

  23. Michael says:

    Worked for me too. Thanks! You need to put a paypal tip jar on your site. I’m sure folks like me would be plenty happy to show their gratitude.

  24. Brett says:

    Thank you awesome 🙂

  25. Vic says:

    Dude, you save me! after 5hs I got here and it works!, thanks!

  26. gem install rails -v 3.2.3 says:

    gem install rails -v 3.2.3

Leave a Reply