Web Development in Ruby : Using a Rackup File to Run Rack App


To learn how to use rackup command to run Rack app.


Rack is also an implementation. The Rack gem provides helper classes for : Request / Response wrappers, logging, authentication (basic and digest), cookies, sessions, mock requests and responses.

Rack gem gives you rackup command which lets you start your app on any supported application server. The rackup command is a useful tool for running Rack applications, which uses the Rack::Builder DSL to configure middleware and build up applications easily. It automatically figures out the environment it is running in, and runs your application as FastCGI, CGI, or standalone with Mongrel or WEBrick--all from the same configuration.


Step 1

Create a basic_rack.ru file with:

app = lambda {|env| [200, {'Content-Type': 'text/html'}, 'Hello World']}
run app

The .ru extension stands for Rack's rackup executable. These files are regular Ruby files.

Step 2

Start the app using:

$rackup basic_rack.ru

Browse to http://localhost:9292.

Step 3

You now see the text : 'Hello World' in the browser.

Step 4

Now let's look at an example where we can use variables.

class HelloWorld
  def initialize(name)
    @name = name

  def call(env)
    [200, {'Content-Type' => 'text/plain'}, ["Hello #{@name}"]]

app = HelloWorld.new("Bugs Bunny")
run app

Step 5

This will print 'Hello Bugs Bunny' in the browser.


The method call takes an environment Hash representing a request. It returns a three part array with each of the parts of a HTTP response. Here is the code for Rack Handler for Thin Server.

require "thin"
require "rack/content_length"
require "rack/chunked"

module Rack
  module Handler
    class Thin
      def self.run(app, options={})
        environment  = ENV['RACK_ENV'] || 'development'
        default_host = environment == 'development' ? 'localhost' : ''

        host = options.delete(:Host) || default_host
        port = options.delete(:Port) || 8080
        args = [host, port, app, options]
        # Thin versions below 0.8.0 do not support additional options
        args.pop if ::Thin::VERSION::MAJOR < 1 && ::Thin::VERSION::MINOR < 8
        server = ::Thin::Server.new(*args)
        yield server if block_given?

      def self.valid_options
        environment  = ENV['RACK_ENV'] || 'development'
        default_host = environment == 'development' ? 'localhost' : ''

          "Host=HOST" => "Hostname to listen on (default: #{default_host})",
          "Port=PORT" => "Port to listen on (default: 8080)",

You can see how it abstracts running a specific server behind a well defined interface.


  1. Use rackup file to run the lobster example.
  2. Use rackup file to run the protected lobster example that uses HTTP Basic authentication.


In this article, you learned how to use rackup file to run Rack apps.

Related Articles

Ace the Technical Interview

  • Easily find the gaps in your knowledge
  • Get customized lessons based on where you are
  • Take consistent action everyday
  • Builtin accountability to keep you on track
  • You will solve bigger problems over time
  • Get the job of your dreams

Take the 30 Day Coding Skills Challenge

Gain confidence to attend the interview

No spam ever. Unsubscribe anytime.