Ruby on Rails on Debian

Posted by ssbrady on Mon 9 Jan 2006 at 13:49

Most of you have probably heard of Ruby on Rails and may be wondering what exactly it does and how you can try it for yourself. Put simply, Rails is a web application framework that uses the model-view-controller software design pattern to allow for rapid development of web applications. This article will cover how to install Rails on Debian and how to configure it to work with Apache and a relational database of your choice.

Introduction and Key Terms

Ruby is an interpreted, object-oriented language inspired by Ada and Perl and shares some features with Python and LISP. Rails is a web application framework written in Ruby. Rails uses the model-view-controller software design pattern and provides a framework for the fast development of maintainable web-based, database-driven applications. Rails supports: advanced views using AJAX; many different databases, including MySQL, PostgreSQL and Oracle; and many different web servers, including Apache and lighttpd.

Key Terms

  • Ruby
    • interpreted, object-oriented language
    • over ten years old
  • Rails
    • web application framework
  • Model View Controller
    • software design pattern
    • data model, user interface, and control logic
  • AJAX
    • Asynchronous Javascript And XML
    • e.g. GMail and Google Maps
  • Gems
    • package management for Ruby

Installing Ruby, Gems and Rails

Ruby

Debian Stable contains a prepackaged version of Ruby. In addition to installing Ruby, you also need to install the zlib compression libraries for Ruby, the Ruby Documentation program that generates documentation from ruby source files, and the command-line Ruby interface. Run the following command to install the needed packages:

aptitude install ruby libzlib-ruby rdoc irb

(You might prefer to use "apt-get install ruby libzlib-ruby rdoc irb" if you don't use aptitude.)

If you want to test-drive Ruby from the command line you can run the irb program and execute Ruby commands:

# irb
rb(main):001:0> 3+5
=> 8
irb(main):002:0> 4*20
=> 80
irb(main):003:0> foo="bar"
=> "bar"
irb(main):004:0> foo.reverse
=> "rab"
irb(main):005:0> bar=40
=> 40
irb(main):006:0> bar.to_s.reverse
=> "04"
irb(main):007:0> exit

Gems

Now that you have Ruby installed you need to manually download the Gems package management software for Ruby. The latest version of Gems can be downloaded from the RubyForge web site.

From the command line, cd into the directory where you downloaded the Gems tarball and run the following command to extract the contents of the archive:

tar zxvf rubygems-0.8.11.tgz

Please note that the version number may be different if you downloaded a different version. You now need to cd into the newly created Gems directory and run the setup program for Gems:

cd rubygems-0.8.11
ruby setup.rb all

Notice how even the setup program for Gems is written in Ruby. These people take their language seriously.

Rails

Now that Gems is installed, you can finally install Rails. Run the following command to tell Gems to download and install the latest version of Rails:

gem install rails --include-dependencies

Congratulations, Ruby on Rails is now installed on your system.

Apache

While you can use many different web servers with Rails, we're going to configure our installation with Apache. If you want to configure Rails with a different web server, a quick Google search will yield results on how to configure Rails with other web servers.

The following command will install Apache and the FastCGI libraries for both Apache and Ruby:

aptitude install apache libapache-mod-fastcgi libfcgi-ruby1.8

(Again, you may use apt-get if you wish: "apt-get install apache libapache-mod-fastcgi libfcgi-ruby1.8".)

The Debian package manager will automatically update Apache's configuration file to enable FastCGI and will start the web server. That's all you need to configure in Apache for right now. We will revisit Apache later in the article when we've made more progress.

Now you need to create a place to store your Rails applications. While you can store your Rails applications anywhere you want, including your home directory if desired, I recommend storing them in /var/rails. Why /var? Because /var is where Apache on Debian stores its web site files (/var/www to be exact) and I like keeping similar files in the same directory.

The following commands will create the Rails application directory and set the proper permissions for their use by the web server:

mkdir /var/rails
chown -R www-data:www-data /var/rails

Database

Ruby on Rails supports multiple databases. The following is a list of some of the packages that supply database drivers for Ruby:

  • libmysql-ruby
  • libpgsql-ruby
  • libsqlite3-ruby

For example, to install the Ruby drivers for PostgreSQL, you would run the following command:

aptitude install libpgsql-ruby

If you want to verify that the drivers are properly installed, you can run the following set of commands:

# irb
irb(main):001:0> require 'postgres'
=> true
irb(main):002:0> exit

You can see that the driver loaded successfully and we were able to exit out of the Ruby interface.

RadRails: A Rails IDE

If you're considering using Rails it's probably because of the promises of a faster development cycle (that's certain not a bad thing). To further speed up your development time you may consider using an IDE (integrated development environment) like RadRails. The RadRails IDE is based on the Eclipse project and has been customized specifically for Ruby on Rails development. If RadRails sounds like something you'd like to try, read on, otherwise, skip on down to the Creating a Rails Application section and fire up your favorite editor.

Java

Because RadRails is written in Java, you need to install Java on your system. The latest version of Java can be downloaded from Sun's website.

Once you've downloaded the self-extracted archive, run the following command to extract the archive's contents:

bash jdk-1_5_0_06-linux-i586.bin

You now need to move the extracted directory into the /usr/local directory:

mv jdk1.5.0_06 /usr/local

In order to make the Java executables accessible from your path, you need to create symbolic links for all the Java programs. Fortunately, you can use the wildcard and your shell will automatically expand all the programs and create the links for you.

ln -s /usr/local/jdk1.5.0_06/bin/* /usr/local/bin/

To test that Java is installed on your system and accessible from the path, run the following command.

java -version

You should see a few lines telling you about the version of Java you just installed. If you get an error, please go back and make certain you followed the steps correctly.

An alternative approach is to create a Debian package, and use that to install your Java install. This has been covered here before.

RadRails

You now need to download the RadRails archive from their website.

After the download is complete, change into the directory where you downloaded the archive to and execute the following command:

unzip radrails-0.5.2.1-linux.zip

If unzip isn't installed on your system you need to install the package:

aptitude install unzip

(Or "apt-get install unzip".)

Once you've successfully extracted the contents of the archive, execute the following command to move RadRails into its destination directory:

mv radrails /usr/local

RadRails is now installed. You can run RadRails by executing the following command:

/usr/local/radrails/radrails

Note: Because RadRails is a Java program it doesn't like being called from a symbolic link. If you create a symbolic link to a directory in your path and then try to run the program it will spit out errors and crash. I guess that's the downside of write-once, run-anywhere.

Creating a Rails Application

Now you're ready to create your first Rails application. All the Rails files that require editing can be opened in any editor (e.g. vi, emacs, etc.) or can be edited with RadRails. Use whichever program is most comfortable for you. However, I do not recommend using RadRails to edit non-Rails files (e.g. httpd.conf). Non-rails files should be edited with a normal editor.

Before you run any commands, change into the rails directory that you created earlier.

cd /var/rails

Now you can tell Rails to create a new application. Replace yourapp with the name of your application:

rails yourapp

Rails will now create a large number of files in /var/rails/yourapp that will make up your Rails application. Let's start out by editing the .htaccess file in order to tell your application to use FastCGI instead of regular CGI.

Edit the file /var/rails/yourapp/public/.htaccess and make the following changes:

Comment out: RewriteRule ^(.*)$ dispatch.cgi [QSA,L]
Insert: RewriteRule ^(.*)$ dispatch.fcgi [QSA,L]

Notice that the different between the two lines is the extension on the dispatch file. We've changed it so your application will use the FastCGI version.

WEBrick

Ruby on Rails has a built-in web server called WEBrick that you can use to test out your Rails applications. While WEBrick is good for testing, you wouldn't want to use it in a production environment. For the real deal you will want to use a real web server like Apache or lighttpd.

To start WEBrick, run the following command:

script/server

Now you can fire up your web browser and load the following URL to access the web server:

Configuring Apache

You now have two different options for configuring Apache to use your Rails application. You can configure your application is a virtual host which will not effect the main web site, or you can make the rails application the main web site. If you want to test Rails on a web server that is already serving web sites then you will want to use the virtual host option. If your Rails application is the only thing being served by your web server then you can make it the main web site.

Virtual Host

Open up the /etc/apache/httpd.conf file and add the following configuration information at the end of the file:

<virtualhost *:80>
   ServerName yourapp
   DocumentRoot /var/rails/yourapp/public/
   ErrorLog /var/rails/yourapp/log/apache.log

   <directory /var/rails/yourapp/public>
      Options ExecCGI FollowSymLinks
      AddHandler cgi-script .cgi
      AllowOverride all
      Order allow,deny
      Allow from all
   </directory>
</virtualhost>

Save the file, exit from your editor and run the following command to restart Apache:

/etc/init.d/apache restart

You now need to edit your /etc/hosts file so the name you gave your server will resolve. Either add the following line to your hosts file or add yourapp to the end of the existing line:

127.0.0.1 yourapp

Now open up your web browser and load the following address:

You should see a page welcoming you to Ruby on Rails!

Entire Web Server

Open up the /etc/apache/httpd.conf file and make the following change:

Comment out: DocumentRoot /var/www
Insert: DocumentRoot /var/rails/yourapp/public

You will also need to add the follow configuration to the end of the file:

<directory /var/rails/yourapp/public>
   Options ExecCGI FollowSymLinks
   AddHandler cgi-script .cgi
   AllowOverride all
   Order allow,deny
   Allow from all
</directory>

Save the file, exit from your editor and run the following command to restart Apache:

/etc/init.d/apache restart

Now open up your web browser and load the following address:

You should see a page welcoming you to Ruby on Rails!

Developing your Application

This article isn't going to cover how to write a Rails application. There are a million resources out there that can get you started developing with Rails. If you want some demonstrations of what can be done with Rails in less than half an hour, take a look at the ScreenCasts on the Ruby on Rails web site.

Ruby on Rails documentation, including web sites, books, and other resources, can also be found on the Ruby on Rails Documentation web page.

Further Reading

About this Article

This article is a companion piece to a presentation given at the January 2006 meeting of the Syracuse Linux Users Group. The latest version of this article can be found on the SyrLUG web server.

 

 


Posted by apropos (82.3.xx.xx) on Mon 9 Jan 2006 at 14:15
[ Send Message ]
Actually the rails package in Unstable works fine for me, which I prefer because I hate the way gem messes up my Debian.

One can also tweak the build of gem to keep it in /usr/local:

1) After downloading RubyGems, configure with:
ruby ./setup.rb config --prefix=/usr/local --sysconfdir=/usr/local/etc --siteruby=/usr/local/lib/site_ruby
Then ruby ./setup.rb install

It's a good idea not to do this as root; set up perms on /usr/local appropriately. Just in case gem tries to violate the FHS!

2) export GEM_HOME=/usr/local/lib/ruby/gems

3) gem install rails

4) export RUBYOPT=rubygems

[ Parent | Reply to this comment ]

Posted by ssbrady (24.235.xx.xx) on Mon 9 Jan 2006 at 14:50
[ Send Message ]
I'm very squeamish about installing packages from other branches on my stable system. I value my uptime too much. :-) If I need a program that isn't available from the Debian package repository I prefer to install it manually. To each his own.

p.s. Thanks for the tip about installing Gems in /usr/local.

[ Parent | Reply to this comment ]

Posted by apropos (82.3.xx.xx) on Mon 9 Jan 2006 at 20:39
[ Send Message ]
Yes, I agree about mixing branches. It would be nice to have a Rails installation managed entirely by APT. I've got a setup I can work with on Unstable, but still with too much stuff in /usr/local.
The make-jpkg approach mentioned by Anon is the way to go, and I vaguely remember reading some discussions about a similar thing in the pipeline for Gems packages.

[ Parent | Reply to this comment ]

Posted by dapatrick (70.20.xx.xx) on Mon 16 Jan 2006 at 02:05
[ Send Message ]
I found it helpful to execute these commands in the following order:

$ export GEM_HOME=/usr/local/lib/ruby/gems/1.8; echo "export GEM_HOME=/usr/local/lib/ruby/gems/1.8" >> /etc/profile
$ ruby ./setup.rb config --prefix=/usr/local --sysconfdir=/usr/local/etc --siteruby=/usr/local/lib/site_ruby
$ ruby ./setup.rb install
$ gem install rails --include-dependencies
$ export RUBYOPT=rubygems; echo "export RUBYOPT=rubygems" >> /etc/profile
$

Placing the GEM_HOME environment variable declaration before the call to ruby ./setup.rb install to create the gems directory structure under /usr/local/lib/ruby/gems/1.8 rather than /usr/lib/ruby/gems/1.8.

[ Parent | Reply to this comment ]

Posted by dapatrick (70.20.xx.xx) on Mon 16 Jan 2006 at 02:11
[ Send Message ]
This was in response to comment #1.

[ Parent | Reply to this comment ]

Posted by Anonymous (195.98.xx.xx) on Tue 13 Jun 2006 at 15:53
don't forget to run "ruby ./setup.rb setup" before "ruby ./setup.rb install"

[ Parent | Reply to this comment ]

Posted by Anonymous (195.98.xx.xx) on Thu 15 Jun 2006 at 11:36
finally I need create symlink in /usr/lib/ruby/gems to /usr/local/lib/ruby/gems/

[ Parent | Reply to this comment ]

Posted by Anonymous (66.54.xx.xx) on Mon 9 Jan 2006 at 18:47
You might want to use the debian packager for java. it's a little cleaner than just letting the Sun installer go wild.

Package: java-package
...
Description: utility for building Java(TM) 2 related Debian packages
This package provides the capability to create a debian package from
a Java(TM) 2 distribution by just running make-jpkg <filename>.

[ Parent | Reply to this comment ]

Posted by Anonymous (83.176.xx.xx) on Wed 11 Jan 2006 at 18:07
hi,

two little questions to this tutorial.

1. is it possible to work with ruby on rails also on apache2? i've tried to edit the /etc/apace2/sites-available/default, but it didnt work.

2. can i use the rails-package from testing, like here explaint?
http://wiki.rubyonrails.com/rails/pages/RailsOnDebianStable

thanx,
minero

[ Parent | Reply to this comment ]

Posted by Anonymous (67.166.xx.xx) on Thu 12 Jan 2006 at 13:30
Yes, you may run Ruby on Rails on Apache 2.0.x although it's advised to run on the 1.3.x branch because of FastCGI issues. I run my blog and other applications on Apache 2.0.54 and Sarge myself.

This is a great article and I consider this to be the prefered way of installing Ruby on Rails.

[ Parent | Reply to this comment ]

Posted by Anonymous (67.166.xx.xx) on Thu 12 Jan 2006 at 13:42
I forgot to add, put this in your main apache2.conf or httpd.conf for each Rails application:

FastCgiServer /railsapp/public/dispatch.fcgi -idle-timeout 120 -initial-env RAILS_ENV=development -processes 2

Tweak processes and timeout as required.

[ Parent | Reply to this comment ]

Posted by Anonymous (83.176.xx.xx) on Fri 13 Jan 2006 at 10:11
Wen I put it in the apache2.conf and restart the apache2-server, the error.log-file says:

Invalid comand 'FastCgiServer'

Is there a package missing or a mistake in my configuration?

minero

[ Parent | Reply to this comment ]

Posted by Anonymous (67.166.xx.xx) on Fri 13 Jan 2006 at 14:37
Yes. You need to ensure that the FastCGI Apache module is loaded and properly configured.

this is what I have in /etc/apache2/mods-enabled/fastcgi.load:

LoadModule fastcgi_module /usr/lib/apache2/modules/mod_fastcgi.so


and in /etc/apache2/mods-enabled/fastcgi.conf:

IfModule mod_fastcgi.c>
AddHandler fastcgi-script .fcgi
FastCgiIpcDir /var/lib/apache2/fastcgi
</ifmodule>


this is also the same for /etc/apache2/mod-available/fastcgi.load and mod_fastcgi.so

Also, I'm not completely sure the procedure or order for using a2enmod but you may enable the fastcgi module using this. ex. a2enmod fastcgi

Hope this helps.

[ Parent | Reply to this comment ]

Posted by Anonymous (83.176.xx.xx) on Fri 13 Jan 2006 at 15:40
Yes, this was my mistake. I forgot to enable these module.

Thank you.
minero

[ Parent | Reply to this comment ]

Posted by Anonymous (72.147.xx.xx) on Wed 18 Apr 2007 at 05:15
FastCGI works fine on Apache 2.x if you use mod_fcgid.

mod_fastcgi != mod_fcgid

Only mod_fastcgi has issues with Apache 2, while mod_fcgid was designed to proved FastCGI with Apache 2.

Thus:

Use mod_fastcgi with Apache 1.3.x because it has "issues" with Apache 2.0.

Use mod_fcgid with Apache 2.x because it is designed for Apache 2.x.

[ Parent | Reply to this comment ]

Posted by Anonymous (81.214.xx.xx) on Sat 14 Jan 2006 at 23:04
Thanks for a great howto, but libapache-mod-fastcgi cannot be found via apt here, on freshly installed debian stable. Debian website tagged that package as non-free so how do I install it? My apt sources only have repositories for stable updates and they are up to date.

Also, Rails server is incomplete without poper MTA, subversion and ssh server. I think these also should be included in this otherwise perfect howto. Thanks!

I really appreciate any help here.

[ Parent | Reply to this comment ]

Posted by Anonymous (66.93.xx.xx) on Tue 17 Jan 2006 at 09:15
I had the same problem. I added the following 2 lines to my /etc/apt/sources.list file and then I was able to "apt-get install libapache-mod-fastcgi":

deb http://ftp.debian.org/debian/ stable non-free
deb-src http://ftp.debian.org/debian/ stable non-free

After you add these lines you will need to do an "apt-get update" before you "apt-get install libapache-mod-fastcgi".

[ Parent | Reply to this comment ]

Posted by Anonymous (65.31.xx.xx) on Sun 22 Jan 2006 at 04:11
The article should mention that you can create a bash script to run radrails instead of a symlink.

~capiCrimm

[ Parent | Reply to this comment ]

Posted by Anonymous (24.77.xx.xx) on Thu 25 May 2006 at 15:31
sweet article, thanks. to get apache serving up my rails app, i also had to do:

# ln -s /etc/apache2/mods-available/rewrite.load /etc/apache2/mods-enabled/

i don't know if this applies to everyone, though...

[ Parent | Reply to this comment ]

Posted by Anonymous (66.80.xx.xx) on Mon 17 Jul 2006 at 21:44
as a short cut:
# a2enmod rewrite

[ Parent | Reply to this comment ]

Posted by barryhawkins (216.27.xx.xx) on Sat 10 Jun 2006 at 22:49
[ Send Message ]
Both rails and rubygems are packaged for Debian now, so this article can be updated to reflect the simplification of setting up Rails.

[ Parent | Reply to this comment ]

Posted by Anonymous (192.147.xx.xx) on Thu 23 Nov 2006 at 14:54
I had some real problems getting this to work inside a virtual host on apache.

Fast CGI doesn't seem to work well with virtual hosts, so it might be a good idea to make a note of this in the guide. Took me awhile to debug and revert to using normal CGI.

Does this problem exist under apache 2?

[ Parent | Reply to this comment ]

Posted by Anonymous (212.117.xx.xx) on Fri 31 Aug 2007 at 14:13
I've installed everything as explained, but when I start

sushi:radrails# ./RadRails
./RadRails: error while loading shared libraries: libgtk-x11-2.0.so.0: cannot open shared object file: No such file or directory

but the lib still exists:

sushi:radrails# locate libgtk-x11-2.0.so.0
/usr/lib/libgtk-x11-2.0.so.0
/usr/lib/libgtk-x11-2.0.so.0.800.20


Any help would be n1^^

[ Parent | Reply to this comment ]

Posted by Anonymous (24.190.xx.xx) on Wed 9 Jan 2008 at 14:25
When attempting to use gem to install rails, I got an error that said "SSL not installed"

In order to fix this, you have to install the libopenssl-ruby package. That cleared it right up for me.

[ Parent | Reply to this comment ]

Posted by Anonymous (188.22.xx.xx) on Mon 21 Feb 2011 at 10:25

This install is a bit prehistoric. Check out the script for Lenny here: https://github.com/vysogot/railsready-debian-lenny

[ Parent | Reply to this comment ]

Posted by Anonymous (98.251.xx.xx) on Sat 5 May 2012 at 17:06
Install steps at unlimit.in/installing-ruby-on-rails-on-debian.html

[ Parent | Reply to this comment ]

Posted by unlimit1 (98.251.xx.xx) on Sun 6 May 2012 at 03:43
[ Send Message ]
I have written a log of how I installed it on my Debian Squeeze at http://unlimit.in/installing-ruby-on-rails-on-debian.html

[ Parent | Reply to this comment ]

Sign In

Username:

Password:

[Register|Advanced]

 

Flattr

 

Current Poll

Which init system are you using in Debian?






( 1625 votes ~ 7 comments )