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:
https://rubygems.org/
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
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.
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.
Make sure you added all modified files into github repo before build your gem And then install the build gem.
Related
If I install a gem programmatically using Backticks in script with gem install gem_name as:
if not_gem_installed
`gem install my_gem`
end
require 'my_gem'
and then require the gem, a LoadError comes up. Any way to fix this?
I've tried installation of gems with backticks and found that I have the same issues as mentioned in question. I see that it's not the optimal choice for this job.
I would suggest to use Gem for search of gems and installation.
require 'rubygems/commands/install_command'
def find_or_install(gem_name)
Gem::Specification::find_by_name(gem_name)
rescue Gem::LoadError
install_gem(gem_name)
end
def install_gem(gem_name)
cmd = Gem::Commands::InstallCommand.new
cmd.handle_options [gem_name]
cmd.execute
rescue Gem::SystemExitException => e
puts "FAILURE: #{e.exit_code}"
end
https://gist.github.com/adamjmurray/3154437 - Gist with good examples
Since you are not using RubyGems, you need to require specific file from your gem's path. RubyGems overrides the require method so that it looks for paths file to include from gem paths in your Gemfile. So, make sure you specify correct path to your gem's main .rb file.
if not_gem_installed
`gem install my_gem`
end
require 'path/to/my_gem'
You can check the paths ruby will look into by printing the value of $: in your file/irb or by running gem environment command in your command line.
uzbekjon#mini:~$ gem environment
RubyGems Environment:
- RUBYGEMS VERSION: 2.5.1
- RUBY VERSION: 2.3.0 (2015-12-25 patchlevel 0) [x86_64-darwin15]
- INSTALLATION DIRECTORY: /Users/uzbekjon/.rvm/gems/ruby-2.3.0
- USER INSTALLATION DIRECTORY: /Users/uzbekjon/.gem/ruby/2.3.0
- RUBY EXECUTABLE: /Users/uzbekjon/.rvm/rubies/ruby-2.3.0/bin/ruby
- EXECUTABLE DIRECTORY: /Users/uzbekjon/.rvm/gems/ruby-2.3.0/bin
- SPEC CACHE DIRECTORY: /Users/uzbekjon/.gem/specs
- SYSTEM CONFIGURATION DIRECTORY: /Users/uzbekjon/.rvm/rubies/ruby-2.3.0/etc
- GEM PATHS:
- /Users/uzbekjon/.rvm/gems/ruby-2.3.0
- /Users/uzbekjon/.rvm/gems/ruby-2.3.0#global
- GEM CONFIGURATION:
...
=== OR with $: ===
uzbekjon#mini:~$ irb
2.3.0 :001 > $:
=> ["/Users/uzbekjon/.rvm/gems/ruby-2.3.0#global/gems/did_you_mean-1.0.0/lib", "/Users/uzbekjon/.rvm/rubies/ruby-2.3.0/lib/ruby/site_ruby/2.3.0", "..."]
Alternatively
You can require simply use bundler in your script.
if not_gem_installed
`gem install my_gem`
end
require 'rubygems' # <= This line here
require 'path/to/my_gem'
I'm in the process of writing my first ruby gem and I'm a little new at the whole structure of setting up gems. My understanding from reading this guide is that in order to create an executable for my gem I need to do the following:
Add a line to my gemspec specifiying the name of the executable like this:
s.executables << 'gemname'
Build the gem with
gem build gemname.gemspec
Install the gem locally (if you try to install by pushing to rubygems each time you'll end up having to change versions constantly) with
gem install gemname-0.0.1.pre.gem
Then test it out with
gemname foo-arguments, --bar-options
If I go through all these steps then on #4 I get the following error:
$ gemname
zsh: command not found: gemname
Assuming that the executable a file starting with a shebang and located at gemname/bin/gemname
Everything works just fine if I navigate to the root of the gem folder and run bin/gemname to test out the executable.
Here is a gist of my current gemspec and the gem source is available on github here.
You need to add all the files that need be included with the gem in spec.files. You missed to add the files that you have in the bin directory.
For example, I have the following configuration from one of my gems:
Gem::Specification.new do |spec|
spec.files = Dir["{bin,lib}/**/*", "LICENSE", "README.md"]
spec.test_files = Dir["spec/**/*"]
spec.require_paths = ["lib"]
spec.executables = ["yarr"]
end
Your gemspec is trying to change the load path. I would suggest fixing that first, because what you're reporting seems to be consistent with a gem not being able to find its files.
Look at your gemspec for this code:
lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
Then see http://guides.rubygems.org/patterns/
"Gems should not change the $LOAD_PATH variable. RubyGems manages this for you. Code like this should not be necessary ..."
See http://guides.rubygems.org/patterns/ for various solutions to load using the existing load path, and helpers such as require_relative.
In addition you need to ensure your executable is, well, executable.
$ chmod a+x bin/gemname
Otherwise the command won't be available after building the gem.
I created a simple gem by doing:
testgem.gemspec
Gem::Specification.new do |s|
s.name = 'testgem'
s.version = '0.0.1'
s.summary = "code generation"
s.authors = ['asdf']
end
created the following file layout:
testgem.gemspec
Gemfile
lib/
lib/testgem.rb
lib/testgem/other.rb
ran the following:
gem build testgem.gemspec
WARNING: no description specified
WARNING: no email specified
WARNING: no homepage specified
Successfully built RubyGem
Name: testgem
Version: 0.0.1
File: testgem-0.0.1.gem
installed it:
gem install ./testgem-0.0.1.gem
Successfully installed testgem-0.0.1
1 gem installed
tested to see if irb can see it:
irb
require 'testgem'
oadError: cannot load such file -- testgem
I'm using Ruby 1.9.3-p194
It seems to be installed if I do:
gem list
..
testgem (0.0.1)
..
What could be the issue?
You need to add your lib to the search path. Just add
$:.push File.expand_path("../lib", __FILE__)
on top of your .gemspec
When you use require in Ruby, you are trying to load a file, not a gem. The file could of course be contained in a gem, and by convention a gem will have a file with the same name as the gem itself so that if you wanted to use the gem foo you would use require 'foo'. This isn’t always the case though, for example to use the bcrypt-ruby gem you need to require 'bcrypt'.
Additionally a gem doesn’t necessarily have to contain any filles at all. An example of this is the rails gem, which doesn’t contain any files itself (at least in version 3.2), but has dependencies to the various Rails components, so that you can install them all in one step.
In your case, although you have a lib/testgem.rb file in your gems project directory, you are not including it in your gem. You need to specify which files should be included, the default is to not include anything.
To simply include all the files in your project directory, you can add something like:
s.files = Dir.glob '**/*'
to your gemspec.
You need to require 'rubygems' first.
I installed Phusion Passenger with Nginx, configured Nginx to point to the right directory, Then I ran webapp directory and this has downloaded the gemfiles, but it can't find the gems.
When I visit the site I get the standard Passenger error page which says:
Error message:
no such file to load -- bundler
Here's the full error: http://tinypic.com/view.php?pic=vpx36r&s=7
I've do a gem install bundler so I know bundler is installed, but I think it's looking in the wrong place for the gems.
It appears as if Passenger has installed ruby-enterprise-1.8.7 and it looks like 1.8 was already installed in this box.
gem env gives me the following:
- RUBYGEMS VERSION: 1.4.2
- RUBY VERSION: 1.8.7 (2009-12-24 patchlevel 248) [x86_64-linux]
- INSTALLATION DIRECTORY: /opt/local/ruby-enterprise-1.8.7-2010.01/lib/ruby/gems/1.8
- RUBY EXECUTABLE: /opt/local/ruby-enterprise-1.8.7-2010.01/bin/ruby
- EXECUTABLE DIRECTORY: /opt/local/ruby-enterprise-1.8.7-2010.01/bin
- RUBYGEMS PLATFORMS:
- ruby
- x86_64-linux
- GEM PATHS:
- /opt/local/ruby-enterprise-1.8.7-2010.01/lib/ruby/gems/1.8
- /root/.gem/ruby/1.8
- GEM CONFIGURATION:
- :update_sources => true
- :verbose => true
- :benchmark => false
- :backtrace => false
- :bulk_threshold => 1000
- REMOTE SOURCES:
- http://rubygems.org/
From what I've read it appears to be a path issue, but I don't know what the best course of action is to fix it.
Here's the output of
which ruby:
/opt/local/ree/bin/ruby
which bundle:
/opt/local/ree/bin/bundle
I've run bundle install in that directory and it's given me:
Your bundle is complete! It was installed into ./vendor/bundle
it looks like you have some path issues (as you already stated). try to check why you have two different paths for your ruby installation.
I mean, according to your gem env output, you have some installation on
/opt/local/ruby-enterprise-1.8.7-2010.01/
but you also have ruby and bunlder under
/opt/local/ree/
so, start from here to check if all the paths are correct and/or you haven't any double installations (well, it's just enough to check if your ENVs are coherent).
Another issue could be related to your nginx.conf, you should have something like this:
[...]
http {
passenger_root /your/path/to/passenger/gem;
passenger_ruby /path/to/ruby;
[...]
}
UPDATE (just saw additional comments with nginx.conf):
as you can see, there's a problem with paths: you have two paths for ruby and gems installs:
/opt/local/ruby-enterprise....
and
/opt/local/ree
you should remove the latter (not phisically, just review ENVs to point to the first one)
EDIT:
to change your env, you can append the following line to your /etc/bash.bashrc (or whaterver file is loaded from your shell by default):
PATH="/opt/local/ruby-enterprise-1.8.7-2010.01/bin:$PATH"
then logout and login, or for a quick test, launche this command from shell:
source /etc/bash.bashrc
now, retry to see what binary is used by default with:
which ruby
which gem
which rake
gem env
...
everything should have the /opt/local/ruby-enterprise-1.8.7-2010.01/ prefix (that's what is used from nginx and passenger configs).
UPDATE2: from the comments, it turned out that you have at least 3 ruby installs:
ruby from your package manager in /usr/lib/ruby/ (are you using ubuntu linux?)
a ruby in /opt/local/ree/
a ruby in /opt/local/ruby-enterprise-....
at this point, the best thing to do is:
uninstall all the rubys under /opt/local/ and all their gems
uninstall (purge) system provided ruby packages (you don't need it)
install and setup RVM: https://rvm.io
re-install passenger using RVM: https://rvm.io/integration/passenger/
fix nginx.conf to use passenger path and ruby
I recently upgraded to Ubuntu 9.04 and I have issues using gems.
I installed Ruby, Rubygems and Rails using apt-get.
The rails command does work.
I then installed capistrano and other gems, such as heroku.
In order to do that, I used the command:
sudo gem install XXX
When I want to use the cap command it does not work:
bash: cap: command not found
It is the same with the other gem commands.
Do I have something particular to do so that the gem commands work?
Where are my Gems?
You can find where your gems are stored using the gem environment command. For example:
chris#chris-laptop:~$ gem environment
RubyGems Environment:
- RUBYGEMS VERSION: 1.3.2
- RUBY VERSION: 1.8.7 (2008-08-11 patchlevel 72) [i486-linux]
- INSTALLATION DIRECTORY: /usr/lib/ruby/gems/1.8
- RUBY EXECUTABLE: /usr/bin/ruby1.8
- EXECUTABLE DIRECTORY: /usr/bin
- RUBYGEMS PLATFORMS:
- ruby
- x86-linux
- GEM PATHS:
- /usr/lib/ruby/gems/1.8
- /home/chris/.gem/ruby/1.8
- GEM CONFIGURATION:
- :update_sources => true
- :verbose => true
- :benchmark => false
- :backtrace => false
- :bulk_threshold => 1000
- REMOTE SOURCES:
If you look at the "GEM PATHS:" section you can see that gems can be stored in two places on my laptop: /usr/lib/ruby/gems/1.8 or in the .gem directory in my home dir.
You can also see that executables are stored in EXECUTABLE DIRECTORY which in this case is /usr/bin.
Because /usr/bin is in my path this lets me run cap, merb, rails etc.
Updating your PATH
If for some reason your EXECUTABLE DIRECTORY isn't on your path (for example if it is /var/lib/gems/1.8/bin) then you need to update your PATH variable.
Assuming that you are using the bash shell. You can do this quickly for the current session by typing the following at the shell prompt; let's pretend that you want to add /var/lib/gems/1.8/bin to the path:
export PATH=$PATH:/var/lib/gems/1.8/bin
and press return. That appends the new directory to the end of the current path. Note the colon between $PATH and /var/lib/gems/1.8/bin
To set the value for all sessions you will need to edit either your .profile or .bashrc file and add the same line to the end of the file. I usually edit my .bashrc file for no reason other than that's what I've always done. When finished, save the file and then refresh your environment by typing:
bash
at the shell prompt. That will cause the .bashrc to get reread.
At any point you can check the current value of $PATH by typing
echo $PATH
at the shell prompt.
Here's a sample from one of my own servers, where my username is "chris" and the machine name is "chris-laptop":
chris#chris-laptop:~$
chris#chris-laptop:~$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
chris#chris-laptop:~$
chris#chris-laptop:~$ export PATH=$PATH:/var/lib/gems/1.8/bin
chris#chris-laptop:~$
chris#chris-laptop:~$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/var/lib/gems/1.8/bin
chris#chris-laptop:~$
My Gem won't load!
"Ruby gems won't load even though installed" highlights a common problem using multiple different versions of Ruby; Sometimes the Gem environment and Gem path get out of sync:
rb(main):003:0> Gem.path
=> ["/opt/ruby1.9/lib/ruby1.9/gems/1.9.1"]
irb(main):004:0> exit
Any Ruby process here is looking only in one place for its Gems.
:~/$ gem env
RubyGems Environment:
- RUBYGEMS VERSION: 1.3.7
- RUBY VERSION: 1.9.1 (2009-05-12 patchlevel 129) [x86_64-linux]
- INSTALLATION DIRECTORY: /opt/ruby1.9/lib/ruby/gems/1.9.1
- RUBY EXECUTABLE: /opt/ruby1.9/bin/ruby1.9
- EXECUTABLE DIRECTORY: /opt/ruby1.9/bin
- RUBYGEMS PLATFORMS:
- ruby
- x86_64-linux
- GEM PATHS:
- /opt/ruby1.9/lib/ruby/gems/1.9.1
- /home/mark/.gem/ruby/1.9.1
- GEM CONFIGURATION:
- :update_sources => true
- :verbose => true
- :benchmark => false
- :backtrace => false
- :bulk_threshold => 1000
- REMOTE SOURCES:
- http://rubygems.org/
Look carefully at the output of gem environment:
- GEM PATHS:
- /opt/ruby1.9/lib/ruby/gems/1.9.1
This isn't the same path as returned by Gem.path:
["/opt/ruby1.9/lib/ruby1.9/gems/1.9.1"]
It's hard to say what exactly caused lib/ruby to change to lib/ruby1.9 but most likely the developer was working with multiple Ruby versions. A quick mv or ln will solve the problem.
If you do need to work with multiple Ruby versions then you really should be using rvm.
As noted by #Chris you need to add the gems environment to your path. You can do this by:
echo 'export PATH=$PATH:/var/lib/gems/1.8/bin' | tee --append ~/.bashrc
The folder in which gems are stored must be on your PATH, for example mine is:
/home/victor/.gem/ruby/1.8/bin
Check your path by typing
echo $PATH
It seens that when installing rubygems, now in ubuntu 9.04, I have this problem. I noticed that in "gem environment" the executable directory is "/var/lib/gems/1.8/bin", instead of "/usr/bin"... This is a problem with rubygems or with ubuntu 9.04??
The solution that I encountered is to add "/var/lib/gems/1.8/bin" to my $PATH doing this:
export PATH=$PATH:/var/lib/gems/1.8/bin
But it don't is saved... how can I save my path?
Thanks...
Resolvi: coloquei o export PATH=$PATH:/var/lib/gems/1.8/bin no ~/.bashrc! =]
mkmf is part of the ruby1.9.1-dev package. This package contains the header files needed for extension libraries for Ruby 1.9.1. You need to install the ruby1.9.1-dev package by doing:
sudo apt-get install ruby1.9.1-dev