Getting Started with Sinatra

· 4 min read · Updated March 17, 2026 · beginner
ruby sinatra web tutorial

Sinatra is a lightweight web framework that lets you build web applications in Ruby with minimal fuss. Unlike Rails, which comes with every feature you could imagine, Sinatra gives you just what you need and nothing more. This makes it perfect for small projects, APIs, and learning how web frameworks work under the hood.

In this tutorial, you’ll install Sinatra, create your first app, learn about routing, work with views, and serve static files.

Installation

Before installing Sinatra, make sure you have Ruby installed on your system. If you don’t, check out our Installing Ruby guide.

Sinatra is distributed as a gem, so you install it like any other Ruby library:

gem install sinatra

Or add it to your Gemfile if you’re using Bundler:

gem 'sinatra'

That’s it! You’re ready to build web applications.

Your First Sinatra App

Create a new file called app.rb and add the following code:

require 'sinatra'

get '/' do
  'Hello, World!'
end

Run your application:

ruby app.rb

You should see output indicating the server is running. Open your browser and visit http://localhost:4567. You should see “Hello, World!” displayed.

Let’s break down what just happened:

  • require 'sinatra' loads the Sinatra framework into your application
  • get '/' defines a route that responds to GET requests at the root URL
  • The string returned by the block is sent as the response

You can also return HTML directly:

get '/' do
  '<h1>Hello, World!</h1><p>Welcome to my Sinatra app</p>'
end

Routing Basics

Routing in Sinatra maps URLs to code that handles requests. You define routes using HTTP methods: get, post, put, delete, and patch.

get '/hello' do
  'This is a GET request'
end

post '/hello' do
  'This is a POST request'
end

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

The :name in the route is a parameter you can access through the params hash. Visit /users/Alice and you’ll see “Hello, Alice!”.

You can also capture multiple segments:

get '/books/*/author/*' do
  "Book author: #{params[:splat].last}"
end

Route parameters are useful for building RESTful APIs and dynamic web pages.

Views with ERB

Returning strings from routes works for simple cases, but most applications use templates to keep their code organized. Sinatra supports many template engines, and ERB (Embedded Ruby) is the most common.

Create a folder called views and add a file called hello.erb:

<h1>Hello, <%= @name %>!</h1>
<p>Welcome to my Sinatra application.</p>

Update your route to render this template:

require 'sinatra'

get '/hello/:name' do
  @name = params[:name]
  erb :hello
end

The @name instance variable is available in your ERB template. The erb :hello line looks for views/hello.erb.

You can also pass a layout file for consistent page structure. Create views/layout.erb:

<!DOCTYPE html>
<html>
<head>
  <title><%= @title || 'My App' %></title>
</head>
<body>
  <%= yield %>
</body>
</html>

Wrap your content in yield tags, and Sinatra automatically uses the layout.

Static Files

Most web applications need to serve CSS, JavaScript, and images. Sinatra makes this easy by default—any files in a public folder are served automatically.

Create the structure:

your-app/
├── app.rb
├── public/
│   ├── style.css
│   └── script.js
└── views/
    └── hello.erb

Reference these files in your views:

<link rel="stylesheet" href="/style.css">
<script src="/script.js"></script>

The /public part of the path is omitted because Sinatra serves from that directory by default. This is perfect for assets that don’t change often.

Next Steps

You’ve built a working Sinatra application from scratch. Here’s what to explore next:

  • Sessions and cookies - Track user data across requests using enable :sessions
  • Helpers - Extract shared logic into helper methods
  • Models - Connect to a database using ActiveRecord or ROM
  • Deployment - Deploy to Heroku, Render, or any Ruby-friendly host

Sinatra is flexible enough to grow with your project but simple enough to understand in an afternoon. Start small, iterate quickly, and enjoy building web applications the Ruby way.

See Also