Guide · Open source
How to use Sinatra: install, write routes and run a Ruby app
Updated · written with AI from the project's own documentation and checked against our catalogue data
Sinatra is a Ruby library, described in its README as a DSL for quickly creating web applications with minimal effort. This guide walks you through installing it, running your first app, and using routes, parameters and templates. By the end you will be able to build a small web app or API endpoint in a single Ruby file.
Quick start
- 1Install Ruby. Make sure Ruby is installed, since Sinatra requires it.
- 2Install the gems. Run gem install sinatra rackup puma in your terminal.
- 3Create myapp.rb. Add require 'sinatra' and a get route that returns a string.
- 4Run the app. Start it with ruby myapp.rb and open http://localhost:4567.
- 5Add routes and parameters. Define more routes and read values from the params hash.
- 6Restart after changes. Restart the server after each code change, or use a reloader like rerun.
Install Sinatra and its prerequisites
Sinatra is written for Ruby, so you need Ruby installed first. Sinatra is installed as Ruby gems, using the gem command. The README installs three gems together: sinatra, rackup and puma.
- Prerequisite: Ruby
- Install with the gem command shown below
gem install sinatra rackup pumaWrite and run your first Sinatra app
Create a file named myapp.rb. Sinatra uses the get method to define a route that matches a web address and an HTTP method. The block returns the response body, here the text 'Hello world!'.
Run the file with ruby, then open http://localhost:4567 in your browser. Changes to code do not take effect until you restart the server, so restart it after each edit or use a code reloader such as rerun or rack-unreloader.
# myapp.rb
require 'sinatra'
get '/' do
'Hello world!'
end
# run with: ruby myapp.rbDefine routes and HTTP methods
A route is an HTTP method paired with a URL-matching pattern, and each route is associated with a block. Sinatra provides methods for get, post, put, patch, delete, options, link and unlink. Routes are matched in the order they are defined, and the first match is invoked.
Routes with a trailing slash are different from routes without one, so '/foo' does not match a request to '/foo/'.
- get, post, put, patch, delete, options, link and unlink each map to an HTTP method
- The first route that matches the request runs
get '/' do
# show something
end
post '/' do
# create something
endRead URL parameters, splats and query strings
Named parameters in a route pattern are available through the params hash. You can also read them as block parameters. Splat (wildcard) parameters are available in params['splat'] as an array.
Query parameters such as ?title=foo are read the same way through params, and they are optional for the route.
- Named parameters: params['name'] for a pattern like /hello/:name
- Block parameters: |n| receives the named parameter
- Splats: params['splat'] for patterns with *
get '/hello/:name' do
"Hello #{params['name']}!"
end
get '/posts' do
title = params['title']
author = params['author']
endUse templates and views
Sinatra supports many template languages, including Erb, Haml, Slim, Markdown and Liquid, among others listed in the README. A route can render a template by name, and templates can also be written inline or as named templates.
Variables set in a route are accessible in templates. The README also covers nested layouts using yield.
- Erb and Haml are among the template languages listed in the README
- Inline and named templates are both supported
Choose between classic and modular style
The classic style, shown in the examples above, is the shortest way to start. The modular style uses Sinatra::Base, which the README describes for middleware, libraries and modular apps. The README also explains how to serve either style with a config.ru file, using rackup.
For a single small file, the classic style is enough. Move to Sinatra::Base when the app grows or needs to be packaged as a library.
- Classic style: the shortest start, as in the first example
- Modular style: subclass Sinatra::Base
- config.ru: described in the README for serving an app
What people use it for
- Small web apps with a few pages
- Quick prototypes to test an idea
- Simple APIs with a handful of endpoints
- Small internal tools for a team
- Learning how web routes and requests work in Ruby
Frequently asked questions
Is Sinatra free to use?
Sinatra is open source and licensed under the MIT licence, as shown on its repository. You can use it in your projects under the terms of that licence.
What do I need to install Sinatra?
You need Ruby. Install the gems with gem install sinatra rackup puma, as the README shows.
How do I run a Sinatra app?
Save your code in a file such as myapp.rb and run it with ruby myapp.rb. Then open http://localhost:4567 in your browser.
Why are my code changes not showing up?
The README says changes take effect only after you restart the server. Restart it after each edit, or use a code reloader such as rerun or rack-unreloader.
Is Sinatra better than Rails?
Sinatra is a minimal DSL, so it suits small apps and APIs with a few endpoints. Rails is a full framework with built-in conventions for models, migrations and admin panels, so it suits larger applications.
Can I use Sinatra if my team does not use Ruby?
No. Sinatra is written for Ruby and requires it, so non-Ruby teams would need a different tool.
New agent skills, repos and AI tools — every week
The best new skills and open-source projects, the fastest-growing repos, and one free prompt. Five minutes to read. Unsubscribe anytime.
Our verdict
Sinatra is a good fit for Ruby developers who want a small, readable web app, a quick prototype, or an API with a few endpoints. If you need a full framework with models, migrations and admin panels built in, Rails or Hanami are more suitable, and Roda or Rack are alternatives if you want a different level of minimalism.
Install options & downloads