Installing Rails Today

It seems that a lot of old blog posts are floating about on the web these days with instructions on how to install ruby/rails/mysql/etc. that are quite out of date. My post on the same topic included. I’d like to remedy that situation, since I just reinstalled my OS today and had to go through the steps once again. So here you are, internets, my contribution to keeping blogged instructions up to date…

I’ll be covering the steps necessary to get the full Rails stack running on your Mac (Rails, Mongrel, and MySQL), just in case you want to click fewer links and get more done.

To reiterate:

A Favor

Please don’t email me asking for help. I feel bad writing that, it feels pretty crummy to say, and I truly wish that I could answer specific questions about these instructions and help figure out why a certain step might have gone wrong for you, but I just can’t.

Just make sure to follow each step (Install Xcode! Set your path!) and these instructions should “just work” for you.

Getting Started

The major steps are

  1. Make sure you have XCode installed
  2. Set up your $PATH
  3. MacPorts!
  4. Gems
  5. MySQL
  6. Fire it up!
  7. Optional stuff (Subversion, RMagick, etc.)

Let’s roll…

Open a Terminal window, you’ll need it for most of these steps. Commands for the Terminal will appear like this:

cd ~
which ruby

Just an example, no need to run that now, the good stuff is next!

Install Apple’s XCode Tools

You need a compiler to compile software (duh). Lucky for us, Apple has created a nice package that installs one for you (along with a host of other nice tools for developing Mac software that we won’t be using right now).

Shortcut: if you have a directory called Developer at the root of your hard drive (the path is /Developer), these tools are already installed. Move to the next step!

To Install Xcode Tools:

  • Put in your Mac OS X Install Disc or download the latest version of XCode Tools from Apple Developer Connection
  • Open XcodeTools.mpkg
  • Click through the installation prompts

Check it:

  • You should now have a directory called Developer at the root of your hard drive.

Setting up your $PATH

Some say the path to success is paved with good intentions. I say it starts with /usr/local.

Start in your Terminal. If you’ve already got a .bash_profile in your home folder, I trust you know how to edit it appropriately. Otherwise, do this:

cd ~
touch .bash_profile
open .bash_profile

This will open TextEdit with .bash_profile activated. Add the following line to the file and save it:

export PATH="/opt/local/bin:/opt/local/sbin:/usr/local/bin:/usr/local/sbin:/usr/local/mysql/bin:$PATH"

The important detail here is that /opt/local/bin:/opt/local/sbin:/usr/local/bin:/usr/local/sbin: comes before $PATH. The mysql part is for later, but I’m saving you some time by putting it in now.

Make sure the file is saved and go back to your Terminal:

source ~/.bash_profile
echo $PATH

Confirm that the first thing in the output you see is
/opt/local/bin:/opt/local/sbin:/usr/local/bin:/usr/local/sbin and if it’s not, make sure you’ve followed these instructions carefully.

Now double check that XCode is installed:

which gcc

Confirm that the output you see is /usr/bin/gcc. If it’s not, you’ll need to go back and install Apple’s XCode Tools (see above).

All good? You’re ready to rock!

MacPorts

The MacPorts (formerly DarwinPorts) package makes installing the rest of the software really easy. Let’s get Readline and Ruby. Download the latest distribution of MacPorts from http://trac.macosforge.org/projects/macports/browser/downloads and install it. Then:

sudo port install readline
sudo port install ruby

Getting RubyGems

Before downloading this, we’ll create a directory to keep from littering your home folder:

mkdir ~/src
cd ~/src

Now, download the latest release of RubyGems:

curl -O http://rubyforge.org/frs/download.php/20585/rubygems-0.9.3.tgz

Install Rubygems:

tar xzvf rubygems-0.9.3.tgz
cd rubygems-0.9.3
sudo /opt/local/bin/ruby setup.rb
cd ..

Get Rails and Mongrel Gems:

sudo gem install rails --include-dependencies
sudo gem install mongrel --include-dependencies

Install MySQL

Download MySQL 5.0 the installer package right for your hardware:

http://mysql.org/downloads/mysql/5.0.html#macosx-dmg

Install MySQL (the installer package does us a favor and installs the MySQL server where it belongs, in /usr/local):

  • Mount the disk image
  • Install the mysql-standard-5.x.xx-osx10.4… package
  • Install the MySQLStartupItem.pkg
  • Install the MySQL.prefPane (hint: you can double-click it)

Start MySQL:

  • Open System Preferences
  • Find the MySQL Preference Pane at the bottom
  • Start the MySQL server by clicking the big “Start” button
  • Be sure to check the box “Automatically start MySQL server on startup” so your app works after you reboot your machine

Optionally Install the MySQL Ruby bindings (for speed):

sudo gem install mysql -- --with-mysql-dir=/usr/local/mysql

Choose the latest version for your platform, which is (ruby) since we’re obviously not on Windows.

Optionally Install the graphical MySQL Administrator so you can avoid the command line:

Note: By default, MySQL is installed with the user account root and no password. Documentation available here can show you how to change this to something more suitable (and secure). The Accounts section of the GUI tools can also be used to edit MySQL’s user accounts and passwords. Note: MySQL’s user accounts are completely separate and unrelated to Mac OS X/Unix accounts, the name root is just a coincidence.

Time to Roll

Now that you’ve got everything installed, you’re ready to write your Rails app! Get yourself in a brand new Terminal window in your development directory already…

Generate a Rails app:

rails myapp
cd myapp

Start Mongrel:

script/server

Hit your app with your browser and read up at http://rubyonrails.org/!

Extras

Capistrano

The best way to deploy your new Rails app.

sudo gem install capistrano --include-dependencies

Subversion

With MacPorts installed, you can install subversion with:

sudo port install subversion

Enjoy!

Comments are closed.