Tags

, ,

Setting up a Mac OS X Rails Dev Environment

by Sheldon Conaty on March 12th, 2009

These are the general steps I use when setting up a new, clean development environment. This assumes you are using Mac OS X 10.5. If you are on Windows then I’m very, very sorry (windows isn’t covered here). Follow all the steps below and you should end up wth:

  • MacPorts, version 1.600
  • Ruby, version 1.8.6
  • Ruby Gems, version 1.2.0
  • Rails Gem, version 2.2.2
  • mySQL, version 5
  • rspec and rcov for testing… you are doing TDD right?

Rails moves fast. Version 2.3 is due out very shortly so this post is going to get out of date quick. Update: 2.3.2 is out now, these instructions are still good.

Installing Ruby

What do you know! Nothing is required here. Version 1.8.6 is installed by default on Mac OSX 10.5.

Installing MacPorts & mySQL

Follow the instructions on Denis’s blog. You definitely need to install MacPorts. MySQL is only required if that’s what you will be using. Personally the biggest pain in the neck I have with mySQL is logging in after it has been installed. As a result I change it so that no password is required for root, not matter which localhost alias the login is from. To do this open a terminal window and type the following:

mysql -u root
use mysql;
update user set password=PASSWORD("") where user='root';
flush privileges;
quit;

This will set localhost, YourMachine.local and 127.0.0.1 to all require no password.

Installing FreeImage Port

Almost all the web applications I’ve worked on have required some form of image manipulation. Generally to resize images, to do this we need access to the OS level freeimage port. This can be installed using the following:

sudo port install freeimage

Installing Ruby Gems Port

Using MacPorts you can install the ruby gems package manager:

sudo port install rb-rubygems

If you encounter problems, such as:

Error: Target org.macports.activate returned: Image error: /opt/local/bin/gem already exists and does not belong to a registered port.  Unable to activate port rb-rubygems.

Then you can force the install:

sudo port -f install rb-rubygems

Patching rspec

For automated tests I prefer using rspec instead of the standard unit test framework built into rails. I also like rcov to give me an idea of how good my code coverage is. There is a known problem with rspec on Ruby 1.8.6 when run in rcov mode. This results in an ugly rb_gc_mark() error. The solution is to install a patched version of rpec

sudo gem sources -a http://gems.github.com
sudo gem install mergulhao-rcov

Rails

Now that the gem system has been installed you can use it to install the rails gem:

sudo gem install rails
sudo gem install memcache-client
sudo gem install rcov
sudo gem install ruby-debug

You will then need to install a gem to access the database server. 

For SQLite….

sudo gem install sqlite3-ruby

For MySQL….

sudo gem install mysql -- --with-mysql-config=/opt/local/bin/mysql_config5

Now your machine is all setup for rails development!

Okay But I have a Project Already

If you have an existing project you can check it out from github (you are using github right?). Now you should have enough system gems installed to let rails install the remaining gems required by the project (see the project’s environment.rb file for the list). To install the gems automatically:

sudo rake gems:install

Create Project’s Databases

If you are using mySQL you will need to create production, test and development databases. In the terminal type:

export RAILS_ENV=test
rake db:drop db:create db:migrate
export RAILS_ENV=development
rake db:drop db:create db:migrate
export RAILS_ENV=

Testing Your Installation

Once all the above has been completed you should be at a stage where you can run the development webrick server and the rspec tests. Try the following:

rake db:create:all        # Creates the mysql databases for development, production & test envs
rake db:migrate:reset     # Destroys any existing data and recreates the database structure
rake spec                 # Runs the tests
./script/server           # Starts the web server on port 3000

The spec command should run the tests and return zero failures. When the server is running you should be able to access the site at http://localhost:3000/.

From rails

blog comments powered by Disqus