Quick Left

- Back To Site
Thanks for signing up!
Subscribe to our RSS

Rails 3.1, Sprockets, and Heroku

Saturday, May 7, 2011

The Rails 3.1 Beta is out. So far, it's pretty cool, but I like to rapidly deploy my working applications to Heroku in order to minimize the develop-to-deploy feedback loop.

Initial attempts

Unfortunately, it isn't as straight-forward as one would assume. Rails 3.1 depends on Sprockets, which uses execjs for its operations. This means that Rails 3.1 depends on some form of JavaScript interpreter running.

My initial attempts were met with complete failure - pushing up revealed that there was no JS implementation on Heroku, causing it to fail. So I did some research, and tried to install The Ruby Racer and add it to my gemfile.

There was no luck with that - The Ruby Racer actually builds V8, and Heroku's console truncates the GCC command, causing the compile to fail.

Extreme frustration

After this, I discovered The Ruby Racer Heroku build, which comes with a pre-compiled V8. Cool. I added it to my Gemfile, and attempted to bundle. This failed horribly, cauisng compilation errors all over the place. Turns out, if you install it locally, it still tries to build.

I searched everywhere for my particular problems with no answer - until I attempted to install V8 directly through Homebrew. It failed horribly, and it had to do with Cinderella.

Great Success!

For rapidly setting up my machine, I use Cinderella. It enables me to setup my development environment fairly well with one command, and more importantly it's namespaced in my Home directory, enabling me to wipe my environment at any time.

This namespace broke the V8 compilation, and The Ruby Racer build. I removed Cinderella, installed homebrew and RVM by hand, and got myself back up in running in half an hour. brew install V8 passed this time.

The final problem was documented - I attempted to install The Ruby Racer again, and was met with another compile error. Apparently, if you have v8 installed, The Ruby Racer will attempt to use that build's header files instead of its own. After removing V8, it installed just fine, and with a quick push, we were up to Heroku.

Heroku will also install the rails3-static-assets plugin, which allows static asset serving. For one reason or another, this fails in Rails 3.1.

In your production.rb environment file, just set:

config.serve_static_assets = true

And you're off to the races. With Sprockets, you won't have much of anything in your public directory anyway.

That's it!

The tl;dr:

  • Use therubyracer-heroku gem for production.
  • Don't use Cinderella.
  • Don't have V8 installed independently.
  • Enable Rails static asset serving with: config.serve_static_assets = true

Glad you liked it. Would you like to share?

Sharing this page …

Thanks! Close

Showing 2 comments

Sort by   Subscribe by email   Subscribe by RSS
  • Hey, don't know how helpful this is to you, but if you put `therubyracer-heroku` in a production only group in bundler like so:

    group :production do
    gem 'therubyracer-heroku'
    end

    Then you do this on your local machine:

    bundle --without production

    Then it means that therubyracer-heroku won't even try and install on your local environment, and everything will still work perfectly on Heroku. Had this same problem myself recently so hope it helps!

    Michael Smith
  • Pro tip Michael thanks.

Add New Comment

Trackback URL 
Gerred
By:
Gerred Dillon