Skip to content
Latchkey

Ruby "cannot load such file" (LoadError) in CI

Ruby could not load a file you required. Either the gem is not installed for the interpreter running your code, or you ran the script outside the bundle so its load paths are not set up.

What this error means

A script or task aborts with "LoadError: cannot load such file -- <name>". The require fails because the file is not on the active Ruby’s load path - usually a gem installed for a different Ruby, or a missing bundle exec.

Ruby traceback
<internal:/usr/lib/ruby/3.3.0/rubygems/core_ext/kernel_require.rb>:136:in
`require': cannot load such file -- nokogiri (LoadError)

Common causes

Gem installed for a different Ruby

The gem was installed under one Ruby (or globally) but the code runs under another interpreter or rbenv version whose gem path does not include it.

Running without bundle exec

Outside bundle exec, the script uses the system load path instead of the bundle’s, so gems resolved by Bundler are not on $LOAD_PATH.

A standard library now shipped as a gem

Some former stdlib libraries (e.g. csv, logger on newer Ruby) became bundled gems and must be added to the Gemfile to be loadable.

How to fix it

Run inside the bundle

Use bundle exec so the bundle’s gems are on the load path.

Terminal
bundle install
bundle exec ruby script.rb

Confirm the gem is installed for this Ruby

Terminal
ruby --version
gem list nokogiri
bundle list | grep nokogiri

Add an extracted stdlib gem to the Gemfile

If the missing file used to be standard library, declare it explicitly.

Gemfile
# Gemfile
gem 'csv'
gem 'logger'

How to prevent it

  • Always run application code through bundle exec in CI.
  • Use one consistent Ruby across all steps.
  • Declare formerly-stdlib libraries in the Gemfile as Ruby extracts them.

Related guides

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