Skip to content
Latchkey

RSpec "LoadError: cannot load such file" - Fix requires in CI

Ruby could not find a file RSpec tried to require - a helper, a library, or a gem. The path is wrong, the gem is not installed in CI, or RSpec ran outside the bundle so the load path is missing entries.

What this error means

The run aborts immediately with "LoadError: cannot load such file -- X" before any example runs. The named file is a *_helper, an internal lib path, or a gem that exists locally but not in CI.

RSpec output
`require': cannot load such file -- rails_helper (LoadError)
  from /app/spec/models/user_spec.rb:1:in `<top (required)>'

Common causes

Not run under bundle exec

Running bare rspec can pick a system RSpec without the project’s load paths/gems. bundle exec rspec sets up the bundle so requires resolve.

Wrong require path or missing helper

A require 'rails_helper' relies on --require / .rspec adding spec/ to the load path; without it, or with a misnamed helper, the file is not found.

Gem not installed in CI

For "cannot load such file -- <gem>", the gem is missing because bundle install was skipped, deployment froze it out, or it is in the wrong Gemfile group.

How to fix it

Run under the bundle with the load path set

Terminal
bundle install
bundle exec rspec   # uses the project's gems and load paths

Add spec/ to the load path

Ensure .rspec adds the spec directory so require 'rails_helper' resolves.

.rspec
# .rspec
--require spec_helper
--require rails_helper
# (rails_helper itself lives at spec/rails_helper.rb)

How to prevent it

  • Always invoke RSpec via bundle exec in CI.
  • Keep .rspec requiring your helpers so paths resolve consistently.
  • Commit Gemfile.lock and run bundle install before tests.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →