Menu

How to set up your Ruby and Sinatra development environment

In this guide we will cover how to set up your Ruby development environment for a Sinatra 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.

Let’s get started!

# Check your Ruby version
$ ruby --version
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

How you install Ruby varies depending on your operating system.

Operating System Instructions
OS X The easiest way to install Ruby on OS X is to use the official installer from ruby-lang.org. You can also use Homebrew if you prefer.
Windows The easiest way to install Ruby on Windows is the official installer from RubyInstaller. You can also use Chocolatey if you prefer.
Linux The exact instructions to install Ruby vary by distribution. Find instructions for yours here.

Install using RVM

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

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

Install Bundler

Bundler 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.

$ gem install bundler

Install a text editor or IDE

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 is a 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 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 is a full Integrated Development Environment (IDE) for Ruby. It takes longer to set up but comes with more helpful tools already installed.
  • Vim is a perernnial 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

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.

# Gemfile
source 'https://rubygems.org'

gem 'sinatra'
gem 'twilio-ruby'

Ruby projects uses Bundler to manage dependencies, so the command to pull Sinatra and the Twilio SDK into our development environment is bundle install.

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

Create a simple Sinatra application

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`.

# index.rb
require 'sinatra'
require 'twilio-ruby'

get '/' do
  content_type 'text/xml'

  Twilio::TwiML::VoiceResponse.new do | response |
    response.say(message: 'Hello World')
  end.to_s
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 in your browser and you should see the <?xml version="1.0" encoding="UTF-8"?><Response><Say>Hello World</Say></Response> response.

Install ngrok

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.

Most Twilio services use webhooks 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

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.

Where to next?

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

Twilio

Sinatra

Hector Ortega David Prothero Kevin Whinnery Kat King Samuel Mendes Andrew Baker Shawn Stern David Baldassari Alberto Mucarsel
Rate this page:

Need some help?

We all do sometimes; code is hard. Get help now from our support team, or lean on the wisdom of the crowd by visiting Twilio's Stack Overflow Collective or browsing the Twilio tag on Stack Overflow.

Thank you for your feedback!

Please select the reason(s) for your feedback. The additional information you provide helps us improve our documentation:

Sending your feedback...
🎉 Thank you for your feedback!
Something went wrong. Please try again.

Thanks for your feedback!

thanks-feedback-gif