15
votes

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:

  • RUBYGEMS VERSION: 2.4.5

    • RUBY VERSION: 2.1.5 (2014-11-13 patchlevel 273) [x86_64-darwin14.0]

    • INSTALLATION DIRECTORY: /Users/speersj/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0

    • RUBY EXECUTABLE: /Users/speersj/.rbenv/versions/2.1.5/bin/ruby

    • EXECUTABLE DIRECTORY: /Users/speersj/.rbenv/versions/2.1.5/bin

    • SPEC CACHE DIRECTORY: /Users/speersj/.gem/specs

    • SYSTEM CONFIGURATION DIRECTORY: /Users/speersj/.rbenv/versions/2.1.5/etc

    • RUBYGEMS PLATFORMS:

    • ruby

    • x86_64-darwin-14

    • GEM PATHS:

      • /Users/speersj/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0

      • /Users/speersj/.gem/ruby/2.1.0

    • GEM CONFIGURATION:

      • :update_sources => true

      • :verbose => true

      • :backtrace => false

      • :bulk_threshold => 1000

    • REMOTE SOURCES:

    • SHELL PATH:

      • /Users/speersj/.rbenv/versions/2.1.5/bin

      • /Users/speersj/.rbenv/libexec

      • /Users/speersj/.rbenv/plugins/ruby-build/bin

      • /Users/speersj/.rbenv/shims

      • /Users/speersj/.rbenv/bin

      • /Library/Frameworks/Python.framework/Versions/3.4/bin

      • /usr/local/bin

      • /usr/local/sbin

      • /usr/local/heroku/bin

      • /usr/local/bin

      • /usr/bin

      • /bin

      • /usr/sbin

      • /sbin

      • /usr/local/bin

      • /usr/local/smlnj/bin

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
3
What are the contents of your lib dir? Does your gem have a file there named mygem?matt
lib contains a couple of files which define classes and also includes a mygem.rb file which requires the rest of the files in lib.speersj
Try to install the gem without sudo?Jikku Jose
What does your gemspec look like?matt
Don't use sudo to install gems with rbenv (or RVM for that matter). Doing so results in the the gem being installed in the default system Ruby, not the Ruby managed by rbenv.the Tin Man

3 Answers

25
votes

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.

2
votes

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.

0
votes

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