"RubyGems were not loaded." error when attempting to install gems into the installation directory

Hello, I am attempting to install a series of rubygems from https://rubygems.org into the snapcraft installation directory, along with any additional stage-packages, so the whole installation can be distributed as a snap image. I am using the ruby snap in build-snaps and stage-snaps, instead of the ruby plugin (due to the plugin requiring core18 which is quite old).

I first tried gem install --install-dir "${SNAPCRAFT_PART_INSTALL}" ... to install the gems, but got a RubyGems were not loaded error. which prevented gem install from working.

2024-04-20T05:36:15.1792564Z + gem install --install-dir /root/parts/ronin/install ronin -v 2.0.5
2024-04-20T05:36:15.3213140Z `RubyGems' were not loaded.
2024-04-20T05:36:15.3213843Z `error_highlight' was not loaded.
2024-04-20T05:36:15.3214589Z `did_you_mean' was not loaded.
2024-04-20T05:36:15.3218625Z `syntax_suggest' was not loaded.
2024-04-20T05:36:15.3224475Z /root/parts/ronin/install/bin/gem:8:in `require': cannot load such file -- rubygems (LoadError)
2024-04-20T05:36:15.3225298Z 	from /root/parts/ronin/install/bin/gem:8:in `<main>'
2024-04-20T05:36:15.4788374Z Failed to run 'override-build': Exit code was 1.

I then tried overriding the GEM_HOME environment variable using build-environment, but received the same error:

2024-04-20T06:30:58.0302979Z + gem install ronin -v 2.0.5
2024-04-20T06:30:58.1718930Z `RubyGems' were not loaded.
2024-04-20T06:30:58.1719817Z `error_highlight' was not loaded.
2024-04-20T06:30:58.1722420Z `did_you_mean' was not loaded.
2024-04-20T06:30:58.1725681Z `syntax_suggest' was not loaded.
2024-04-20T06:30:58.1732254Z /root/parts/ronin/install/bin/gem:8:in `require': cannot load such file -- rubygems (LoadError)
2024-04-20T06:30:58.1733250Z 	from /root/parts/ronin/install/bin/gem:8:in `<main>'
2024-04-20T06:30:58.3301001Z Failed to run 'override-build': Exit code was 1.

I then finally tried to use bundler package --path "${SNAPCRAFT_PART_INSTALL}" with a simple Gemfile containing the gem I wanted to install, but got the same error!

2024-04-21T01:41:44.5765025Z + bundle package --path /root/parts/ronin/install
2024-04-21T01:41:44.9113630Z `RubyGems' were not loaded.
2024-04-21T01:41:44.9114554Z `error_highlight' was not loaded.
2024-04-21T01:41:44.9115564Z `did_you_mean' was not loaded.
2024-04-21T01:41:44.9116113Z `syntax_suggest' was not loaded.
2024-04-21T01:41:44.9220890Z /root/parts/ronin/install/bin/bundle:9:in `require': cannot load such file -- rubygems (LoadError)
2024-04-21T01:41:44.9222119Z 	from /root/parts/ronin/install/bin/bundle:9:in `<main>'
2024-04-21T01:41:45.0766368Z Failed to run 'override-build': Exit code was 1.

What is odd is that bundle package --path test_install/ works locally with ruby 3.2.2 and bundler 2.4.20.

What is the correct way to package an installed rubygem (with executables) as a snap? Link to the snap repo here. Thanks in advance.

After a lot of trial and error, and looking at other snapcraft.yml examples, I ended up having to copy the installed gems using cp -R from /var/lib/gems/ into the $SNAPCRAFT_PART_INSTALL/var/lib/gems/ and the executables installed into /usr/local/bin/ into $SNAPCRAFT_PART_INSTALL/usr/bin/, as well as explicitly setting RUBYLIB, GEM_HOME, and GEM_PATH for the app commands. Only then could ruby load rubygems and load the other gem(s).

https://github.com/ronin-rb/snap/blob/2b98d2ad9fd7077037d65d21639d057717684b5e/snap/snapcraft.yaml

Not sure if this is the best way to package a rubygem, but until the ruby plugin is updated to core20 or core22, this seems to be the only way to get it working.

2 Likes