I'm wondering how one can use Bundler with Sinatra. The idea is to use the gems that Bundler downloads inside the .gems folder.
-
3Yehuda Katz has a great article about using bundler today: http://yehudakatz.com/2009/11/03/using-the-new-gem-bundler-today/ – Damien MATHIEU Nov 10 '09 at 08:37
-
1Also, the bundler documentation itself has a good example of how to setup a sinatra application http://gembundler.com/sinatra.html and it is current. – christophercotton Nov 17 '10 at 03:51
5 Answers
Inside your Sinatra app, you just have to require the bundler setup:
require "bundler/setup"
require "sinatra"
get "/" do
"Hello world!"
end
Alternatively, if you don't want to add the additional require "bundler/setup"
at the top of your app, you can instead invoke sinatra via bundle exec
(e.g. bundle exec ruby myapp.rb
)
This assumes that you have a Gemfile
in the root of your application. It might look like this:
source "http://rubygems.org"
gem "sinatra"
This also assumes that you've already installed bundler (gem install bundler
) and that you ran bundle install
to install all the gem dependencies.

- 235,892
- 13
- 95
- 104
-
3if you use Bundler.require :defaults there's no need to manually require 'sinatra' or other gems – makevoid Mar 05 '14 at 23:56
I believe the best way is described here on EngineYard blog:
# This makes sure the bundled gems are in our $LOAD_PATH
require File.expand_path(File.join(File.dirname(__FILE__), 'vendor', 'gems', 'environment'))
# This actually requires the bundled gems
Bundler.require_env
class MyApp < Sinatra::Base
# stuff
end

- 26,737
- 24
- 105
- 146

- 23,590
- 15
- 91
- 109
As my original answer was quite old but there seems to be still attention to this topic here's the latest version of bundler/sinatra setup which will cover most of the use case:
A minimal config.ru
require './my_sinatra_app'
run MySinatraApp
An environment env.rb
file that requires all the bundled gems (also supports loading the current environment's group):
require 'bundler/setup'
APP_ENV = ENV["RACK_ENV"] || "development"
Bundler.require :default, APP_ENV.to_sym
Then your app file (requiring the environment) with your sinatra app (Sinatra::Base
):
require_relative 'env'
class MyApp < Sinatra::Base
get "/" do
"hello world"
end
end
Start your development server with rackup, and Sinatra will be loaded via Bundler, your app will be accessible from http://localhost:9292.
$ rackup
or bundle exec rackup
if needed
Make sure you have a Gemfile like the following one and you run the bundle
command before starting the app
source "https://rubygems.org"
gem "sinatra"
gem "puma" # a better rack server than the default webrick

- 3,276
- 2
- 33
- 29
+1 for the guide on the bundler website, but if you have a simple app and use Sinatra's dsl at the top level, then you need to do the following:
in your Gemfile (tell bundler not require sinatra):
gem 'sinatra', :require => false
and in the app's file (explicitly require sinatra):
require 'rubygems'
require 'bundler'
Bundler.require
require 'sinatra'
get '/' do
'hello world'
end

- 2,082
- 20
- 8
To use bundler with a Sinatra application, you only need to do two things. First, create a Gemfile.
gem 'sinatra'
Then, set up your config.ru file to load the bundle before it loads your Sinatra app.
require 'rubygems'
require 'bundler'
Bundler.require
require './my_sinatra_app'
run MySinatraApp
Start your development server with rackup, and Sinatra will be loaded via Bundler.
rackup

- 1,842
- 18
- 20