Quick Start with Rails on OS X

Install Ruby

OS X (10.4) comes with Ruby 1.8.2 already installed.
For a quick start this is OK so let’s install Rails.

Install Rails

Since Rails comes as gem package, first we need to install RubyGems.
Download the latest package from RubyForge and unzip it.
Now go to the directory where you unpacked the package and run

$ sudo ruby setup.rb

When asked enter your password, and after install finish you can verify that everything went well by typing:

$ gem -v

Output should look something like this, depending on version you installed

=> 0.9.4

Now installing Rails is as simple as

$ sudo gem install rails --include-dependencies

To verify installation, type:

$ rails -v
=> Rails 1.2.3

Install MySQL

You can skip this step if you don’t want to use database but since Rails is a framework for developing database-backed web applications, you’ll probably want to use one.

  1. Download the MySQL package for OS X for your platform
  2. Double-click the drive image to mount it
  3. Locate the MySQL installer (something like mysql-5.0.45-osx10.4-i686.pkg) and run it, authenticating as needed
  4. Double-click MySQLStartupItem.pkg
  5. Double-click MySQL.prefPane and install it

Once the install is complete, start the MySQL server using the control panel. ( System Preferences -> Other -> MySQL ) and check Automatically Start MySQL Server on Startup

Test your installation

To start a new project (application), go to the directory where you plan to keep your applications (i.e. ~/Apps) and execute following commands

$ rails test_app
$ cd test_app
$ ./script/server

This will generate Rails skeleton in ~/Apps/test_app, and start a WEBrick server on port 3000.
Now open your browser and enter http://localhost:3000 in address bar. You should see a page with “Welcome aboard, You’re riding the Rails!” message.

We also need to tell Rails which database to use.
To do this edit database.yml file (~/Apps/test_app/config/database.yml) to look like this

development:
  adapter: mysql
  database: test_app_development
  username: root
  password:
  host: localhost

test:
  adapter: mysql
  database: test_app_test
  username: root
  password:
  host: localhost

production:
  adapter: mysql
  database: test_app_production
  username: root
  password: 
  host: localhost

Naturally we also need to create a database

$ mysqladmin -u root create test_app_development

and table in that database

$ mysql -u root

mysql> create table items ( 
         id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
         name VARCHAR(100) );

Execute scaffold command

$ script/generate scaffold item

Open http://localhost:3000/items and if you see something like this

Listing items

Name

New item

you have everything needed to get started with Rails development so Enjoy!

Technorati Tags: , , ,

Advertisements

2 thoughts on “Quick Start with Rails on OS X

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s