Ruby on Rails envy part 3 – Getting started with Rails 3 on Snow Leopard

This is the third post about getting started on Ruby on Rails. Now I’m on my MacBook, which I ust upgraded from 10.5 to 10.6.6. Since Ruby is already installed on the Mac, I did not need to use the Bitnami virtual machine I used in the previous articles.

Installing Rails

Here’s how I got ROR running on my MacBook:

  • Install Git here
  • Install RVM from a Terminal like this:
    [code lang=”bash”]
    bash < <( curl http://rvm.beginrescueend.com/releases/rvm-install-head )
    [/code]
  • add this to ~/.profile:
    [code lang=”bash”]
    [[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm"
    [/code]
    Then logout the terminal and log back in.

  • Install Rails 1.9.2 like so:
    [bash] rvm install 1.9.2 [/bash]
  • Set 1.9.2 as default like so: [bash]rvm –default 1.9.2[/bash]
  • Install Rails 3: [bash]gem install rails[/bash]
  • Install sqlite 3 [bash] gem install sqlite3-ruby[/bash]
  • Check versions with [bash] rails -v[/bash] and [bash] ruby -v [/bash] and see if they’re OK
  • The cool thing about install Rails like this is that everything is installed in my home folder, and you can completely run your apps from you home folder. So you don’t need elevated rights to install or run an application.

    Get the party started

    Create a new Rails project:
    [bash]
    $ rails new Weblog
    $ cd Weblog
    $ rails generate scaffold Story title:string posted_on:date storytext:text
    $ rake db:migrate
    $ rails s
    [/bash]

    Now, browse to http://localhost:3000/stories to take a look at your app.

    At least you know everything is running well and I am at the same point I was here. Next time (maybe tomorrow), let’s check out what actually happened.

Ruby On Rails Envy – how to set up a ROR environment (with BitNami)

From the moment I read about ScottGu sitting in a plane and setting the outline for ASP.NET MVC, I couldn’t help thinking: “This is a serious case of Ruby On Rails Envy.” I know, the MVC pattern is very old indeed, but I thought it is more than coincidental that, with the success of ROR, Microsoft comes up with an alternative. Oh well. Just my humble opinion.

I was writing webapplications with the Ruby on Railsframework back then (I think it is 2008) but as ASP.NET MVC matured I switched. Because the companies I work for do have a Windows environment ready for me, so practically I had to. And I never looked back at ROR. Which is a shame, because Ruby.Is.Beatiful.

So let’s get our hands dirty and get started with Ruby On Rails!

But wait. What? Where? How?

Setting up the Rails environment

If you are on a Mac or on Linux it’s relatively easy to set up a Rails environment. If you’re on Windows it is less comfortable, because it’s slow. And as for editing, when you’re on a Mac, the TextMate editor is the absolute bomb.
My MacBook is in the living room playing Spotify songs at the moment, so let’s take the Linux road today in a virtual environment. Let’s use vim as a texteditor.
This is going to be lots of fun!

Yeah, okay, but I’m a bit lazy and after all, it’s Sunday evening.

Right. Let’s download a virtual machine that runs Ubuntu Server with a complete Ruby environment. Please do so and we’ll start the tutorial from there.

After booting the virtual machine, this what you’ll see:


Login with username ‘bitnami’ and password ‘bitnami’.
You”ll be prompted to change the password.
Next, change to root. To do so type ‘sudo -s’ :

Basic Linux housekeeping for starters:
[bash]
sudo -s
apt-get update
apt-get upgrade
# install the vim editor
apt-get install vim
# set the network interface:
vim /etc/network/interface
[/bash]

Now the interfaces file opens. The IP address is set to dhcp by default, which we’ll change to static.
Hit the INSERT key to edit the file:

[bash]
# The primary network interface
auto eth0
iface eth0 inet static
address 192.168.201.6
netmask 255.255.255.0
network 192.168.201.0
broadcast 192.168.201.255
gateway 192.168.201.1
[/bash]

Now hit ESCAPE followed by :w and :q (for writing and quitting vim).
Set the DNS Server in /etc/resolv.conf:

[bash]
vim /etc/resolv.conf
nameserver 62.45.191.241
nameserver 62.45.191.242
[/bash]

Now it’s time to restart the network:

[bash]
/etc/init.d/networking restart
[/bash]

Now check with ifconfig if eth0 has the correct IP address. Because if this is the case, we can leave the VMWare console for what it is and connect to the virtual machine using Putty.
Oh. Apparently ssh is disabled on the virtual machine. Go:
[bash]
mv /etc/init/ssh.conf.back /etc/init/ssh.conf
start ssh
[/bash]

Now we can connect to the appliance using Putty.
Accept the fingerprint securitywarning and login with bitnami and your password. Switch to root immediately with sudo -s (I always do so).

Eeuw, this is so boring. Where are those fancy dircolors? (Note: in some versions the dircolors are already enabled)

[bash]
cd /home/bitnami
vim .bashrc
#paste this in the file:
if [ "$TERM" != "dumb" ]; then
[ -e "$HOME/.dircolors" ] && DIR_COLORS="$HOME/.dircolors"
[ -e "$DIR_COLORS" ] || DIR_COLORS=""
eval "dircolors -b $DIR_COLORS"
alias ls=’ls –color=auto’
#alias dir=’ls –color=auto –format=vertical’
#alias vdir=’ls –color=auto –format=long’
fi
[/bash]

that’s much better.

So, how about Ruby On Rails then?
The Bitnami Ruby Stack came with an example project. Let’s start that up and check it out:

[bash]
cd /opt/bitnami/projects/rubystack-2.1-0
#start the app:
ruby script/server
-bash: ruby: command not found
[/bash]

Hmm, that’s not working. We should add Ruby to the path.
[bash]
vim /etc/environment
#hit INSERT to edit:
PATH="/opt/bitnami/ruby/bin/:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games"
#hit ESCAPE followed by :w and :q
[/bash]

Now log off, log on and try again:

[bash]
root@linux:/opt/bitnami/projects/rubystack-2.1-0# ruby script/server
=> Booting Mongrel
=> Rails 2.3.5 application starting on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
[/bash]

And now we can access the application at http://your-vm-ip:3000.

So, everything works apparently. Next time we are going to create our first ROR project.