Kernel#require

require(name) -> true or false
Returns: Boolean · Updated March 13, 2026 · Kernel Methods
loading files modules libraries

require loads and executes the named Ruby source file. It’s the primary mechanism for loading external Ruby code, including standard library modules, gems, and your own files. If the named file has already been loaded, require returns false to prevent double-loading.

Syntax

require "filename"
require "path/to/filename"
require "gem_name"

Parameters

ParameterTypeDefaultDescription
nameStringThe name of the file, gem, or library to load. Can be a relative path, absolute path, or gem name.

Examples

Loading a standard library file

require "json"

json_string = '{"name": "Alice", "age": 30}'
parsed = JSON.parse(json_string)
# => {"name"=>"Alice", "age"=>30}

# Calling require again returns false
require "json"
# => false

Loading a gem

require "httparty"

response = HTTParty.get("https://example.com")
# Makes an HTTP GET request using the httparty gem

Loading a local file

# Assume ./my_module.rb contains: def greet; "Hello!"; end
require "./my_module"

greet
# => "Hello!"

Common Patterns

Using require_relative for local files

require_relative loads files relative to the current file’s location, useful for loading project-local modules:

# In lib/my_app/helpers.rb
require_relative "utils"  # Loads lib/my_app/utils.rb

Conditional loading

# Only load optional dependency if available
begin
  require "nokogiri"
  # Use Nokogiri for XML parsing
rescue LoadError
  # Fall back to REXML or skip XML features
end

Loading multiple files at once

# Load several files in one call (Ruby 3.0+)
require "json", "yaml", "csv"

Load Path

Ruby searches for files in the $LOAD_PATH (also accessible as $:) array:

# See where Ruby looks for files
$LOAD_PATH.each { |path| puts path }

# Add a custom directory to the load path
$LOAD_PATH.unshift("/path/to/my/library")

Errors

ErrorWhen it occurs
LoadErrorThe specified file cannot be found in the load path
LoadErrorThe gem is not installed
SyntaxErrorThe loaded file contains invalid Ruby syntax

See Also