Installing Rails Server on Ubuntu 12.04 with RVM, Nginx, Passenger and Oracle support

We have a new product ready and it is a Ruby on Rails webshop bundled with an ERP software written on Oracle Forms 6i. I will not go into the details of this work, maybe later i will make some posts of the process but as it is very linked to current software(only authentication is done with Devise, every other data and processing is done internally on Oracle database) there is no sense for me to explain it deeper. Off to the installation.

Server installation and configuration

First we need a clean install of Ubuntu server 12.04, x86 or x64 version will work all the same, but be careful later on when you download the Oracle instant client. On the installation, fill all the relevant data needed, and for installed services, choose only the ssh server, as you won’t be needing anything more. Another reminder: this will only be the webserver, assuming that you have the Oracle database installed somewhere in network reach.

After you login to your server for the first time, take your time to set up the ip address because it defaults to dhcp.
Edit the /etc/network/interfaces file and fill your address, gateway and dns servers(this is a new thing, i think starting in 12.04 /etc/resolv.conf gets updated whenever you restart networking, so everything you write there will get overwritten)

# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
auto eth0
iface eth0 inet static

After the config is saved, restart the networking and do a full update/upgrade of the server to get the latest packages installed:

sudo /etc/init.d/networking restart
sudo apt-get update
sudo apt-get -y dist-upgrade

After all is done, reboot your server.

Installing RVM and latest Ruby version

After your server is updated you can proceed with installing RVM and the latest stable Ruby(as i’m writing this, the latest version is 1.9.3p194 so i will be using this version)

First we install RVM prerequisites:

# rvm requires curl and git to install, and you will probably need them on your server so go ahead and install them
apt-get -y install git-core curl

# Installing RVM as sudo, to get RVM ruby system wide
curl -L | sudo bash -s stable

# sourcing rvm environment so you can use it untill you login again
source /etc/profile.d/

And that will install RVM and make it available for all users that belong to the rvm group(hint: edit /etc/groups and add rvm after the desired username). After installing RVM and adding our user to the  rvm group we are off to installing latest Ruby version

# install all requirements needed for YARV/MRI Ruby (you can easily get these by running 'rvm requirements' in the terminal)
sudo apt-get install build-essential openssl libreadline6 libreadline6-dev curl git-core zlib1g zlib1g-dev libssl-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt-dev autoconf libc6-dev ncurses-dev automake libtool bison subversion

# install requirements needed for Oracle Instant client
sudo apt-get install libaio1

# install latest stable 1.9.3 and set it as default ruby version
rvmsudo rvm install 1.9.3 --default

# disable installation of RDoc and RI when installing gems
echo 'gem: --no-ri --no-rdoc'  >> ~/.gemrc

# install bundler gem, as you will later need for deployment
rvmsudo gem install bundler

 Installing Passenger and Nginx web server

After you have ruby and everything needed installer go and install passenger gem and nginx web server with it

# install  passenger gem
rvmsudo gem install passenger

# install passenger nginx module (and nginx if you haven't installed it yet), choose first option(Yes: download, compile and install Nginx for me) when it asks you what to do
# if there are more requirements the script will tell you and after you install them rerun the command
rvmsudo passenger-install-nginx-module

The installation will set up the nginx.conf file for you and show you how you can enable a rails site on your server. But i will include a sample config later on.
Just for convenience, symlink nginx conf folder that is in /opt/nginx/conf to /etc/nginx and download linode nginx startup/shutdown script

# symlink nginx conf folder to /etc/nginx
sudo ln -s /opt/nginx/conf /etc/nginx

# download linode nginx init script and create the service
wget -O
sudo mv /etc/init.d/nginx
sudo chmod +x /etc/init.d/nginx
sudo /usr/sbin/update-rc.d -f nginx defaults

Installing Oracle instant client

To connect your Rails application to Oracle database, you will have to install Oracle client, either full or instant one. As installing full client requires alien and converting .rpm packages to .deb it’s a bit of a hassle. Also, it will consume more space on your drive, and all that is unnecessary, we are trying to keep it small here.

First you should download zip files from Oracle site depending what architecture you are running:
Oracle Instant Client 11.2 for linux:  X86X64

Download only instantclient-basic, instantclient-sqlplus and instantclient-sdk zip archives and unzip them all into the same instantclient_11_2 folder. Now as sudo move that folder so the path is /opt/oracle/instantclient_11_2

After that you only have to add two environment variables to be able to install ruby-oci8 gem, which is prerequisite for running Ruby applications on Oracle database. Add the following lines to /etc/enviroment file


# as i am from Croatia, and we use our national date and money preferences, my NLS_LANG is this one, yours will maybe be different

After you reboot you have to symlink the oracle library so it will function properly(i don’t know why they don’t do this themselves).

ln -s /opt/oracle/instantclient_11_2/ /opt/oracle/instantclient_11_2/

After that try and install ruby-oci8 gem.

rvmsudo gem install ruby-oci8

If there are no errors, and there should not be if you followed the guide, you are set to deploy your application on the server you just installed. In the next post, i plan to write something about tips, tricks and caveats of developing Rails applications on Oracle database.

Useful links to get and learn about all the stuff i have mentioned in the post:

Ruby Version Manager(RVM):
Oracle Instant Client 11.2 for linux:  X86, X64
Passenger server:

Deploying Rails applications on Windows

Due to some legacy issues, and HP workstations being shit that you can’t turn the hardware virtualization on, i had an issue with one client regarding speed and loading time of an application. My common deployment server is running Ubuntu on VMWare or Oracle VBox, passenger + nginx, or apache even. But, this “server” is just a regular machine(people here don’t want to invest in real hardware, or just don’t have the money).

So i had to deploy the app on windows 7, and i will show you how.

First, there are few steps you have to take care of:

1. Install Ruby with the ruby installer

2. Install Ruby DevKit from the same source(this will be needed for you to compile some native binaries)

3. Install Thin server(gem install thin)

4. Set up dropbox deployment for your application(i know this is monkeypatching as hell, but if it works…) as described on Rob Conery’s blog

5. Test the app and make sure everything is working ok, (run rails server), then thin start -e production, just to make sure everything is ok, and all of your gems work on windows(fix or replace the ones that don’t)

6. Set up the task scheduler(this is a fine piece of software on Win 7 and up) as described  here, to run rails application almost as a windows service( you start with ‘thin start -e production -p 80 -c “path_to_your_app” ‘)

7. Write a nifty batch script that will restart the server, run migrations, compile the assets and whatnot.

cd "path_to_your_app"

bundle exec rake db:migrate RAILS_ENV=production

bundle exec rake assets:precompile

schtasks /End /TN "name_of_your_task_in_scheduler"

schtasks /Run /TN "name_of_your_task_in_scheduler"

8. After you pull to your dropbox synced repository, login to the server and execute the batch file from there to almost automate the deployment process.

I know this is not capistrano, and cap deploy, and it is hacked as it can be, and also, it works, it’s a small client so i don’t expect much trouble with this.

Lesson here: Try as much as you can to deploy your rails server on a linux server, best in a virtual machine. But if HP messes with you and you have some shitty hardware running windows, this is the best way i have found. If anyone has anything better, put it into comments, i’m happy to make my life easier.




Speaking at a conference with no time to prepare

I was surprised to know that i am presenting a product this week at the oracle user conference

A colleague and me were supposed to hold a talk about creating our latest web app, a nursery management app created using Ruby on Rails and Oracle XE. As we thought, the talk was canceled and we were free of all obligation. But no, this saturday, 5 days before the talk, i was reviewing the agenda and found out the talk is on. So what can i do?

I will walk you through the steps, though they shouldn’t be used unless you are in a crunch, and it is not longer than an hour, luckily my talk is only half an hour.

1. Gather as much irrelevant info about the company and the client(our somewhat of a manager sorted that out)

2. If you are the developer, try to talk as much as you can about the technology and its integration( I love Rails so that is no problem )

3. Scrape out the basic workflow of the application you are presenting, user experience and why is something done in that way.

4. Take as much screenshots as it takes

5. Talk about details in the application, and finally leave at least 5 minutes for the audience to ask questions.

I managed to cramp all that into 30 minutes, i will update this post after i’m done thursday morning, to write how the talk went.

Lesson for everyone: Know your talking schedule at least one month before you are doing a talk, this way you can prepare and practice.

Update: The talk went surprisingly well, the hall was almost full, with 24 people listening. I haven’t got my reviews yet, but i feel they will be great for my first conference talk.

Rails 3.1 JQuery-UI

If you are considering the upgrade to Rails 3.1.x and you wish to include the JQuery-UI in your web application follow these simple steps:

Load jquery-ui in your app/assets/javascripts/application.js

//= require jquery
//= require jquery_ujs
//= require jquery-ui
//= require_tree .

Pick and download a nice theme from: JQuery-UI themes site

Copy jquery-ui-1.8.16.custom.css(version number could differ) to your vendor/assets/stylesheets folder as jquery-ui.css (This will save you time later when you are updating or changing the theme) and include it in your app/assets/stylesheets/application.css

*= require_self
*= require jquery-ui
*= require_tree .

Copy images from the images folder to vendor/assets/images

Modify vendor/assets/stylesheets/jquery-ui.css and replace “images/” to “/assets/”

Now that you have everything set up and ready, go ahead and use the fancy JQuery-UI plugins

Readonly checkboxes with JQuery

I have encountered a problem where i needed to disable some checkboxes on my forms. As you know, a disabled html item by design isn’t going to be posted back. So i needed to figure out another solution. So with the disabled tag, we have a readonly tag, which just blurs the checkbox, but lets the user click on it and change the value(i was testing this in Chrome, i don’t know what happens in other browsers).

The solution is:

1. Make your checkboxes readonly by setting the readonly attribute to true

<%= check_box_tag "some_id", :readonly => true %>

2. Put this javascript in your document.load so the click will be overridden:

// Javascript:

$(':checkbox[readonly="readonly"]').click(function() {
return false;

// Coffeescript:

$(':checkbox[readonly="readonly"]').click ->

Autocomplete fields in Rails 3.1 with JQuery-UI pt. 2

As i have written in my previous post, i’m playing with autocompletion in my web app. I have found some new tricks, that will speed up the process. Never liked the find_by_name implementation, but as this example is derived from jquery.autocomplete plugin behavior, i had no other way. Did some more exploring today and here is what i found out.
Because you are using json response on the ajax controller, and JQuery-UI autocomplete handles it perfectly, you can create a hidden field, and put the user_id there, so you have one less request to the database whena you are saving your record.
The improved model:

class Post < ActiveRecord::Base belongs_to :user attr_accessor :user_name def user_name if user_id end end [/ruby] And the part in your view that i left out the last time, but this one is just better in my opinion: [html] < %= f.label :user_name %>
< %= f.text_field :user_name %>
< %= f.hidden_field :user_id %>
Also, you have to edit the CoffeScript that does the magic for you, so it will return the id into the hidden field:

$(document).ready ->
    source: "/ajax/users"
    select: (event,ui) ->

An easier way of doing it, and leaner on the database queries, at least one less.