Snow Leopard, Ruby, Sinatra

My goal was to start working in Sinatra fairly quickly (specifically to get it working with Heroku). In order to do that, I had to spend a bit of time updating Ruby under Snow Leopard. The process is straight forward, if you realize the following:

  • (a) The $PATH is probably not set up properly;
  • (b) You need git;
  • (c) Update everything;

First step, is actually setting $PATH properly. This actually took hours because I kept forgetting a basic idea: Apple has changed this. Setting $PATH actually takes place in ~/.profile. This used to be .bash_profile. So, if you set things in .bash_profile, they may or may not show up. This wound up biting me at the end of the process when things just didn’t seem to be working properly and for the life of me I could not figure out why. It was because $PATH was not properly set. The moral of the story is: set $PATH properly or you will waste lots of time cursing.

After properly setting up $PATH, the next step is getting Git on OS X. Thankfully, “there’s a package for that.” This was painless and sets up the environmental variables for you which was nice.

Next, updating Rails and RubyGems. Note that Snow Leopard already has Ruby installed (ruby 1.8.7). I may consider updating to ruby 1.9, but right now, I just want to get up and running. So, to update:

sudo gem update --system
sudo gem install rails
sudo gem update rake

This updates all the core stuff. Rake is Ruby’s make tool. Where does everything get installed? Ah, here is the tricky bit. If you happy to be unlucky and use:

gem install/update

without sudo, you will have a set of useless files placed in:

~/.gem

Now, you could try just rm -rf on that directory and everything goes away. But, the problem is that gem list is not updated properly. Instead, you need to uninstall using:

gem uninstall --install-dir=~/.gem/ruby/1.8/
Successfully uninstalled -x.y.z

This helps you ‘undo’ the damage, but what happens when, inevitably, you yet again install a gem without sudo? Well, be smart and protect the directory:

chmod -R u-w ~/.gem

Thus saving your setup from you in the future. So far, so good. Now, install Sinatra:

sudo gem install sinatra

As I plan on using Sinatra with Heroku, it’s probably a good idea to go ahead and install the Heroku. The first step is to create an account. With the account created install the Heroku gem:

sudo gem install heroku

With Heroku, deployment under Sinatra is super easy: just Push with Git. But before we can deploy, we need to check that everything is working properly. As I am using TextMate,  I need to make sure to grab the latest RoR bundle. Inside TextMate, select RoR in the status bar to enable syntax highlighting. Now, to ensure that everything is running, create a new file in TextMate called hello.rb with:


require 'rubygems'
require 'sinatra'


get '/' do
"Konichi-wa Heroku-san!"
end

Save the file and then hit ⌘r to run the code. With any luck, you will see:


== Sinatra/0.9.4 has taken the stage on 4567 for development with backup from Mongrel

This means that Sinatra is now running. Head on over to http://localhost:4567/ to see your application. Success will mean that you will see “Konichi-wa Heroku-san!” in your browser. This means that the local version is up and running. To get the version setup for Heroku, you need to create an additional config.ru, a Rackup file for starting the Rack application. This file must contain the complete logic for initializing the application, such as:


require 'hello'
run Sinatra::Application

This file and hello.rb should be put in a new directory that is going to be put under Git revision control:


git init
Initialized empty Git repository in /Users/sarosh/src/hello/.git/
git add .
git commit -m "sinatra and heroku"
[master (root-commit) 4f93b81] sinatra and heroku
2 files changed, 8 insertions(+), 0 deletions(-)
create mode 100644 config.ru
create mode 100644 hello.rb

Now, to create the app on Heroku we need to set up keypair authentication first (you only have to do this once, but again a ‘gotcha’ if you have not set it up):

ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/sarosh/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /Users/sarosh/.ssh/id_rsa.
Your public key has been saved in /Users/sarosh/.ssh/id_rsa.pub.
The key fingerprint is:
xxxxxxxxx sarosh@Sarosh-Wahlas-MacBook-Pro.local
The key's randomart image is:
+--[ RSA 2048]----+

ssh-keygen -t dsa
Generating public/private dsa key pair.
Enter file in which to save the key (/Users/sarosh/.ssh/id_dsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /Users/sarosh/.ssh/id_dsa.
Your public key has been saved in /Users/sarosh/.ssh/id_dsa.pub.
The key fingerprint is:
xxxxxxxxx sarosh@Sarosh-Wahlas-MacBook-Pro.local
The key's randomart image is:
+--[ DSA 1024]----+

Creating the app on Heroku:


heroku create
Enter your Heroku credentials.
Email: blah@blah.com
Password:
Uploading ssh public key /Users/sarosh/.ssh/id_rsa.pub
Creating simple-robot-57.... done!
Created http://simple-robot-57.heroku.com/ | git@heroku.com:simple-robot-57.git
Git remote heroku added

Now, push the code:


git push heroku master

Counting objects: 4, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (4/4), 401 bytes, done.
Total 4 (delta 0), reused 0 (delta 0)
-----> Heroku receiving push
-----> Sinatra app detected
Compiled slug size is 4K
-----> Launching...... done
http://simple-robot-57.heroku.com deployed to Heroku
To git@heroku.com:simple-robot-57.git
* [new branch] master -> master

Now, navigating to http://simple-robot-57.heroku.com should show the Sinatra code. To test that things really are working, modify the code in hello.rb and commit the changes:


git add .

git commit -m "second heroku"

[master b3f0a44] second heroku
1 files changed, 1 insertions(+), 1 deletions(-)

git push heroku master

Counting objects: 5, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 387 bytes, done.
Total 3 (delta 0), reused 0 (delta 0)
-----> Heroku receiving push
-----> Sinatra app detected
Compiled slug size is 4K
-----> Launching...... done
http://simple-robot-57.heroku.com deployed to Heroku
To git@heroku.com:simple-robot-57.git
4f93b81..b3f0a44 master -> master

Then check the site again: http://simple-robot-57.heroku.com. Going to http://api.heroku.com/myapps will show you more information and the Heroku Dashboard. The next step is getting comfortable with Git.

Advertisements
%d bloggers like this: