Feb 2
A 7 Step guide to installing and configuring Ruby on Rails, MySQL, Apache with PHP, and phpMyAdmin on OS X Leopard
AddThis guide is intended for people who want a local Rails/MySQL development environment on OS X Leopard with the minimum amount of fuss. I’m sure there are many ways to accomplish an installation of this type, this is how I did it. Hopefully it will help someone.
First the disclaimer: I’ve been through these instructions several times to make sure that they are accurate. That said you use them at your own risk, it’s not my fault if you melt your machine etc etc. Right, onward!
These steps will make a new install of Ruby on Rails and MySQL in the directory /usr/local. This means that they should be safe from future OS X updates which might change how the existing Ruby install behaves.
1. Install Xcode.
You can either install Xcode from your Leopard DVD, or download it from here:
http://developer.apple.com/technology/xcode.html
It’s a large download, so you might want to go put the kettle on.
2. Install Textmate.
You can find a trial of this editor here:
http://macromates.com/
When prompted create a link to
/usr/local/bin
If you are not prompted click Help then Terminal usage to create this link
3. Set paths.
Before we do anything else we need to tell our system where things are going to be living.
We are now going to need to use the Terminal. You should be able to find this in Applications/Utilities. After each line has been typed, press return. Here’s the first one:
mate ~/.bash_login
This will create a blank file in Textmate. Enter the following into the blank file, ensuring it is the LAST line in the document:
export PATH="/usr/local/bin:/usr/local/sbin:/usr/local/mysql/bin:$PATH"
Exit and save the file.
Close and re-open Terminal.
4. Install Ruby, Ruby Gems and Rails.
I am basically going through Dan Benjamin’s process for installing Ruby on Rails in /usr/local, I’m just bringing it up to date. The original article can be found here:
http://danbenjamin.com/articles/2008/02/ruby-rails-leopard
Let’s make a folder for our downloads. In Terminal, type:
sudo mkdir -p /usr/local/src
sudo chgrp admin /usr/local/src
sudo chmod -R 775 /usr/local/src
cd /usr/local/src
Now let’s download and install the latest stable version of Ruby. At time of writing this is version 1.8.7. You can check to see what the latest versions of Ruby, Ruby Gems and Rails are here:
http://www.rubyonrails.org/down
Note! If the software versions listed on the website above differ from the ones I refer to, then in order to download and install the latest release(s) you will need to amend the Terminal lines that refer to these downloads. This should be quite easy, simply update any number that refers to a version of a particular piece of software.
In Terminal type:
curl -O ftp://ftp.ruby-lang.org/pub/ruby/1.8/ruby-1.8.7-p72.tar.gz
tar xzvf ruby-1.8.7-p72.tar.gz
cd ruby-1.8.7-p72
./configure --enable-shared --enable-pthread CFLAGS=-D_XOPEN_SOURCE=1
make
sudo make install
cd ..
To verify that Ruby has installed to the correct place type:
which ruby
You should see:
/usr/local/bin/ruby
If not, go back and check what you entered in step 3.
Assuming that all is well, we can now install Ruby Gems.
In the Terminal window type:
curl -O http://files.rubyforge.mmmultiworks.com/rubygems/rubygems-1.3.1.tgz
tar xzvf rubygems-1.3.1.tgz
cd rubygems-1.3.1
sudo /usr/local/bin/ruby setup.rb
cd ..
Nearly there. Now we can use RubyGems to install Rails.
In terminal type:
sudo gem install rails
Now that Rails is installed, we can add some Gems in the same way:
sudo gem install mongrel
sudo gem install capistrano
Or we can install multiple Gems on one line like this:
sudo gem install RedCloth termios rspec sake
Finally, we just need to install the Gems for accessing a database. Rails has been configured since version 2.0 to use SQlite as it’s standard database, and this comes pre-installed on Leopard. If you want to install the relevant Gem, then type:
sudo gem install sqlite3-ruby
The line to install the MySQL Gem is:
sudo gem install mysql -- --with-mysql-dir=/usr/local/mysql
Hooray! Onto the next step.
5. Install MySQL.
Download MySQL for Leopard from here:
http://dev.mysql.com/downloads/mysql/5.0.html#macosx-dmg
Select the “pick a mirror” link next to the (package format) download for Mac OS X 10.5 (x86)
When you have selected your mirror and the download has completed double click on MySQL package, followed by MySQL startup and finally MySQL prefpane
In Terminal type:
mate /etc/my.cnf
This will create another blank file in Textmate. This one will ensure that only local connections can be made to the MySQL database. Enter the following lines into the blank file, save and close.
[mysqld]
bind-address = 127.0.0.1
6. Edit Apache installation.
Apache is already installed on Leopard, it is turned off by default. We need to enable PHP support which is also turned off by default.
In Terminal type the next line followed by enter:
mate /etc/apache2/httpd.conf
This will open the Apache configuration file. Go to line number 114. It should look like this:
#LoadModule php5_module libexec/apache2/libphp5.so
Delete the # from the start of this line and save the document.
If you want to you can also change the root folder of LOCALHOST in this file. The relevant line should be 163, and it should look like this:
DocumentRoot "/Library/WebServer/Documents"
If you wanted to change the root of LOCALHOST to say, your Sites folder inside your user account, you could change the path to:
DocumentRoot "/Users/the_name_of_your_user_account/Sites"
Save and close the file.
Now we need to make some tweeks to how PHP will work. This will ensure that all errors will be reported to us.
In Terminal (again!) type these lines followed by enter after each:
cd /private/etc
sudo cp php.ini.default php.ini
mate /private/etc/php.ini
The file php.ini should open in textmate.
Go to line 305. It should look like this:
error_reporting = E_ALL & ~E_NOTICE
Ammend it to:
error_reporting = E_ALL
Save and close the file.
Go to the Apple menu and select System Preferences
Click on the sharing tab, and enable Web Sharing from the service list. This will turn on Apache server.
If at any point you want to restart Apache, all you need do is turn it on and off from this menu.
7. Install and configure phpMyAdmin.
Download phpMyAdmin from:
http://www.phpmyadmin.net/home_page/downloads.php
Copy the downloaded folder into the root folder of your Apache install. For me this is now:
Users/ian_wood/Sites
Rename the folder to phpMyAdmin
Now in Terminal do these lines followed by return after each: (Note! If you are installing the phpMyAdmin folder to a different location, you will need to amend the first line to reflect this)
cd ~/Sites/phpMyAdmin
mkdir config
chmod o+rw config
Open a web browser and go to the location of the phpMyAdmin folder. If you have done things the same way as me then this would be:
http://localhost/phpMyAdmin
You should see a “Welcome to phpMyAdmin” page, there will be an error on this page saying something like “access denied” because we haven’t created a config file yet.
You should also be able to see a link that says “setup script”, click on this.
Click on the “Add” button
Change the “Host” field from the standard entry of localhost to 127.0.0.1
Click on “Save” in the configuration row.
Now move the config.inc.php file out of the config folder and into the root phpMyAdmin folder. Delete the config folder when this is done.
Open Terminal and do these lines with a return after each:
sudo mkdir /var/mysql
sudo ln /tmp/mysql.sock /var/mysql/mysql.sock
Restart Apache.
You are done!
Posted by: Ian Alexander Wood
Wow, great instructions. I almost got it to work on my desktop but when I try to connect to localhost/phpMyAdmin, I get the following error just below the login:
Cannot load mcrypt extension. Please check your PHP configuration.
Have you run across that?
I also used your instructions to install it on my macbook, but localhost, localhost/phpMyAdmin, 192.168.0.xx, all the different ways to get to my Sites folder say “Permission denied”. I found it odd because I did both these OS X 10.5 machines, step by step and in tandem. Macbook did have another user logged on, so I’m installing OS updates, rebooting and trying again. Websharing was enabled on the macbook.
These are, by far, the best and most fresh instructions for getting RoR properly installed and configured. I feel like I’m finally close to getting there!
Hi John, glad you have found this post useful.
I haven’t come across the problem with the mcrypt extension. You can get some info on it here: http://uk2.php.net/mcrypt I’ve had a quick look browsing for references to it on my machine and have drawn a blank. The “permission denied” in phpMyAdmin I have encountered before. It’s usually because the config.inc.php file is referencing “localhost”. In the above configuration connection to your local MySQL database is restricted to the address “127.0.0.1″
Hope that helps!
Mcrypt was a very odd problem. Luckily I wasn’t the only one. This guide worked for me: http://michaelgracie.com/2008/12/26/plugging-mcrypt-into-php-on-leopard-10.5.6/
Now I’m off to fix my next error: #2002 – The server is not responding (or the local MySQL server’s socket is not correctly configured)
LOL
It’s amazing how many things can go wrong when setting up a development environment. I spent most of yesterday trying to install the RMagick gem for ImageMagick. Got it in the end, but I couldn’t quite believe how tough it was to do! Your MySQL problem hopefully will be a little easier. I seem to remember coming across a similar error before while trying to connect to a MySQL database using phpAdmin and it was either because the config.inc.php file wasn’t quite right, and once or twice it was because I hadn’t started the MySQL server!
I got it all working on my mac desktop. Unfortunately, I think there is still something wrong. irb doesn’t work they way it should and Xcode is hangs up when I drag rails in to the organize window….
I’m going to order a new hard drive, unplug my old ones, get this all running before plugging the old drives back and making the mac liveable. I’ve had a hard time getting everything working in 10.5.6.
Windows was a breeze, but I don’t deal with windows anymore. =(
By the way, I did get it all up and running and have been able to program a test app during the past couple of sleepless nights.
This is still the best 7 step guide to getting everything installed and configured on OS X 10.5.x My problems were due to following other ppl’s guides before I found yours.
Thanks again!
Excellent news! I’m glad you managed to banish all the bugs. Be sure to send me a link when your Rails apps go live.
[...] May I prod you in the direction of another Waavoo! article that you may be interested in if you are thinking about installing MySQL. There are instuctions for installation of pretty much everything. phpMyAdmin is especially useful 7 step guide for installing Ruby on Rails, MySQL, Apache with PHP, and phpMyAdmin on OSX Leopard [...]
Great post. Another great addition I used for this post is to install Marc L’s PHP5 beta with GD support is posted here at the Apple Discussions here – http://discussions.apple.com/message.jspa?messageID=8871129%238871129
Is it possible to do it on a G4 Mac running Leopard?
i did download ruby in rail but iam reciving this message can you help on how to fix this problem
missing the rails 2.3.2 gem. please “gem install -v=2.3.2 rails” update your rails_gem_version setting in config/enviroment.rb for the rails version
i did downloade this version and i do not know where i have to put please help me to fix the problem.
thank you all