Ruby On Rails Environment on Ubuntu 12.04 3

Posted by JD 07/18/2012 at 17:00

I attended a local Ruby Meetup last week where they showed how to get all the dependencies for a smart RoR setup on Ubuntu. I’ve only tested it on 12.04 myself, but something similar should work for 10.04. I have doubts whether installing on 8.04 is this easy for the current versions of RVM, Ruby and Rails.

The installation instructions came from a gist from Cajun_Code. Following those instructions makes this process fairly straight forward. We’ll install RVM, Ruby v1.9.3, Rails, SQLite3, nginx and thin to get started.

Updated for Rails4 and Ruby 2.0.0 at the bottom of the article.

What is a Full RoR Environment?

  • RVM – latest stable
  • Git
  • Ruby – latest stable
  • Rails – latest stable
  • nginx – load balancer – latest stable
  • thin – application server – distro version

It is important to install RVM before installing Ruby or Rails. RVM is Ruby Version Manager and lets us have multiple complete Ruby and Rails versions on the same machine. It also leaves the operating system ruby alone, so that OS tools like Puppet that are based on the OS Ruby version continue to work as expected for the system administrator. This is very important to have a stable system AND maintainable development environments that don’t clash.

With the latest RVM, we can install multiple different ruby and rails environments.

Git is the most popular DVCS. If you do any software development, becoming familar with git is a good idea, even if your company uses SVN or CVS. Git has interfaces to both cvs and SVN, so once you learn git, you can just point to those other repositories and keep working as you already know. Ok, it isn’t quite that simple since git has more capabilities, but for day to day stuff, it really is that simple.

Ubuntu Dependencies

When you know which dependencies are required before starting everything seems easier. Install these:

sudo apt-get update;
sudo apt-get install curl bison build-essential autoconf zlib1g-dev libssl-dev libxml2-dev libreadline6-dev git;
curl -L get.rvm.io | bash -s stable

This will create an RVM environment under ~/.rvm/.

The last command installs RVM under your userID by default. If you use the following command with sudo, it will be installed for everyone sharing the machine in a central location, /usr/local/rvm/. The downside is that it will need root equiv to manage going forward, which might not be desired.

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

Regardless of which command you use, follow the further installation instructions on the screen. These help pointers were provided during the installation.

  1. RTFM
  2. HELP (#rvm on irc.freenode.net)
  3. Cheatsheet
  4. Screencast

The next item was a reminder to run

source ~/.rvm/scripts/rvm

to setup environment settings. This needs to be done in any open terminals. Any new terminals on the machine wll get the new settings automatically.

It is a good idea to review the notes for your specific rvm install.

rvm notes

Further, there is another command to ensure your system meets the requirements for rvm.
rvm requirements

On my system, the requirments had many more dependencies for Ubuntu like subversion and ncurses-dev. I installed them before moving further, just to be safe.

Install Stable Ruby

Using rvm list known, I saw that Ruby 1.9.3 is the current latest stable release. That’s what we should install unless we have a specific need for some other Ruby version.

rvm install 1.9.3

This will take a while on most systems. The download will be quick. It is the later steps that will take some time. You can add a -j 2 to let your compiler use more CPU cores. Just change the number to match the number of CPUs on your machine.

On my virtual machine with 1 Core2Extreme core allocated, it took about 15 minutes.

Install Stable Rails

Next we want to install Rails3+, but we want to use a gemset so we can switch different versions of gems easier.

rvm gemset create rails3

will create a gemset called “rails3”. We will be careful to reference this specific tag for our figure gem installs, like rails.

rvm  use 1.9.3@rails3
gem install rails
gem install sqlite3

This installed rails-3.2.6 and sqlite3 (1.3.6).

At this point you should be ready to start programming in Ruby on Rails, but we aren’t ready to deploy any small apps. Using the development environment for deployments is a really, really bad idea.

Web Server, Reverse Proxy and Load Balancing

For development, the built-in web server is probably just fine.
For production, using a ruby on rails friendly server called thin with nginx running as a reverse proxy/load balancer is a good idea. I find the performance and flexibility of this configuration fantastic.

Installation for both are trivial, but the configuration for nginx is non-trivial. For any web server that is constantly under attack, using the latest available stable version from the developer is a good idea, not the older ( sometimes extremely older ) package manager version.

  • Add the nginx PPA
  • Update the repository lists
  • install nginx and thin
sudo add-apt-repository ppa:nginx/stable
sudo apt-get update
sudo apt-get install nginx
gem install thin

We installed thin using gem. We’ll need to maintain it using gem going forward.

Nginx provides some fantastic anti-hog capabilities plus it can be configured to prevent external sites from borrowing your image files by forcing the referrer to be only your servers and can easily redirect different clients to different versions of your app as needed. Nginx has many other useful capabilities.

Nginx configuration is beyond this article, but doesn’t have to be all that complicated. This article will explain a little more and this article explains how to restrict access to certain pages from the outside world using nginx.

Thin is the application server for our ruby apps. I like to run 2-3 app servers and let nginx manage connections to each server based on responsiveness. Nginx will recognize if a server fails to respond and will redirect traffic to other, faster servers. These servers can be running anywhere on your network or on the internet, but all traffic from those servers will be redirected through the nginx server back to the requesting client.

About a year ago, this little blog was hit with much more traffic than our network could stand. Here’s the discussion about that and our updates to improve performance using Thin and Nginx.

Here’s the current blog systems architecture, but we only have 2 app-servers running.

Testing showed that running more actually slowed down the responses.

Summary

If you’ve followed along, you should have rvm, ruby, rails, thin, nginx installed and be able to run multiple versions of ruby and rails on the same system.

Ruby 2.0.0 and Rails4 Update

Just some quick notes. Hopefully they aren’t out of order:

  1. #################################
    sudo apt-get update;
    sudo apt-get install curl bison build-essential autoconf zlib1g-dev libssl-dev libxml2-dev libreadline6-dev git;
    curl -L get.rvm.io | bash -s stable
  1. #################################
  2. Modify your ~/.bash_profile
    source ~/.rvm/scripts/rvm

rvm gemset create rails4
rvm install 2.0.0
rvm use 2.0.0@rails4

  1. Create a folder to hold your Rails projects
    mkdir ~/rails_projects; cd ~/rails_projects
    vi .rvmrc # add these lines to the project folder
    rvm_make_flags=“-j 3”
    rvm —ruby-version use 2.0.0@rails4
    rvm use gemset rails4
    gem install thin
    gem list rails
    gem install rails
    gem install therubyracer
    gem install sqlite3
  1. #################################
  2. Might want to install nginx using the stable PPA
    sudo add-apt-repository ppa:nginx/stable
    sudo apt-get update
    sudo apt-get install nginx
  1. #################################
  2. After all this, create a project folder
    rails new SomeApp
    cd SomeApp
  3. edit the Gemfile – fix the javascript thingy
  4. run bundler – to get all the necessary gems loaded
  5. rails server – to start a dev server on port 3000 – point your browser there
    There you have it. Hopefully these are enough notes for others to work through.
  1. JD 07/29/2012 at 21:58

    After you start writing Ruby code, you’ll need to test it. Test Driven Development rocks! I’m a huge fan due to my Perl background, though sometimes I write many more test cases than most people. I prefer to have lots of boundary condition test scenarios to ensure the code really behaves as required. I’ve even used the Perl test facilities to monitor systems.

    Ruby has a few different methods to aid with Test Driven Development. I’ve seen methods like RSpec, but it seems that many in the Ruby community use something called Cucumber. This pushes a slightly different testing method, BDD, Behavior Driven Development. I’m not certain I understand the difference, except that Cucumber uses simple language (English or many others) to describe how features should work and builds tests and code to achieve them. At least that’s what it appears to do.

    Anyway, in the article above, I left off testing from the installation. That was a mistake.

  2. JD 09/07/2012 at 18:45

    From the rvm.io website found the date of this comment. Over time, it can become out of date.

    This system is designed to install new rubies, not upgrade older ones. The upgrade process really swaps out installed rubies under rvm.

    You can update all gems within a specific rvm-managed environment using normal gem update commands.

    Known Rubies

    $ rvm list known

    Update rvm itself

    $ rvm get stable

  3. JD 09/08/2012 at 19:05

    A few things are missing from the instructions above.

    After rvm is setup and ruby is installed into to, there are a few more items


    $ gem install rails
    $ gem install therubyracer

    The last thing is a javascript tool – RoR is highly tied into javascript.

    After we create a new rails project. It is a good idea to put all your different project under your HOME directory in a single folder … perhaps ~/src. or ~/projects/. Then cd into those and run:


    $ rails new MyApp

    to have rails create the application directory and skeleton files.

    Then we cd into MyApp and edit the Gemfile to add the Javascript library. Add this to the Gemfile somewhere:

    • gem ‘therubyracer’

    If you forget to add this, there will be a javascript error when you try to start the server.
    Now we can startup a test server.

    $ rails server

    Then just point your web browser at http://localhost:3000/ to see the Welcome aboard. page.

    Seems that one of the questions asked most often is which editor should I use? That is a tough question to answer since we are all a little different. On Linux, I use either vim or geany. When we are beginning to learn a new system, new programming language, new libraries … a full IDE can hide really important details.