/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
require 'nokogiri' # a gem that is not installed
require 'helpers' # a file next to app.rb...: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
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'require_relative 'helpers' # file next to this file
$LOAD_PATH.unshift File.join(__dir__, 'lib')
require 'mylib' # now found in ./librequire vs require_relative vs load
| Call | Searches | Loads twice? |
|---|---|---|
| require 'x' | $LOAD_PATH and installed gems | No (tracked in $LOADED_FEATURES) |
| require_relative 'x' | The directory of the current file | No |
| load 'x.rb' | Current directory and $LOAD_PATH; needs the extension | Yes, every call |
Sources
Related pages
commands · bundlerbundle exec: run a command with the Gemfile's gem versionsbundle exec CMD runs a command with exactly the gem versions in Gemfile.lock, avoiding conflicts with other installed versions. When you need it, and binstubs.errors · rubyRuby NoMethodError: undefined method for nilRuby raises NoMethodError undefined method for nil when a method is called on a value that is nil, often a failed lookup. Find the nil source; use &. or fetch.errors · rubyRuby: uninitialized constant Name (NameError)Ruby raises NameError uninitialized constant when a class or module is used before it is loaded or is misspelled. In Rails, the file name must match the class.flags · rubyruby -r and -I: require a library and add to the load path from the command lineruby -r requires a library and -I adds a load path before the script runs. Fix cannot load such file for local code, plus RUBYLIB, RUBYOPT and bundler/setup.