We hope you find this tutorial helpful. In addition to guides like this one, we provide simple cloud infrastructure for developers. Learn more →

How To Install Ruby 2.1.0 And Sinatra On Ubuntu 13 With RVM

PostedFebruary 10, 2014 30.3k views Sinatra Ruby Ubuntu

Introduction


If a couple words were chosen to define Sinatra, they would most certainly be inspirational and concise. This tiny but remarkable little project led the way to the creation of many other similar ones – across different programming languages and platforms.

The "classy" web-development library Sinatra can allow you to quickly build web applications from scratch. Unlike the ever-so-popular Ruby on Rails framework, applications created on Sinatra can consist of a single file, solely depending on the Sinatra gem.

In this DigitalOcean article, we are going to learn how to install the latest available version of the official Ruby interpreter (v 2.1.0) on a Ubuntu 13 droplet along with Sinatra web-application development library. Afterwards, we will create a sample project and continue with real-world deployments.

Glossary


1. Installing Ruby And Sinatra


  1. Updating The Operating-System
  2. Getting The Essential Build/Development Tools
  3. Installing Ruby Version Manager (RVM)
  4. Installing Ruby 2.1.0
  5. Installing Sinatra
  6. Creating A www User Group And A deployer User

2. Creating A "Hello world!"Application With Sinatra


  1. Application Directory
  2. Sample Application Files
  3. Testing The Application (rackup)

3. Deployments


Note: This article is the first one of our two-piece Sinatra series. After finishing this one, to learn about actual deployments check out How To Deploy Sinatra Based Ruby Web-Applications.

Installing Ruby And Sinatra


Updating The Operating-System


We will start our tutorial by preparing our VPS, which means upgrading its default components to the latest versions to make sure we have everything up-to-date.

Update the software sources list and upgrade the dated applications:

aptitude    update
aptitude -y upgrade

Getting The Essential Build/Development Tools


Before continuing with installation of our target applications, we are going to install the essential development tools package: build-essential using the default package manager aptitude. This package contains tools necessary to install certain things from source.

Run the following command to install build-essential package:

aptitude install -y build-essential

Next, we are going to get commonly used development and deployment related tools, such as Git.

Run the following command to install some additional, commonly used tools:

aptitude install -y cvs subversion git-core mercurial

Installing Ruby Version Manager (RVM)


Ruby Version Manager (or RVM) lets developers quickly get started using Ruby and develop applications with it.

Not only does RVM allow you to work with multiple versions of Ruby simultaneously, but also it comes with built-in tools to create and work with virtual environments called gemsets. With the help of RVM, it is possible to create any number of perfectly isolated - and self-contained - gemsets where dependencies, packages, and the default Ruby installation is crafted to match your needs and kept accordingly between different stages of deployment -- guaranteed to work the same way regardless of where.

Note: To learn more about how to work with gemsets and how to use RVM, check out the article How To Use RVM to Manage Ruby Installations and Environments on a VPS.

In order to download and install RVM, run the following:

curl -L get.rvm.io | bash -s stable

And to create a system environment using RVM shell script:

source /etc/profile.d/rvm.sh

Installing Ruby 2.1.0


All that is needed from now on to work with Ruby 2.1.0 (or any other version), after downloading RVM and configuring a system environment is the actual installation of Ruby from source - which is to be handled by RVM.

In order to install Ruby 2.1.0 from source using RVM, run the following:

rvm reload
rvm install 2.1.0

Installing Sinatra


Once we have RVM install Ruby, we can use RubyGems package which comes with it by default to download and set up Sinatra on our system. RubyGems is the default Ruby package manager and it's an excellent tool at what it does.

Run the following command to install Sinatra with gem:

gem install sinatra

Creating A www User Group And A deployer User


After we are done with all the installations, it is time to get into basics and create a Linux group and a user to host web applications. For this purpose, we can name our group as www and the user as deployer.

Add a new user group:

# Usage: sudo addgroup [group name]
sudo addgroup www

Create a new user and add it to this group:

# Create a new user:
# Usage: sudo adducer [user name]
# Follow on-screen instructions to user-related
# information such as the desired password.
sudo adduser deployer

# Add the user to an already existing group:
# Usage: sudo adducer [user name] [group name]
sudo adduser deployer www

Now create the application folder in /var directory:

sudo mkdir /var/www

And set the permissions:

# Set the ownership of the folder to members of `www` group
sudo chown -R :www  /var/www

# Set folder permissions recursively
sudo chmod -R g+rwX /var/www

# Ensure permissions will affect future sub-directories etc.
sudo chmod g+s      /var/www

Edit /etc/sudoers using the text editor nano to let the user deployer sudo for future deployments:

nano /etc/sudoers

Scroll down the file and find where root is defined:

..

# User privilege specification
root    ALL=(ALL:ALL) ALL

..

Append the following right after root ALL=(ALL) ALL:

deployer ALL=(ALL:ALL) ALL

This section of the /etc/sudoers file should now look like this:

..

# User privilege specification
root     ALL=(ALL:ALL) ALL
deployer ALL=(ALL:ALL) ALL

..

Press CTRL+X and confirm with Y to save and exit.

Creating A "Hello world!" Application With Sinatra


Note: Below is a short tutorial on creating a two-page Sinatra-based application for demonstration purposes which is intended to be used as an example for our deployment articles. To get a more in-depth knowledge on working with Sinatra, check out the official Sinatra: Getting Started documentation.

Application Directory


Let's begin our Sinatra journey by creating a directory to host our sample Hello world! application.

Run the following command to create an application directory:

mkdir /var/www/my_app
cd    /var/www/my_app

RACK make certain assumptions regarding file hierarchy. Therefore, we need to have two more directories created alongside our application files: tmp and public.

Let's create them:

mkdir tmp
mkdir public
mkdir pids
mkdir logs

And also add a restart.txt to be used later by application servers:

touch tmp/restart.txt

Our final application structure:

/my_app                    # /var/www/my_app
   |-- /public             # Static files (and for Passenger server)
   |-- /tmp              
         |-- restart.txt   # Application restart / reload file
   |-- /pids               # PID files
   |-- /logs               # Log files
   |-- config.ru           # Rack file (for servers)
   |-- app.rb              # Application module

Note: To learn about different Ruby web-application servers and understand what Rack is, check out our article A Comparison of (Rack) Web Servers for Ruby Web Applications.

Sample Application Files


Now, we can begin constructing a Hello world! application.

Run the following command to create a app.rb inside the application directory my_app using the nano text editor:

nano app.rb

Copy and paste the below code block:

require 'rubygems'
require 'sinatra/base'

class MyApp < Sinatra::Base

  get '/' do
    'Hello world!'
  end

end

Save and exit by pressing CTRL+X and confirming with Y.

Next, we can create our config.ru file that web-application servers will use to run our program.

Run the following command to create a config.ru inside the application directory my_app using the nano text editor:

nano config.ru

Copy and paste the below code block:

require File.expand_path('../app.rb', __FILE__)
use Rack::ShowExceptions
run MyApp.new    

Save and exit by pressing CTRL+X and confirming with Y.

Now let's create our Gemfile:

nano Gemfile

Copy and paste the below code block:

source 'https://rubygems.org'
gem 'rack'
gem 'sinatra'

Save and exit by pressing CTRL+X and confirming with Y.

And perform an installation of these gems using bundle:

bundle install

Testing The Application (rackup)


In order to test your application, you can simply run a test server using rackup.

Run the following command to start a test server:

rackup config.ru --port=8080

# Hello world!

# To turn off the test server, press CTRL+C

Deployments


Although we have covered the basics of creating a Sinatra application, for deployment purposes you will be dealing with source code from your development computer machine to get your application online. Therefore, you will need to put (i.e. upload) your application's repository (i.e. source code) on your droplet.

Here are some ways you can achieve this before continuing with our Sinatra deployment article:

Note: Make sure to pay attention to file/folder permissions for deployments with certain server set ups. To learn about actual web deployments, check out our article on the subject How To Deploy Sinatra Based Ruby Web-Applications.

Submitted by: O.S. Tezer
Creative Commons License