Bootstrapping a Dreamhost Account for Rails and Git

Monday, September 8th, 2008

I had three clients who needed Ruby on Rails applications running on their Dreamhost accounts. No big deal, I thought, since Dreamhost has supported Rails applications for quite some time and they were instrumental in developing Passenger.

Well, was I in for a surprise.

Two of the clients had complicated Rails applications with native gem dependencies, and the third wanted me to develop an app for him that would be much quicker with some of these gems, too. Dreamhost may offer one of the most flexible shared hosting environments on the planet, but sometimes you REALLY wish you had sudo privileges. I plan to write an ode to apt-get some time in the near future.

So I set out toward what turned out to be a 5+ hour ordeal. Out of the kindness (and some pride) of my heart. I share my installation notes with you, the internets, in hope of sparing the same pain and suffering I endured. In the end, you will have Git 1.5.4, Rails 2.1.1, Ruby 1.8.7 and peace of mind.

First, I needed git to work so I could have a local repository in each of the Dreamhost accounts. Yes, I could have them sign up for a Github.com account, but I wanted to save them money and I thought git was already installed. It was. But it was version 1.4.4, and it had some weird quirks. You didn’t type git init to initialize a git repository. You typed git init-db. I remember using git back before 1.4.4 but I don’t remember that syntax. Regardless, I kept getting “corrupt repository” errors when I tried to use the local git to access my pushed changes. Argh!

So, I used these commands to compile and setup my own private copy of git 1.5.4. (inspired by this link). I had to compile git with NO_MMAP because Dreamhost thinks that git uses too much memory with it’s memory mapping technique and kills the process. I also didn’t need libcurl for the install. Git works just fine pulling submodules from Github without it.

mkdir ~/tmp
cd ~/tmp
wget http://www.kernel.org/pub/software/scm/git/git-1.5.4.rc4.tar.gz
tar xzvf git-1.5.4.rc4
cd git-1.5.4.rc4
./configure --prefix=$HOME/.packages NO_CURL=1 NO_MMAP=1
make
make install

All of the resources I could find on custom ruby/gems installations were old but worked with few modifications. Here’s the inspiration for these instructions.

First, setup .bash_profile the way you like it. I did it this way:

umask 002
PS1='[\h:$PWD]$ '
alias ll="ls -l"
EDITOR="/usr/bin/nano"
. .bashrc

Then, setup .bashrc with the proper paths. This file is executed by all non-shell logins. That would include git and capistrano.

export TZ=EST5EDT # Sets my timezone to Eastern U.S. time
export LD_LIBRARY_PATH="$HOME/.packages/lib"
export PATH="$HOME/.packages/bin:$HOME/.gems/bin:${PATH}"
export GEM_HOME="$HOME/.gems"
export GEM_PATH="$GEM_HOME:/usr/lib/ruby/gems/1.8"

Install readline for console support. You can’t do script/console without it.

cd ~/.packages
wget http://ftp.gnu.org/gnu/readline/readline-5.2.tar.gz
tar zxvf readline-5.2.tar.gz
cd readline-5.2
./configure --prefix=$HOME/.packages
make
make install

Install openssl

cd ~/tmp
wget http://www.openssl.org/source/openssl-0.9.8h.tar.gz
tar zxvf openssl-0.9.8h.tar.gz
cd openssl-0.9.8h
./config --prefix=$HOME/.packages --openssldir=$HOME/openssl shared
make
make test
make install

Install Ruby. Substitute any earlier version if you need it. 1.8.7 was fine for me.

cd ~/.packages
wget ftp://ftp.ruby-lang.org/pub/ruby/1.8/ruby-1.8.7-p72.tar.gz
tar zxvf ruby-1.8.7-p72.tar.gz
cd ruby-1.8.7-p72
./configure --prefix=$HOME/.packages --with-openssl-dir=$HOME/.packages --with-readline-dir=$HOME/.packages
make
make install

Install Rubygems

cd ~/.packages
wget http://rubyforge.org/frs/download.php/38646/rubygems-1.2.0.tgz
tar zxvf rubygems-1.2.0.tgz
cd rubygems-1.2.0
ruby setup.rb config --prefix=$HOME/.packages
ruby setup.rb setup
ruby setup.rb install

Setup default gems. Feel free to install any others you need.

gem install rails hpricot rspec mysql ruby-debug ruby2ruby rake

Setup your Rails Environment. This is the most crucial. Even though you now have the most up to date rails gems in your path, you still need to freeze them in your app if you are using Passenger like I am.

rake rails:freeze:gems

Add the following lines to the top of your config/environment.rb. Remember to replace “USER” with your username.

ENV["GEM_HOME"]="/home/USER/.gems"
ENV["GEM_PATH"]="/home/USER/.gems:/usr/lib/ruby/gems/1.8"

Also, make sure you have your gem dependencies configured correctly. Passenger can get a little confused even if you have them installed in your local gem repository.

Hope this helps!

UPDATE:

Peter Boling was kind enough to point out that you should also install the “rake” gem. I added it to the gem install list above.