Installing Ruby on Rails Stack on Debian Testing
Ruby on Rails can be incredibly easy to install for development purposes. However, there are lots of choices when you anticipate your needs in production that can quickly add complexity. After a fair amount of research, I’ve settled on the following stack:
- Debian Testing – very close to the next release of stable at this point, necessary for Mongrel installation
- Ruby 1.8.4, etc. – only available on testing
- Mongrel – considered the successor to lighthttpd for best webserver to use with RoR
- Ruby on Rails – from the trunk under version control rather than a specific release, will lock down to a release later
- MySQL 5.0.24 – the latest on Debian Testing
- Globalize – Rails plugin to support internationalization, from trunk under version control
- Subversion – for managing my source, as well as dependencies to external source code
- Emacs and Ruby and Rails Support in Emacs
- misc. supporting packages
This stack isn’t yet complete. I’ll need to add a load balancer to dish requests out to the pack of mongrel processes. I’ve skipped this step and put off that decision until I get ready to deploy my application. I can use either Mongrel without a proxy or WEBrick for development.
Disclaimer: I’m new to both Ruby and Ruby on Rails. So please take my advice with caution. This worked for me, but may not work for you.
Now let’s get down to the nuts and bolts:
Note: there is text wrapping for the code snippets. Be careful with cutting and pasting. Also note that there are a few steps that are spelled out in paragraphs rather than code snippets, these usually involve editing a file.
Let’s assume that you have already installed a clean instance of Debian Stable (Sarge) to work with (in my case on a Xen instance). The first step is upgrading to Testing.
Edit /etc/apt/sources.list to testing instead of sarge, then do the following. Obviously this assumes that you have the sudo package installed and your user has full privileges in it.
$ sudo apt-get install apt-utils
$ sudo apt-get install emacs21
$ sudo apt-get install subversion subversion-tools libsvn0-dev libsvn0
$ sudo apt-get upgrade
$ sudo apt-get install gnupg # apt-get complained that we needed it
$ sudo aptitude update
$ sudo aptitude upgrade
$ sudo dpkg –purge exim4
$ sudo apt-get install postfix # follow prompts
$ sudo apt-get -t testing install ruby irb rdoc ri ruby1.8-dev libzlib-ruby libopenssl-ruby1.8
Now edit /etc/apt/sources.list by adding deb http://www.sgtpepper.net/hyspro/deb unstable/ as the last line.
$ sudo apt-get upgrade # not really necessary, but doesn’t hurt
$ apt-get install rubygems
$ apt-get install build-essential
Now we move onto Rails installation via rubygems (Ruby package manager, standard for Rails) rather than Debian’s apt-get. There is a Rails apt-get package if you want to stick with apt-get. We’ll be developing with EdgeRails (source code from trunk under version control) in our project, but in order to do that you have to install the latest stable release of Rails first.
We’re going to go with Mongrel (http://brainspl.at/articles/2006/04/26/dead-simple-deployment for details) for webserver cluster with some sort of proxy front end which is why we are using Testing and need to install ruby1.8-dev (to get ruby 1.8.4). Also see here for more details: http://mongrel.rubyforge.org/docs/debian-sarge.html.
Select the latest ruby version. Now let’s doublecheck our work and test.
Edit /etc/profile and add :/var/lib/gems/1.8/bin to PATH, do this for any already existing users’ .bash_profile files, too.
$ rails test_app
$ cd test_app/
$ script/generate controller HelloWorld index
Edit app/views/hello_world/index.rhtml to look like:
<p><%= Time.now.to_s(:long) %></p>
From another shell on the same host…
After you accept the cookie, you should get “Hello World” with the current time. If not, sorry, this worked for me.
Quit lynx and in the shell where you started mongrel, hit Control-C to shut it down. Then blow away the test app.
$ rm -rf test_app # blow away test app
Time to add MySQL and it’s necessary bindings for Ruby.
Change your mysql root password!
Let’s wrap up our package downloading for a bit with Imagemagick stuff for Ruby.
My project is going to require the ability to represent non-latin characters, localization, and internatilization. This is sort of dicey in Ruby because it doesn’t support Unicode natively, but there are workarounds. In this section we’ll install and set them up. See the links at the bottom of the article for details.
First up, add Unicode package to Ruby.
MySQL needs to be reconfigured to use Unicode, too. Make utf8 the default by editing /etc/mysql/my.cnf and adding this in the [mysqld] section:
init-connect = ‘SET NAMES utf8′
character-set-server=utf8
collation-server=utf8_general_ci
Restart mysql with /etc/init.d/mysql restart. Create a test database and user. Login to
that database as the test user and run this query:
n the results if you see include this, you are all good:
character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
| collation_connection | utf8_general_ci |
| collation_database | utf8_general_ci |
| collation_server | utf8_general_ci
…
Go ahead and drop your test db and user.
We aren’t quite done with internationalization, but we are close. We’ll need the Globalize plugin for Rails. We will return to that after we get the skeleton of our new app checked into version control.
If you don’t have one yet, setup a Subversion repository for your new application. In my case this was done on a different server. I’ll be using ssh to talk to the repository, so I don’t need any of Subversion’s server functionality. Here’s how I did it:
This is as root on the host where the repository is going to live…
$ chown -R root.staff /usr/local/svn-your-project # note that you will need a staff group for this or you can create or use a group that is logical for your work
$ chmod -R 775 /usr/local/svn-your-project
This is in the development are on a different host…
(optional) If you haven’t already, create a ssh key pair for your development host, place it in ~/.ssh/authorized_keys on the repository machine, this makes using subversion remotely much more pleasant.
Unless you have done it already, you probably want to set SVN_EDITOR variable in your
environment. For bash do this with SVN_EDITOR=/path/to/editor; export SVN_EDITOR and/or add the above to ~/.bash_profile. In my case, of course, it was Emacs.
Ok, time to put the initial skeleton of our application into subversion.
$ rails your_application
$ cd your_application
$ svn import . svn+ssh://the_remote_repository_host/usr/local/svn-your-project -m “initial import of app” # specify –username remote_username if your local username is different than on the remote repository
$ cd ..
$ rm -rf your_application # just a bare rails skeleton, no need to back it up
$ svn checkout svn+ssh://the_remote_repository_host/usr/local/svn-your-project your_application
$ cd your_application/
$ svn remove log/$
$ svn commit -m ‘removing all log files from subversion’
$ svn propset svn:ignore “$.log” log/
$ svn update log/
$ svn commit -m ‘Ignoring all files in /log/ ending in .log’
$ svn propset svn:ignore “$” tmp/sessions tmp/cache tmp/sockets
$ svn commit -m ‘Ignoring all files in /tmp/’
$ svn move config/database.yml config/database.example
$ svn commit -m ‘Moving database.yml to database.example to provide a template for
anyone who checks out the code’
$ svn propset svn:ignore “database.yml” config/
$ svn update config/
$ svn commit -m ‘Ignoring database.yml’
We add our external sources as dependencies to our repository, this way subversion
knows to update them as well without us having to manage vendors source code.
First we grab Ruby on Rails from it’s trunk. From here on out, I’ll use the short form of svn commands, like ci for commit, etc.
$ svn ci -m “Setting externals property to grab RoR trunk”
$ svn up
(Read, but skip for now) Next up, the Globalize plugin.
Currently (September 27th, 2006) Globalize from the trunk doesn’t work with Rails 1.1.X. There is a branch for compatibility, but I’m waiting for the merge from the branch back to the trunk. See http://globalize-rails.org/wiki/ for details. Here’s how you would do it, if the trunk was Rails 1.1.x compatible:
$ svn ci -m ‘added plugin globalize’
$ svn up # watch as subversion grabs the latest externals sources from their respective repositories
Note: if you no longer want a plugin, you can uninstall it with ruby script/plugin remove plugin_name and it will even handle the subversion removal. Caveat: Earlier versions of remove or the code in your plugin may not do the right thing and clean out data, etc.
Globalize has a bunch of base tables that need to be added to your database in order to work. We’ll create our databases, add the Globalize data model, and then finish with our Emacs enhancements. After that we should be ready to start our development with RoR in earnest.
Create the databases:
$ mysqladmin -u root -p create your_application_test
$ mysqladmin -u root -p create your_application_production
Rails will need to know our password for accessing mysql…
$ cp database.example database.yml
Edit database.yml with passwords.
$ rake db:migrate # should return “(in /path/to/where/your/app/is)”, if so you are set
(Read, but skip for now) Finally, we finish our internationalization by loading the Globalize data-model
Note: if you get an error about an index name being to long, try replacing line 68 of vendor/plugins/globalize/tasks/data.rake with this:
Edit config/environment.rb for Globalize. Add the following to the end.
Locale.set_base_language(’en-NZ’) # and here
If you want to use Emacs with Ruby on Rails, you’ll want to add some nice extensions that people have added. Here’s how to do it:
First ruby, there is a debian package that will install the ruby .el files and make sure the
modes are available at startup for all users:
Now Rails:
$ cd
$ wget http://rubyforge.org/frs/download.php/9994/emacs-rails-0.39.tar.gz
$ tar -xvfz emacs-rails-0.39.tar.gz
$ wget http://www.kazmier.com/computer/snippet.el
$ wget http://www.webweavertech.com/ovidiu/emacs/find-recursive.txt
$ mv find-recursive.txt find-recursive.el
$ sudo mv find-recursive.el snippet.el /usr/local/share/emacs/21.4/site-lisp/ # I’m guessing
this where you should put .el files that you want to have all user have access to, but not
mixed in with emacs distribution code…
$ sudo cp emacs-rails/*.el /usr/local/share/emacs/21.4/site-lisp/
Now add the following to your .emacs file:
(lambda()
(add-hook ‘local-write-file-hooks
‘(lambda()
(save-excursion
(untabify (point-min) (point-max))
(delete-trailing-whitespace)
)))
(set (make-local-variable ‘indent-tabs-mode) ‘nil)
(set (make-local-variable ‘tab-width) 2)
(imenu-add-to-menubar “IMENU”)
(require ‘ruby-electric)
(ruby-electric-mode t)
))
(defun try-complete-abbrev (old)
(if (expand-abbrev) t nil))
(setq hippie-expand-try-functions-list
‘(try-complete-abbrev
try-complete-file-name
try-expand-dabbrev))
(require ‘rails)
;; If you want to use Mongrel instead of WEBrick, add this to you .emacs file:
; (setq rails-use-mongrel t)
The next time you start Emacs, you should have a pretty sweet setup for editing Ruby and Rails.


September 27th, 2006 at 12:19 pm
Sorry for the unindented lisp code. I recommend copying it to your *scratch* buffer in emacs which is lisp and then doing meta-x indent-region over the it, before adding it to your .emacs file.
Also, note the # comments in the code snippets. They are there for explanation and don’t need to be pasted to the shell.
Ok, ok, I have a tendency to state the obvious…
Cheers,
Walter
October 2nd, 2006 at 9:04 am
pstickne form #rubyonrails irc channel had this to say:
“should recommend emacs 22cvs”
“html-mode is actually usable”
“also negative points for using mysql in general
”
I assume he prefers PostgreSQL, a view I’m not adverse to, but he could also mean Oracle.
Cheers,
Walter
December 5th, 2006 at 12:41 pm
[...] Installing Ruby on Rails Stack on Debian Testing [...]
December 24th, 2006 at 8:56 am
Now that Ruby on Rails 1.2 RC1 is out, you may prefer to work with that rather than Edge. Here’s how:
$ svn propedit svn:externals vendor
# edit the file to look like this for your rails line:
rails http://dev.rubyonrails.org/svn/rails/tags/rel_1-2-0_RC1
# then you’ll want to update your svn checkout, from your rails root dir
$ svn update vendor
That should do it.
Cheers,
Walter
January 3rd, 2007 at 10:54 am
I’m repeating these steps for another development setup and found that this line:
sudo apt-get install subversion subversion-tools libsvn0-dev libsvn0
needed to be changed to this:
sudo apt-get install subversion subversion-tools libsvn-dev libsvn1
Cheers,
Walter
January 28th, 2007 at 3:26 pm
[...] This assumes you have already installed the subversion package by following the directions outlined here. [...]
January 29th, 2007 at 9:38 pm
[...] This assumes that you have already installed things outlined here. $ sudo apt-get install swig $ sudo apt-get install libsvn-ruby1.8 $ sudo gem install redcloth [...]
October 12th, 2007 at 11:19 am
If you are installing the Kete stack, a more up to date version of these instructions can be found here:
http://kete.net.nz/documentation/topics/show/15
Keep an eye on http://kete.net.nz/ for more information on installing Kete on various host types.
Cheers,
Walter