Skip to content

/errors/ruby/cannot-load-such-file

Ruby: cannot load such file -- name (LoadError)

The commands on this page have not been executed by our verification runner yet.updated
tool
Ruby
exit code
1
versions
Ruby 2.x, Ruby 3.x
os
any

cannot load such file -- NAME (LoadError) means require 'NAME' found no matching file in Ruby's load path. If NAME is a gem, it is not installed for the Ruby that is running, or it is missing from the Gemfile when the script runs under Bundler: install it (gem install NAME or add it to the Gemfile and bundle install). If NAME is one of your own files, use require_relative, because require does not look in the current directory.

Example

app.rb
require 'nokogiri'      # a gem that is not installed
require 'helpers'       # a file next to app.rb
text
...:in 'Kernel#require': cannot load such file -- nokogiri (LoadError)

The part before cannot load such file is the backtrace location (often a RubyGems file) and its format changes between Ruby versions; the message itself does not.

Fixes

bash
gem install nokogiri                   # install for the current Ruby
bundle add nokogiri                    # or add it to the Gemfile and install
bundle exec ruby app.rb                # run with the gems of the Gemfile.lock
ruby -v; which ruby; gem env | grep -A3 'GEM PATHS'
ruby
require_relative 'helpers'             # file next to this file
$LOAD_PATH.unshift File.join(__dir__, 'lib')
require 'mylib'                        # now found in ./lib

require vs require_relative vs load

CallSearchesLoads twice?
require 'x'$LOAD_PATH and installed gemsNo (tracked in $LOADED_FEATURES)
require_relative 'x'The directory of the current fileNo
load 'x.rb'Current directory and $LOAD_PATH; needs the extensionYes, every call

Sources