RVM and Bundler on Dreamhost

UPDATE This post is stale and at least partially incorrect. Check out my updated setup instead.

So, I run all my sites on DreamHost, and I've been a pretty happy customer for years. Very little downtime, fairly responsive to support questions, great price, and about as much control over a server as you can get without being root.

But… there's no root access, so sometimes you have to play some tricks. My site WhalePail is on Dreamhost. It runs on Ruby/Sinatra. Getting it working the first time took a little work. I use Ruby every day for work, and so I've been using Bundler for awhile now. Also, I have been getting into RVM, since I actually am maintaining a couple different projects with different ruby version/library needs. Anyway, I made a bunch of updates to WhalePail and wanted to get RVM and Bundler running on Dreamhost. I pretty much got it working, and here's what I did.

First, I installed RVM:

bash <<(curl -s https://rvm.beginrescueend.com/install/rvm)

Then, I used it to download and compile ruby 1.8.7. This is the same version of Ruby as is running on DH now. I don't think this would work with Ruby 1.9 unfortunately. I also setup a gemset for my sinatra project.

rvm install ruby-1.8.7-p334 rvm gemset create sinatra rvm use 1.8.7@sinatra

Install bundler next: ` gem install bundler `

Then I setup a Gemfile for the app. There's one important thing here, which is to match the version of Rack that DH has: ` gem "rack", "=1.2.1" `

Run bundle install to get your gems loaded.

I have a couple scripts that run alongside the app. In order to make sure they're running the same version of ruby, you can create an .rvmrc file that specifies what version you want: echo "rvm use 1.8.7@sinatra" > .rvmrc

Next, you need to add a couple special lines to your config.ru file. Mine looks like this:

` #!/usr/bin/env ruby

require 'rubygems'

ENV['GEM_HOME']="/home/username/.rvm/gems/ruby-1.8.7-p334@sinatra" ENV['GEM_PATH']="/home/username/.rvm/gems/ruby-1.8.7-p334@sinatra:/home/username/.rvm/gems/ruby-1.8.7-p334@global"

require 'rubygems' Gem.clear_paths

require 'bundler' Bundler.require

require 'mufftweet' run MuffTweet `

The important stuff here is the hardcoded ENV variables, and also the Gem.clear_paths. At this point, you're actually running Dreamhost's pre-installed ruby via passenger. After you hardcode the path to custom gems, and clear out any preset paths via the call to clear_paths, the only gems loaded will be your bundle.

Finally, if you need something to run in cron, you can do it like this:

*/10 * * * * . ~/.bash_profile; cd /home/username/appname; /home/username/appname/cron.rb

The ". ~/.bash_profile" bit loads in the RVM functions that you will need to run anything in cron.

Filed under: Code, ruby