Installing Sinatra

Installing Sinatra is relatively straighforward, but you have to have other programs installed first. This guide should help people install Sinatra from scratch.

I use Ubuntu as my main operating system, so please accept my apologies for anything that doesn’t work on OSX or Windows. Please add any comments if you spot anything I’ve missed.

You will need to get used to using a terminal command prompt when using Sinatra. This might be more familar to Linux and OSX users. Any code examples that start with $> mean “type this into a terminal prompt”. To open up a terminal in Ubuntu go to Applications menu -> Accessories -> Terminal. On Windows, the terminal can be launched through “Start->Programs->Accessories->Command Prompt”, or “Start->Run->cmd.exe”. On Mac OS X, the terminal is found in Applications/Utilities/Terminal. (Thanks to Lucas for helping out with the instructions for Windows and OSX).

Install Ruby

Sinatra and Sinatra applications are written in the Ruby programming language, so before you do anything, you need to install Ruby.

If you are using Linux or OSX, Ruby may already be installed (OSX should have it by default). Open up a terminal command line prompt and type:

$> ruby -v

This will either tell you which version of Ruby you have installed or complain about “ruby: command not found”, which means you don’t already have it.

On Ubuntu (or any Debian based Linux), open up a terminal and enter the following command:

$> sudo aptitude install ruby

You then need to add some symlinks to tell your system where Ruby is installed.

$> sudo ln -s /usr/bin/ruby1.8 /usr/bin/ruby
$> sudo ln -s /usr/bin/ri1.8 /usr/bin/ri
$> sudo ln -s /usr/bin/rdoc1.8 /usr/bin/rdoc
$> sudo ln -s /usr/bin/irb1.8 /usr/bin/irb

If you’re a Windows user then the best way to install Ruby is to use the Ruby Installer for Windows.

Install Ruby Gems

Ruby Gems are the standard way of packaging programs and libraries that are written in Ruby. Sinatra is a Ruby Gem as well as many of its extensions. You need to install the Ruby Gems program to be able to install or remove gems.

If you are using Windows, then Rubygems should have been installed along with the Ruby Installer for Windows.

If you are using Linux or OSX, go to http://rubygems.org/pages/download and download the latest version of Ruby Gems (currently it is 1.3.5).

If you are using a Debian-based Linux (such as Ubuntu), you might need to add two new libraries first, enter the following command into a terminal:

$> sudo aptitude install libyaml-ruby libzlib-ruby

Assuming you have downloaded to your home folder, open up a terminal and enter the following commands:

$> tar xzvf rubygems-1.3.5.tgz
$> cd rubygems-1.3.5
$> sudo ruby setup.rb

Don’t forget to change the version number if it is not 1.3.5. This should install Ruby Gems. You also need another symlink:

$> sudo ln -s /usr/bin/gem1.8 /usr/bin/gem

To check that it is installed, type the following into a terminal:

$> gem -v

Install Sinatra

Now you are ready to install Sinatra. If you are using Linux or OSX, open up a terminal and enter:

$> sudo gem install sinatra

In Windows, open up a command prompt and enter:

c:> gem install sinatra

You should see lots of lines of code, indicating that Sinatra is being installd on your system.

Your First Sinatra App

Finally we’re going to build a very basic app to test that Sinatra is installed and working correctly.

Open up a text editor and type the following code:

require 'rubygems'
require 'sinatra'
get '/hi' do
  "I Did It My Way!"
end

Save this in your home folder as test.rb then open up a terminal and type:

$> ruby test.rb

You should see the following message:

== Sinatra has taken the stage ...
>> Listening on 0.0.0.0:4567

Open up your favourite browser and type “http://localhost:4567/hi” in the address bar. You should see the message

“I Did It My Way!”

Congratulations! Now you’re ready to start using Sinatra. My next blog post will go through building a complete application from start to finish.

blog comments powered by Disqus