Read more

Howto: Require a gem that is not in Gemfile

Emanuel
May 10, 2017Software engineer at makandra GmbH

In case you want to require a gem, that is not in the Gemfile of you bundle and therefore not in your loadpath, you need to add the path manually and require the gem afterwards.

Expected error

Illustration UI/UX Design

UI/UX Design by makandra brand

We make sure that your target audience has the best possible experience with your digital product. You get:

  • Design tailored to your audience
  • Proven processes customized to your needs
  • An expert team of experienced designers
Read more Show archive.org snapshot

Requiring a gem, that is not in the Gemfile or .gemspec, will cause an LoadError exception:

require 'example_gem' => LoadError: cannot load such file -- example_gem

Adding a gem to the loadpath temporary

  1. You need to install the gem

gem install 'example_gem'

  1. Then you need to require the path where the gem was installed to:
$: << '/home/xxx/.rvm/rubies/ruby-x.x.x/lib/ruby/x.x.x/gems/example_gem-x.x.x'
$: << '/home/xxx/.rvm/rubies/ruby-x.x.x/lib/ruby/x.x.x/gems/example_gem-x.x.x/lib'

Note: To find out the path quickly just look at $: to get the base path of your ruby dir and then append the gem with version of the output of gem install. The Ruby version can be found out with ruby -v.

  1. Then require the gem
require 'example_gem => true

Outline

Usually the method above is just needed e.g. for debugging. For all other cases you should add the gem to Gemfile or .gemspec . There also exists a development group in the Gemfile or .gemspec, in case you do not want to have a development gem on production servers.

Posted by Emanuel to makandra dev (2017-05-10 09:11)