It’s been a while since the last installment in our series of in-depth looks at the setup on WebFaction servers, so today we thought we’d take a close look at Ruby. Ruby has become a popular choice for web development and we’re pleased to support this powerful, dynamic open-source language.

To start off, let’s do a quick mic check with Ruby’s interactive interpreter, irb:

$ irb
irb(main):001:0> def hi(name)
irb(main):002:1> puts "Hello, #{name}!"
irb(main):003:1> end
=> nil
irb(main):004:0> hi("WebFactioneers")
Hello, WebFactioneers!
=> nil
irb(main):005:0> exit

Ruby’s installed on all of our servers; version 1.8.7 is the default ruby interpreter. For example, on web310:

$ ruby --version
ruby 1.8.7 (2011-06-30 patchlevel 352) [x86_64-linux]

That’s not the only version available, however. Ruby 1.8.7 Enterprise Edition is available as ruby1.8.7ee and Ruby 1.9.3 is available as ruby1.9. Or you can run Ruby 1.8.7 explicitly as ruby1.8.7.

$ ruby1.8.7ee --version
ruby 1.8.7 (2009-12-24 patchlevel 248) [x86_64-linux], MBARI 0x6770, Ruby Enterprise Edition 2010.01

$ ruby1.9 --version
ruby 1.9.3p125 (2012-02-16 revision 34643) [x86_64-linux]

$ ruby1.8.7 --version
ruby 1.8.7 (2011-06-30 patchlevel 352) [x86_64-linux]

Of course, we wouldn’t leave you on your own with just an interpreter. RubyGems, the Ruby package manager, is installed for your convenience, along with a few handy gems, such as the build tool rake.

One of the easiest ways to use Ruby on the web is with the Passenger and Rails one-click installers. They both provide ruby and gem (and other executables) in the /home/<username>/webapps/<app>/bin directory. Consider this Rails app, for instance:

$ cd ~/webapps/myrailsapp/bin/

$ ls
bundle
erb
erubis
gem
irb
passenger
passenger-config
passenger-install-apache2-module
passenger-install-nginx-module
passenger-make-enterprisey
passenger-memory-stats
passenger-status
rackup
rails
rake
rake2thor
rdoc
restart
ri
ruby
sass
sass-convert
scss
start
stop
testrb
thor
tilt
tt

But it’s not too convenient to type out whole paths for your application when it’s time to install a gem or two or run some rake commands. Instead, we recommend that you temporarily change your environment, like this:

$ export GEM_HOME=$HOME/webapps/myrailsapp/gems
$ export RUBYLIB=$HOME/webapps/myrailsapp/lib
$ export PATH=$HOME/webapps/myrailsapp/bin:$PATH

Those changes only apply during the current session; the next time you log in, the environment variables will go back to their defaults. Once the environment is ready, then it’s a simple matter of installing the gem you need:

$ gem install oauth
Fetching: oauth-0.4.7.gem (100%)
Successfully installed oauth-0.4.7
1 gem installed
Installing ri documentation for oauth-0.4.7...
Installing RDoc documentation for oauth-0.4.7...

And now it’s ready to use in the application.

For a more complete demonstration of a Rails application, you might want to give the Redmine one-click installer a try. Redmine is a Rails application as well as a handy project management tool.

We hope that this post has given you some ideas about how you can use Ruby with WebFaction. For more information, please see the Ruby on Rails documentation. If you have any questions, let us know in the comments or join us in the Q&A communtity.