Installation of a Rails platform

This is a short documentation which explains how to set up a Rails environment in a Unix "home". Expected usage is to have one account per application: it makes it very easy to maintain parallel environmnents with their own Gems and such, while sharing a server and the basic infrastructure like the Ruby interpreter and core libs.

For Ruby 1.8.X

Because its repository and install paths are setup at install time, we need to install and build it with GEM_HOME already set into our user account.

Recommended: 'apt-get remove rake rubygems libgems-ruby1.8'

# su - ror

cd ~
mkdir gem
export GEM_HOME=~/gem
tar xzf rubygems-1.8.17.tgz
cd rubygems-1.8.17
ruby1.8 setup.rb --prefix=~

Set up a useful symlink:

cd ~/bin
ln -s gem1.8 gem

In ~/.bashrc (and no other file), add:

export EDITOR=vim
alias less='less -R'
alias grep='grep --color=auto'
alias ls='ls --color=auto'
alias ll='ls --color=auto -la'

source ~/ruby-env

Create a ~/ruby-env file (which can be sourced from your cronjobs):

export GEM_HOME=~/gem
export RUBYLIB=~/lib
export PATH=~/bin:$GEM_HOME/bin:$PATH
export APP_ROOT=~/CHANGE_ME/current
export THIN_CONF=~/CHANGE_ME/shared/thin.yml

if [ -z "$LANG" ]; then
  export LANG=en_US.UTF-8

# Comment this if you're not on a production environment!
export RAILS_ENV=production

reload() {
    pushd ~/prod/current && bundle exec thin restart -C ~/prod/shared/thin.yml

stop() {
    pushd ~/prod/current && bundle exec thin stop -C ~/prod/shared/thin.yml

alias maintenance="touch $APP_ROOT/public/maintenance/ENGAGED"
alias no_maintenance="rm $APP_ROOT/maintenance/ENGAGED"

Create a ~/.gemrc file:

:bulk_threshold: 1000
:update_sources: true
:verbose: true
:backtrace: false
:benchmark: false
gem: --no-ri --no-rdoc

And finally, a ~/.vimrc config:

set bg=dark
set bs=2
set dir-=.
set ek
set ff=unix
set is
set ls=2
set lz
set nojs
set pt=<F11>
set wildmenu
set wildignore+=*.o,*.so,*.a,.svn,*.pyc

...and log in again. You can now proceed to install local gems as you wish with gem install:

gem install bundler

If you use thin and you want to be sure that your services are restarted after a reboot, you have to use an init script. Below is an init script tested successfully on Debian (Lenny and Squeeze), working with the ~/ruby-env used above :

# /etc/init.d/thin: start/stop thin instances for $user
# Provides:          thin
# Required-Start:    $network
# Required-Stop:     $network
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6



case "$1" in
    echo -n "Starting Thin instances for user '$user'"
    su -l -c "source ~/ruby-env; reload" $user
    echo "."
    echo -n "Stopping Thin instances for user '$user'"
    su -l -c "source ~/ruby-env; stop" $user
    echo "."
    echo -n "Restarting Thin instances for user '$user'"
    su -l -c "source ~/ruby-env; reload" $user
    echo "."
    echo "Usage: /etc/init.d/thin {start|stop|reload|restart|force-reload}"
    exit 1

exit 0

For Ruby 1.9.X

Most of the setup is identical, except that you don't have to download and build rubygems locally, since it comes bundled with Ruby 1.9.X.

You can follow the same steps as for 1.8.X, except for the first script (download and build rubygems) and the symlink in ~/bin. Also, note that ~/ruby-env no longer needs the RUBYLIB definition. Apart from that, it is identical :

export GEM_HOME=~/gem
export PATH=~/bin:$GEM_HOME/bin:$PATH
export APP_ROOT=~/CHANGE_ME/current
export THIN_CONF=~/CHANGE_ME/shared/thin.yml

Last modified 9 years ago Last modified on Mar 6, 2012, 5:40:27 PM