Ruby CSV LoadError: Fix the Library and Bundle

Diagnose a missing CSV library, declare the dependency in the application bundle and distinguish loading errors from input file and parser problems.

Written by Technically reviewed by Jim Freeze · Reviewed

A Ruby CSV LoadError occurs when Ruby cannot load the CSV library or a feature requested while loading it. For a Bundler application, include the CSV gem in the Gemfile and install the bundle. Next, run the failing command in that same environment. Examine the exact missing path before changing dependencies. A missing data file is a separate problem.

This can appear after a Ruby upgrade during an import or report export. CSV changed from a default gem to a bundled gem in Ruby 3.4. That distinction matters when Bundler manages the gems available to the application. Ruby has not abandoned support for CSV files.

Ruby CSV LoadError diagnosis separating library loading, bundle dependencies and input file access
Load the library first, verify the application bundle, then investigate the input file.

Find what the Ruby CSV LoadError actually names

Begin with the missing feature in the exception. If the exception identifies as CSV, inspect whether the library is available. If the exception identifies as some other feature, use the stack trace to determine which feature was requested by your code. Simply installing CSV again would not resolve a different missing dependency within the import workflow.

Don't begin by modifying the spreadsheet. Libraries are loaded before they have the opportunity to parse the spreadsheet. A corrupted spreadsheet row cannot possibly explain why Ruby cannot find the entry point for the library. Similarly, renaming a report file will not make a missing gem accessible to the interpreter.

The diagnostic below catches the loading exception and displays its class and requested path. It produced the shown result in the official Ruby 4.0.0 WASI runtime used here, where CSV was unavailable. An installation able to load CSV takes the other branch. These results describe this runtime, not every Ruby 4.0 installation.

begin
  require "csv"
  p "CSV loaded"
rescue LoadError => error
  p [error.class.to_s, error.path]
end
# ["LoadError", "csv"]

Utilize the rescue to collect evidence, NOT to mask a failed import. Returning an empty list after a loading failure may create an incomplete report that looks successful. In application code allow failures to reach their corresponding error handlers after collecting useful diagnostic information.

Declare CSV in the application bundle

For projects using Bundler, place the CSV dependency in the Gemfile and install it through the normal project process. The maintained CSV project's installation instructions describe both the Gemfile declaration and standalone installation. Include it with the other libraries your application needs.

# Gemfile
gem "csv"

# Shell, from the application directory
bundle install

Select a maintained release that corresponds with all versions of Ruby your project utilizes. Don't duplicate an outdated version pin simply because it existed in an answer to this very same error. Inspect any resulting lockfile change, record your intended dependency modifications and rebuild your deployment artifact through your regular methods.

Also check the dependency group. An import task running in production needs its dependencies there. A gem declared only in a group excluded from the installation may be absent. Declare dependencies according to where tasks run, rather than where they happened to be developed.

The installation commands are instructions for your environment. They were source-checked, but no native gem installation or deployed application was executed for this guide. After installation, run the original operation in a fresh process. Installation success alone does not prove application compatibility.

Why Ruby CSV LoadError can persist after installation

A global gem installation and an application's bundle are separate dependency environments. A developer shell may find CSV while a bundled worker cannot. The official bundle exec reference explains how Bundler exposes the bundle's gems while preventing unrelated system gems from entering that environment.

Test through the startup path the application actually uses. Removing Bundler from the command changes the environment; it does not repair the application's dependency declarations. The next clean deployment could fail again even if your current shell works.

ruby --version
bundle exec ruby --version
bundle exec ruby -e 'require "csv"; puts CSV::VERSION'

The final command is a diagnostic instruction, not a result from the WASI test. Execute it from the application directory with the intended Gemfile. Record the Ruby version and CSV version printed by these checks. If loading fails, retain the exception rather than substituting a version from documentation.

When a scheduling task fails but your terminal succeeds, compare the build used for your deployment, working directory and interpreter selection chosen by your application. A scheduling task may execute under a different account or executable. Re-run this comparison inside the actual failing environment before reinstating a library already located elsewhere.

Separate library errors from file and parser errors

Failure stageInvestigation needed
Loading the CSV libraryRequested feature, dependency declaration and active bundle
Opening the input fileFile path, working directory and access permissions
Parsing the input dataCSV format, encoding and parser options

Each failure stage demands different types of evidence. After loading the library, an unavailability of an input path is no longer a library-loading problem. After opening the file, a parsing failure necessitates investigating both input data and parser configurations. Maintain both the exact type of exception and its associated message along with each operation that raises it.

For further insight into how Ruby locates requested features, refer to Ruby's cannot-load-such-file error guide. Understanding this distinction will help prevent considering a library name as if it were an actual path to an uploaded spreadsheet.

Do not replace your CSV parser with a simple comma split simply to bypass an installation issue. Comma separated values can contain quoted values that include both commas and line breaks. A loading failure does not eliminate these cases nor does it affect the data format. Restore your intended dependency before determining whether any adjustments to your parser settings are necessary.

Check who owns the failing require

The stack trace may route through a report gem versus your own application. Evaluate the report gem's declared dependencies and supported releases. There may already exist an updated version that declares CSV explicitly. Choose targeted updates based upon compatibility instead of updating every dependency simply to observe whether this error resolves itself.

If you temporarily add CSV as an application dependency to resolve your immediate problem, document why it exists as such. This provides future maintainers reasons to retain or remove it once the upstream issue has been resolved. Modifying files inside installed gems can be unreliable: cleaning up your installation can discard patches you applied locally.

Libraries intended for other applications should specify their runtime dependencies in their gem specifications. An application's Gemfile is not a suitable substitution for dependency metadata provided to consumers of that library. Determine how that library intends to be integrated prior to selecting which declaration to adjust.

Try the Ruby CSV LoadError decision exercise

Suppose a report command succeeds under plain Ruby but fails under Bundler, naming CSV as the missing feature. The same data file is available in both runs. What should you check first: the delimiter, the bundle's CSV dependency, or the column headings? Choose before reading the explanation.

Reveal the next diagnostic step

First check the bundle's CSV dependency. The observed difference is the dependency environment, and the failure concerns library loading. Delimiters and column headings belong to a later parsing stage. Changing either would not explain why plain Ruby finds the library while the bundled command cannot.

Confirm the Gemfile and installed bundle instead of assuming the cause is established. If the dependency is present, review the intended Gemfile, runtime and startup command. A useful next step narrows the investigation. It does not turn a single observation into a guarantee about the entire deployment.

Verify the repair with the original operation

After changing dependencies, start a fresh process through the original entry point. Run the import or export that failed with representative sample input. Validate the actual rows or output as well as the exit status. Reaching application startup alone does not verify the CSV workflow.

Keep the original failing command and the successful replacement together in your notes. Change one relevant aspect of the setup at a time where practical. Several simultaneous changes can produce a working build without revealing which repair mattered. That uncertainty makes a later failure harder to diagnose.

The execution evidence covers the missing-library diagnostic on Ruby 4.0.0 WASI. It does not demonstrate successful CSV parsing, native installation, filesystem access or a production Bundler deployment. Validate those parts in the environment where the application will run.

Takeaway

Repair a Ruby CSV LoadError at the loading stage: identify the missing feature, declare the dependency and verify the active bundle. Then rerun the real operation in a fresh process. Investigate input file access and CSV syntax after the library is available.