---
title: Improving Rails boot time with Zeus
teaser:
tags: web,ruby,rails,testing
author: Dan Croak
published_on: 2013-01-10
---

[Zeus](https://github.com/burke/zeus) improves Rails boot time. Saving seconds
is most important when running focused tests:

    rspec spec/models/user_spec.rb
    rspec spec/models/user_spec.rb:123

Those are times when a tight feedback loop make a meaningful difference.

## Install

Install the Zeus gem on your machine:

    gem install zeus

Do not include it in your `Gemfile`. It is an external piece of software.

## Set up

Initialize:

    zeus init

This will create two files in your Rails app's directory. [Ignore them
globally](https://thoughtbot.com/blog/post/18739402579/global-gitignore) in
`~/.gitignore`:

    custom_plan.rb
    zeus.json

Edit `zeus.json` to include only the tasks for which you'll use Zeus. Mine
looks like this:

    {
      "command": "ruby -rubygems -r./custom_plan -eZeus.go",

      "plan": {
        "boot": {
          "default_bundle": {
            "development_environment": {
              "prerake": {"rake": []},
              "console": ["c"],
              "generate": ["g"]
            },
            "test_environment": {
              "test_helper": {"test": ["rspec"]}
            }
          }
        }
      }
    }

I remove `cucumber` in favor of [RSpec and
Capybara](https://thoughtbot.com/blog/post/33771089985/rspec-integration-tests-with-capybara).

I remove `server` in favor of [Foreman and
Pow](https://thoughtbot.com/blog/post/40110176152/foreman-as-process-manager-pow-as-dns-server-and-http).

## Force the test environment

In `spec/spec_helper.rb`, change:

    ENV['RAILS_ENV'] ||= 'test'

To:

    ENV['RAILS_ENV'] = 'test'

## Remove auto-running code

The goal is to run tests in the context of Zeus. So, remove other similar
systems.

From the RSpec docs:

> Generally, life is simpler if you just use the `rspec` command. If you
> must use the `ruby` command, however, you'll want to do the following:

    require 'rspec/autorun'

> This tells RSpec to run your examples.

We don't need this behavior and can cause bugs when used with Zeus.

Remove either of these lines in `spec/spec_helper.rb` if they exist:

    require 'rspec/autorun'
    require 'rspec/autotest'

## Remove Spork and Guard

For the same reasons, delete Spork and Guard from your `Gemfile`, delete your
`Guardfile`, and delete any related Spork code in `spec/spec_helper.rb` or
`spec/support/`.

## Start Zeus

Zeus will need to be running before you can use its commands:

    zeus start

I usually run this, and other [long-running processes in a tmux
session](https://github.com/thoughtbot/dotfiles/pull/84).

Now, those original commands will have the benefit of Rails boot time in under
a second:

    zeus rspec spec/models/user_spec.rb
    zeus rspec spec/models/user_spec.rb:123

## Bonus: run specs from vim

Many of us are running specs directly from vim. If you edit your [`~/.vimrc` to
use Zeus like in this
commit](https://github.com/thoughtbot/dotfiles/commit/80b77cdefc75d271c7c41f82118a8cfea6642755),
you can run focused specs with:

    t

Enjoy!
