How To Install And Get Started With Sinatra On Your System Or VPS

Published on July 19, 2013

Maxwell Bernstein

How To Install And Get Started With Sinatra On Your System Or VPS


Sinatra is a simple and lightweight web framework written in Ruby. This article assumes you know basic Ruby and have Ruby and RubyGems installed on your system or cloud server. Using Ruby Version Manager (RVM) is preferable.

It's not Rails. What is it?

Sinatra is similar to Ruby on Rails in that they are both web frameworks. It's pretty different from Rails in that it's much lighter (less overhead), and you have more fine-grained control over your webapp. Additionally, there is no built-in database functionality or page rendering — all of that is done manually.

Why Sinatra, then?

It's fast. It's simple. It's efficient. However, with that comes more work and more room for error.

Sinatra is best used for smaller web applications, or ones that don't need bloat, like those on the list on their website. Single-purpose apps abound, like one that only flips text upside down. You could even write a simple Jekyll front-end with Sinatra.

Installing Ruby and RubyGems

The best way to install Ruby and RubyGems is with Ruby Version Manager, or RVM. RVM allows you to have multiple Rubies installed, with their own sets of gems (gemsets), and even different gemsets per Ruby. To get the latest RVM and Ruby 2.0.0 run:

\curl -L https://get.rvm.io | bash -s stable --ruby=2.0.0

You can put any version number for the Ruby version. This installs RVM for the one user running the command. To install systemwide, prepend sudo before bash:

\curl -L https://get.rvm.io | sudo bash -s stable --ruby=2.0.0

If a binary Ruby is available for your system or cloud server (most likely), it will be downloaded and installed. If not, it will build Ruby from source. This can take some time.

At the end of the installation, log out and log back in. Then it will ask you to run the command

source ~/.rvm/scripts/rvm
— this loads RVM into your current terminal. If all goes well, you will not have to do that again for any other terminal you open. However, if you are continually prompted to run that command, add it to your startup file, like




Installing the gem

Just as you would install any other gem, Sinatra is installed like so:

gem install sinatra

That is all. You have installed Sinatra.

Getting started: Hello World

Your first web application should be simple, and easy to understand. Applications are wrapped in Ruby classes. Here's a simple web application in 2 files:

# app.rb
require 'sinatra'

class HelloWorldApp < Sinatra::Base
  get '/' do
    "Hello, world!"
# config.ru
require './app'
run HelloWorldApp

After writing the contents to the two files app.rb and config.ru (using Emacs or your preferred text editor), you can run the application by running from the same folder (say, /home/user/code/my_sinatra_app):


This starts WEBrick, which serves your application. You will notice that WEBRick tells you what IP and port it is serving; take note. You can access your application at http://IP:port.

Understanding the Hello World application

In Sinatra, each get (or post, put, etc) block defines each route, and how the app responds to specific HTTP requests. In our case, we defined what happens when the user requests the root directory of the application, or /.

Let's try something more complex.

Let's configure our HelloWorldApp to take a parameter!

# app.rb
require 'sinatra'

class HelloWorldApp < Sinatra::Base
  get '/' do
    "Hello, world!"

  get '/:name' do
    "Hello, #{params[:name]}!"

URL parameters are specified like so: :param, and stored in the params hash back in the Ruby code.

This code specifies 2 routes: one for the naked /, and another for /:name. We could simplify this routing to one route that checks for the presence of a name parameter, though!

# app.rb
require 'sinatra'

class HelloWorldApp < Sinatra::Base
  get '/?:name?' do
    "Hello, #{params[:name] ? params[:name] : 'world'}!"

This is more complex. We can denote "optional" parameters by surrounding them with question marks. Then we use the ternary operator to check for the presence of params[:name].Alright, what if you wanted to led the user specify the greeting? Let's take a look... how would you structure that?

# app.rb
require 'sinatra'

class HelloWorldApp < Sinatra::Base
  get '/:greeting/?:name?' do
      "#{params[:greeting]}, #{params[:name] ? params[:name] : 'world'}!"

Now you can just navigate to http://yoursever:port/Aloha/Timothy and Mr Timothy will feel very special!

Further Information

We recommend Sinatra's own Getting Started guide, which is phenomenal. It'll help tremendously.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the authors
Default avatar
Maxwell Bernstein


Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Great article. Please also write a tutorial on how to host a production Sinatra app on DigitalOcean. There are some tutorials but nothing seems to work.

Great tutorial. But I want to access my site at example.com. How can I do that?

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Featured on Community

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel