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
Best Answer
The
spec.files
entry of yourgemspec
doesn’t include themygem.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:This is a fairly simple fix, you might want to do something more flexible like using a
Dir
glob:In fact the Rubygems guide suggests you do something like this (text is from the end of that section):
Also, you really do need to fix your permissions problem, you shouldn’t need
sudo
to install gems into your own home directory.