speersj
speersj

Reputation: 178

Making a Ruby Gem - Cannot Load Such File

I'm attempting to build a Ruby gem using the instructions at http://guides.rubygems.org/make-your-own-gem/. The following seems to work fine and a *.gem file is generated.

gem build mygem.gemspec

The following also appears to be successful (only if prefaced with sudo):

sudo gem install mygem-0.0.1.gem

However, when I attempt to require 'mygem' inside irb, I get the following error:

LoadError: cannot load such file -- mygem

I've seen similar errors around Stackoverflow but haven't been able to figure out what's going wrong in my specific situation. I am able to require other gems (not mine) without problems. My gem does show up in the output of gem list but it will not load with require.

FWIW I am using rbenv, which is brand new to me.

Here is the output of gem env:

Gemspec:

Gem::Specification.new do |spec|
    spec.name        = 'mygem'
    spec.version     = '0.0.1'
    spec.date        = '2015-01-05'
    spec.summary     = "mygem" 
    spec.description = "Attempting to build a gem"
    spec.authors     = ["speersj"]
    spec.email       = # my email here
    spec.files       = ['lib/command.rb', 'lib/connection.rb']
    spec.homepage    = ''
    spec.license     = 'MIT'
end

Upvotes: 15

Views: 13883

Answers (3)

matt
matt

Reputation: 79803

The spec.files entry of your gemspec doesn’t include the mygem.rb file, so that file won‘t be in the gem when it is built. Only files listed in this entry will be included in the final gem.

The simplest solution would be to just add mygem.rb to the array:

spec.files = ['lib/command.rb', 'lib/connection.rb', 'lib/mygem.rb']

This is a fairly simple fix, you might want to do something more flexible like using a Dir glob:

spec.files = Dir['lib/**/*.rb']

In fact the Rubygems guide suggests you do something like this (text is from the end of that section):

If you’ve added more files to your gem, make sure to remember to add them to your gemspec’s files array before publishing a new gem! For this reason (among others), many developers automate this with Hoe, Jeweler, Rake, Bundler, or just a dynamic gemspec.


Also, you really do need to fix your permissions problem, you shouldn’t need sudo to install gems into your own home directory.

Upvotes: 26

SaraVanaN
SaraVanaN

Reputation: 329

Make sure you added all modified files into github repo before build your gem And then install the build gem.

Upvotes: 1

the Tin Man
the Tin Man

Reputation: 160611

You can't use sudo to install a gem when using rbenv (or RVM), except for the "multi-user" or "system-wide" type installations which are specialized and very seldom what normal/regular users should be using.

sudo escalates your privileges to root, and root has no knowledge of the Rubies in a user's rbenv environment. As a result, root will use the default system Ruby, which will install the files there.

Instead, use a basic gem install, which will do the right thing.

Upvotes: 2

Related Questions