---
title: "Fixing require LoadError as an example for the matrix gem"
description: "Some default gems like matrix may be removed from Ruby releases. How to handle this on project upgrades or usage?"
url: "https://qameta.com/posts/fixing-require-loaderror-as-example-for-matrix-gem/"
image: "https://qameta.com/images/posts/require-matrix-gem-load-error.png"
---

# Fixing require LoadError as an example for the matrix gem

![cannot load such file matrix LoadError](https://qameta.com/images/posts/require-matrix-gem-load-error.png)

Some gems like [matrix](https://rubygems.org/gems/matrix) or
[rexml](https://rubygems.org/gems/rexml) were added at some point to ruby
becoming so-called default gems. But they may be removed from being default in
newer ruby versions, resulting in broken builds like for us with
[prawn](https://rubygems.org/gems/prawn):

```sh
bin/rails c
'require': cannot load such file -- matrix (LoadError)
```

In most such cases you have to update the used gem (for us prawn) to a newer
version, which defines the missing gem as required for this ruby version.

If there is no update, you can mostly fix this by adding the missing gem to your
[Gemfile](https://bundler.io/guides/gemfile.html) using a command:

```sh
bundle add matrix
bundle install
```

Or just add an entry to your [Gemfile](https://bundler.io/guides/gemfile.html):

```diff
+ # missing dependency for prawn since ruby 3.1
+ gem "matrix"
gem "prawn"
```

And run:

```sh
bundle install
```

Now your project should may work again.

## Some background around default and bundled gems

Default gems are gems that are part of Ruby and you can always require them
directly. The special thing is, you can't remove them from your ruby
installation.

Bundled gems are normal gems that are installed on Ruby installation. But they
can be removed at any time by yourself or someone else.

The [matrix gem](https://rubygems.org/gems/matrix) was added to [Ruby 2.6.0 as
default Ruby gem](https://stdgems.org/matrix/) and became a [bundled gem since
Ruby 3.1.0](https://stdgems.org/matrix/).

As most installations are using the default Ruby setup, most developers don't
even notice such changes. But if you are using a customized Docker Image or a
special development machine, where such dependencies were removed for some
reason (like security problems), you have to fix it.

## Fixing by creating a pull request for a gem

The best thing is to create a pull request for the related project with
something like this to the `.gemspec` file:

```diff
+ spec.add_dependency('matrix', '~> 0.4')
```

The problem with this change is, that now your project can't be installed on
Ruby older than 3.1.0, as the previous versions have an older matrix gem
version or maybe require an older gem version in their projects (see
[sdtgems.org](https://stdgems.org/matrix/)) and [prawn issue
#1304](https://github.com/prawnpdf/prawn/pull/1304).

A better fix would add the dependency only on Ruby versions newer than 3.1:

```diff
+ spec.add_dependency("matrix", "~> 0.4") if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new("3.1")
```

But the problem is, this fix may take some time to get to a new release like it
is [for prawn since 2020](https://github.com/prawnpdf/prawn/issues/1195).

## Move on to a new gem (if you can)

If you do have not much code dependency or you can handle it, maybe it is time
to move on to a better-maintained project by migrating your code to another
solution. I'll try [hexapdf](https://rubygems.org/gems/hexapdf) for my next
PDF-related work.

At some point, you can maybe fork a stale project and try to maintain it for
yourself or even create a fork and start a new project based on the code of the
old one.

## Summary

Handling dependencies in any manner may become hard at some point. Ruby has
several solutions to get back on track or have a fix, which may gain you some
time to find a better solution.

In most cases, it is enough to add such a gem to your
[Gemfile](https://bundler.io/guides/gemfile.html) and get your
project running again like it was for me with prawn, Ruby 3.2, and the missing
matrix gem in a Docker image by running:

```sh
bundle add matrix
bundle install
```

But prawn is now on a watch list as it may become at some point a road blocker
if the maintainer(s) don't find a solution to go on.
