Skip to contentSkip to navigationSkip to topbar
Rate this page:
On this page

How to set up your Ruby and Sinatra development environment


In this guide, we will cover how to set up your Ruby(link takes you to an external page) development environment for a Sinatra(link takes you to an external page) project. We will also talk about a couple helpful tools that we recommend for all Ruby applications that use Twilio: Ngrok and the Twilio Ruby SDK(link takes you to an external page).

Let's get started!


_10
# Check your Ruby version
_10
$ ruby --version
_10
ruby 2.3.1p112 (2016-04-26 revision 54768)

If Ruby is already installed on your system, you can check its version by running ruby --version.


Install Ruby

install-ruby page anchor

How you install Ruby varies depending on your operating system.

Operating SystemInstructions
OS XThe easiest way to install Ruby on OS X is to use the official installer from ruby-lang.org(link takes you to an external page). You can also use Homebrew(link takes you to an external page) if you prefer.
WindowsThe easiest way to install Ruby on Windows is the official installer from RubyInstaller(link takes you to an external page). You can also use Chocolatey(link takes you to an external page) if you prefer.
LinuxThe exact instructions to install Ruby vary by distribution. Find instructions for yours here(link takes you to an external page).

Install using RVM

install-using-rvm page anchor

Ruby Version Manager or RVM, is a Unix-like software platform designed to manage multiple installations of Ruby on the same device.


_10
gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3
_10
curl -sSL https://get.rvm.io | bash -s stable --ruby


Bundler(link takes you to an external page) is a dependency manager that provides a consistent environment for Ruby projects by tracking and installing the exact gems and versions that are needed.

We have to install Bundler to manage the dependencies for the project.


_10
gem install bundler


Install a text editor or IDE

install-a-text-editor-or-ide page anchor

Before we can start our Ruby project we'll need something to write it with.

If you already have a code-writing tool of choice, you can stick with it for developing your Ruby application. If you're looking for something new, we recommend trying out a few options:

  • Atom(link takes you to an external page) is an IDE built with HTML, JavaScript, CSS, and Node.js integration. It runs on Electron, a framework for building cross-platform apps using web technologies.
  • Sublime Text(link takes you to an external page) is a text editor popular for its ease of use and extensibility. Start here if you're eager to get coding and don't think you'll want a lot of frills in your development environment.
  • RubyMine(link takes you to an external page) is a fully Integrated Development Environment (IDE) for Ruby. It takes longer to set up but comes with more helpful tools already installed.
  • Vim(link takes you to an external page) is a perennial favorite text editor among advanced users

If you're new to programming, we recommend giving Atom and Sublime Text each a try before you settle on your favorite.


Install Sinatra and the Twilio Ruby SDK

install-sinatra-and-the-twilio-ruby-sdk page anchor

We're almost ready to start writing our Sinatra web application, but first, we need to install the Sinatra library.

First, you need a Gemfile with the following content on it.


_10
# Gemfile
_10
source 'https://rubygems.org'
_10
_10
gem 'sinatra'
_10
gem 'twilio-ruby'

Ruby projects use Bundler(link takes you to an external page) to manage dependencies, so the command to pull Sinatra and the Twilio SDK into our development environment is bundle install.


_12
$ bundle install
_12
Using builder 3.2.2
_12
Using jwt 1.5.6
_12
Using multi_json 1.12.1
_12
Using rack 1.6.5
_12
Using tilt 2.0.5
_12
Using bundler 1.13.3
_12
Using twilio-ruby 4.13.0
_12
Using rack-protection 1.5.3
_12
Using sinatra 1.4.7
_12
Bundle complete! 2 Gemfile dependencies, 9 gems now installed.
_12
Use `bundle show [gemname]` to see where a bundled gem is installed.


Create a simple Sinatra application

create-a-simple-sinatra-application page anchor

We can test that our development environment is configured correctly by creating a simple Sinatra application. We'll grab the example from Sinatra's documentation and drop it in a new file called index.rb.


_11
# index.rb
_11
require 'sinatra'
_11
require 'twilio-ruby'
_11
_11
get '/' do
_11
content_type 'text/xml'
_11
_11
Twilio::TwiML::VoiceResponse.new do | response |
_11
response.say(message: 'Hello World')
_11
end.to_s
_11
end

We can then try running our new Sinatra application with the command ruby index.rb -p 3000. You can then open http://localhost:3000(link takes you to an external page) in your browser and you should see the <?xml version="1.0" encoding="UTF-8"?><Response><Say>Hello World</Say></Response> response.


Once you see your sample Sinatra application's "<?xml version="1.0" encoding="UTF-8"?><Response><Say>Hello World</Say></Response>" message, your development environment is ready to go. But for most Twilio projects you'll want to install one more helpful tool: ngrok(link takes you to an external page).

Most Twilio services use webhooks(link takes you to an external page) to communicate with your application. When Twilio receives an incoming phone call, for example, it reaches out to a URL in your application for instructions on how to handle the call.

When you're working on your Sinatra application in your development environment, your app is only reachable by other programs on the same computer, so Twilio won't be able to talk to it.

Ngrok is our favorite tool for solving this problem. Once started, it provides a unique URL on the ngrok.io domain which will forward incoming requests to your local development environment.

To start, head over to the ngrok download page and grab the binary for your operating system: https://ngrok.com/download(link takes you to an external page)

Once downloaded, make sure your Sinatra application is running and then start ngrok using this command: "./ngrok http 3000". You should see output similar to this:

ngrok screen.

Look at the "Forwarding" line to see your unique Ngrok domain name (ours is "aaf29606.ngrok.io") and then point your browser at that domain name.

If everything's working correctly, you should see your Sinatra application's "<?xml version="1.0" encoding="UTF-8"?><Response><Say>Hello World</Say></Response>" message displayed at your new Ngrok URL.

Anytime you're working on your Twilio application and need a URL for a webhook you should use Ngrok to get a publicly accessible URL like this one.


You're now ready to build out your Sinatra application. Here are a few other resources we like:


Rate this page: