Prasenjit Kumar Nag a.k.a Joy

A Developers Adventure in Coding

Setting Up the Environment and Running Your First Rails App in Mac OSX Snow Leopard

I have been into Rails recently and found out that configuring Rails Environment is easy in Mac. So I will be sharing the steps I followed to setup the environment and running my first rails app.

Installing a C compiler: Mac OSX’s installation Disc comes with a set of developer tools which is not installed by default. So go ahead and install XCODE from the Installation DVD. It will install all necessary tools to compile and install packages from source. And we will need compiling source to install versions of ruby using RVM(Ruby version manager).

Installing RVM: Ruby Version Manager is a very handy tool while working with multiple version of Rubies. Snow Leopard comes with Ruby 1.8.7. And using RVM you can install Ruby 1.9.2 or the current stable version 1.9.3. And switching between version using RVM is very easy. So let’s jump into installing RVM. Rvm’s installation is very easy. The rvm website provides a one liner installation script to run in your terminal.

1
$ curl -L get.rvm.io | bash -s stable

Then to load the rvm you have to run the following command:

1
$ source ~/.rvm/scripts/rvm

But you are not going to load the rvm every time you start terminal. So its better to add the following file in your ~/.bashrc or ~/.bash_profile file and ~/.zshrc if you are using zsh as your default shell.

1
2
# place in ~/.bash_profile or ~/.bashrc as the very last line
[[ -s $HOME/.rvm/scripts/rvm ]] && . $HOME/.rvm/scripts/rvm

After loading the rvm script file to check if rvm is properly installed in your system run the following command in terminal

1
$ type rvm | head -n1

If you it says “rvm is a shell function” or “rvm is a function” then it’s fine and you are good to go.

Now go ahead and install an updated ruby version like 1.9.2 or 1.9.3 using the command

1
2
3
$ rvm install 1.9.2
# or
$ rvm install 1.9.3

It will take some time and that specific ruby version will be installed automatically. Now to check which version of ruby is default in terminal you can try the following command

1
$ ruby -v

It’s supposed to say 1.8.7  if you havent upgraded the default Ruby version. Now to use the newly installed Ruby you have to tell rvm to do so using the command

1
2
3
4
5
# Use which version you installed using rvm
rvm use 1.9.2
# The above command will load the environment in current terminal session
# if you want to use this as the default version in terminal all the time use --default option instead
$ rvm use 1.9.2 --default

now if you run ruby -v you will see your desired ruby version. Now let install rails gem using gem install command.

1
$ gem install rails

It will take some time and install latest rails system into your system. Now let’s create a new rails project. Move to your desired directory and run the following command

1
$ rails new blog

It will create a new rails application named blog in current directory. Now move into the new blog directory. and then run following command for bundling the gems :

1
2
$ cd blog
$ bundle install

It will take some time and install all gems required to run the application. Now start the app using the follwoing command

1
$ rails server

if you followed the previous steps properly you should see something like the following

1
2
3
4
5
6
7
Booting WEBrick
Rails 3.2.3 application starting in development on http://0.0.0.0:3000
Call with -d to detach
Ctrl-C to shutdown server
[2012-05-02 14:57:59] INFO WEBrick 1.3.1
[2012-05-02 14:57:59] INFO ruby 1.9.2 (2012-02-14) [x86_64-darwin10.8.0]
[2012-05-02 14:57:59] INFO WEBrick::HTTPServer#start: pid=81487 port=3000

It will start WEBrick (a simple  web server rails ships with itself ). And you can open your browser and point it to http://0.0.0.0:3000 and you should see the following screen

To test a full featured rails 3 app you can try this invoice app from github. This app is quite complete and even have pdf invoice generation. To install and run it try the following command  in terminal, you will have to have GIT installed. If you dont have git installed on your machine follow instructions from this page http://help.github.com/mac-set-up-git/ to install git.

1
2
3
4
5
6
7
$ git clone https://github.com/linkworks/invoices.git

$ cd invoices

$ bundle install

$ rails server

and then check the app in your browser in the following url. http://0.0.0.0:3000 you should see the app running. :) here is a screenshot of the app after you add register and add some data

Like me if you want to get fancy url’s for you rails apps through virtual hosts then follow the steps in this tutorials .

Rails by default uses sqlite as default database engine if you want to use mysql as default database then using the following command instead

1
rails -d mysql blog

Then configure your database access info in the file located at config/database.yml  and you are good to go. Though you have to have mysql2 gem installed in your system. Add

1
gem 'mysql2'

in Gemfile  in the root of your app directory and then run bundle install again

1
$ bundle install

If you have MAMP installed and want to know how you can reuse mamp’s mysql installation with rail’s apps then follow this tutorial I wrote earlier. :)

If you are using ubuntu you may try this tutorial http://blog.sudobits.com/2011/10/27/how-to-install-ruby-on-rails-in-ubuntu-11-10/

or this http://www.web2linux.com/05/installing-rails-3-on-ubuntu-10-04-lucid-lynx/

Comments