Ruby on Rails. On Ubuntu.

Ruby on Rails logoHere’s how to do it.

I had a Ubuntu box running 6.06 (Dapper) and a vanilla install of Apache2, PHP5 and MySQL5. I wanted to dabble in Ruby on Rails using the same box, but not affect the “production” side of it.

Despite the normally trivially easy install of software on Ubuntu I banged my head against a wall for hours trying to get it working… I just could not find a simple and complete example. I know I’ll probably find that some clever clogs can reduce all of this to 5 keystrokes, but I couldn’t. So this is a complete and detailed walk-through of how to get Ruby on Rails running on an existing Ubuntu server.

My definition of success was to have a noddy “Hello World” running on a URL such as http://192.168.0.4:3000 while http://192.168.0.4/ continued to run my normal media server (i.e. via port 80) I want complete independence between then. Here goes…

First off is the “Do I use apt-get and the standard repositories or not?” It seems that while some stuff must be obtained via apt-get from the usual places, other parts can be obtained but are better grabbed via alternative methods. Why? Don’t ask me, I’m not sure. I am loathe to go “outside” of the standard Ubuntu install tools, as it’s one more management tool to worry about, but it really does seem simpler to follow the initial instructions as per the Ruby On Rails main web-site.

Let’s assume that you have Apache2, PHP5 and MySql5 already up and running. And by running, I mean demonstrably working! Really. Get to that point FIRST! Also install PhpMyAdmin for managing the MySql.

Now to add Ruby On Rails.

RubyGems

First download and use RubyGems, a package manager for Ruby on Rails analogous to apt-get. Do a:
wget http://rubyforge.org/frs/download.php/11289/rubygems-0.9.0.tgz
(note that this was the latest version at the time of writing, but you can check-out http://rubyforge.org/frs/?group_id=126 to be sure)

Untar and cd into the directory rubygems-0.9.0.
Since ruby itself should be there by default, then do:
sudo ruby setup.rb
(in fact I *think* it’s there by default on a normal workstation installation. However on a Ubuntu ‘server’ install, it is not. However easily fixed by a “sudo apt-get install ruby”)

Rails

Now do a
sudo gem install rails --include-dependencies
This bit can take several minutes, as stuff is downloaded and installed. Sit tight and let it finish.When I do this on a virgin system I get:

/usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:27:in `gem_original_require’: no such file to load — rdoc/rdoc (LoadError)

which is worrying. However if the install command is repeated, all seems well. Fingers crossed…

Apache

This is the point where we deviate from the simple documentation (which advises using the RoR built-in server) – we want to get up and running with Apache2.

Let’s (arbitrarily) decide that the root for our rails-world will be /var/www/rails (since our standard web-server is already running out of /var/www).
cd /var/www
and do a
sudo mkdir rails

Now this is going to be a private server, so security is not a major concern. I want easy remote access to the Ruby files, for example so I can edit them via a Samba share from another machine. To that end I want the owner of the files to be my normal Ubuntu logon, and the group to be the standard group for Ubuntu Apache, namely www-data. So:
sudo chown sgroarke:www-data rails
where ‘sgroarke’ is of course your Ubuntu logon id.

Test Application

Now to create a template application, as per the standard RoR getting-started information:
rails /var/www/rails/helloworld
cd rails/helloworld
and have a poke around.

Apache again

Terrific, but we can’t actually serve it yet. We need to create a virtual server for Apache2:
cd /etc/apache2/sites-available
vi rails
(or use the editor of your choice)

The contents of this file will be:

NameVirtualHost *:3000
<VirtualHost *:3000>
ServerAdmin webmaster@localhost
ServerName rails
DocumentRoot /var/www/rails/helloworld/public/
ErrorLog /var/www/rails/helloworld/log/server.log

<Directory /var/www/rails/helloworld/public/>
Options ExecCGI FollowSymLinks
AllowOverride all
Allow from all
Order allow,deny
</Directory>

<Directory /var/www/rails>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>

ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory “/usr/lib/cgi-bin”>
AllowOverride None
Options ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all
</Directory>

# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel debug

ServerSignature On

Alias /doc/ “/usr/share/doc/”
<Directory “/usr/share/doc/”>
Options Indexes MultiViews FollowSymLinks
AllowOverride None
Order deny,allow
Deny from all
Allow from 127.0.0.0/255.0.0.0 ::1/128
</Directory>
</VirtualHost>

– Enable it via
sudo a2ensite rails

– Need to make sure that the Apache2 Fast CGI modules is installed and enabled:
sudo apt-get install libapache2-mod-fcgid

– One minor Apache change which if you miss it can drive you to insanity.
cd /etc/apache2
vi ports.conf

Add a second line there that reads:
Listen 3000

– Reload Apache2:
sudo /etc/init.d/apache2 restart

and make sure no hugely nasty errors pop out.

– Go to the normal server and check it’s still OK:
http://<myserver>/

Then try the new server:
http://<myserver>:3000

Working?

– Let’s create some code.
cd /var/www/rails/helloworld
ruby script/generate controller hworld
cd app/controllers
vi hworld_controller.rb

Then add before the ‘end’ line:
def index
render_text "Hello World"
end

– Now visit http://<server>:3000/hworld
Working? Should be.

If you get an error about “Missing source file” and a mention of “ird”, then try:
sudo apt-get install irb1.8

– Finally, to avoid headaches later on (you’ve not seen the issue thus far in the tutorial, but trust me it’ll kill you when you try and connect to a MySQL database) do
sudo apt-get install libmysql-ruby1.8

Oddly the gems equivalent of this command wouldn’t work for me, so I fell back on apt-get… But it works.

I hope this helps anyone struggling to get Ruby on Rails running on Ubuntu. Feel free to ask questions or, heaven forfend, make corrections.

OK, over to you now…

Comments are closed.