How do you install a library that is not a gem in Ruby?
I'm trying to use graphy.
In the example usage, it says to require 'graphy', but even when my ruby file is in the same directory as graphy.rb, I get the following error:
<internal:lib/rubygems/custom_require>:29:in `require': no such file to load -- graphy.rb (LoadError)
from <internal:lib/rubygems/custom_require>:29:in `require'
from foo.rb:1:in `<main>'
Tell ruby to look in the current dir using the -I flag:
ruby -I. my_script.rb
To see the ruby load path, add puts $: at the top of your script.
Everything works fine. I have pasted my IRB try below.
Yasky$ cd Projects/ruby/bruce-graphy-70f213b/lib/
lib Yasky$ irb
ruby-1.8.7-p352 :001 > require 'graphy'
=> true
ruby-1.8.7-p352 :002 > dg = Graphy::Digraph[1,2, 2,3, 2,4, 4,5, 6,4, 1,6]
=> Graphy::DirectedGraph[Graphy::Arc[2,3,nil], Graphy::Arc[1,6,nil], Graphy::Arc[2,4,nil], Graphy::Arc[4,5,nil], Graphy::Arc[1,2,nil], Graphy::Arc[6,4,nil]]
ruby-1.8.7-p352 :003 > exit
lib Yasky$
Explicitly specifying your current directory in your load path may do the trick.
EDIT: Oops, I was too late (:
In this particular case, the author is using Jeweler to manage his gemspec. I'm not that familiar with Jeweler, but AFAIR, you generate and install a Gem with rake install. You may need to generate a version number first with rake version:write MAJOR=0 MINOR=0 PATCH=1.
Related
I am new to Ruby, working my way through an example, and am having trouble in my local environment in macOS, receiving a LoadError trying to require bundler in a single ruby file.
I have a bcrypt.rb file that contains the following:
require 'bunder/inline'
gemfile true do
source 'https://rubygems.org'
gem 'bcrypt'
end
require 'bcrypt'
my_password = BCrypt::Password.create("my password")
my_password.version #=> "2a"
my_password.cost #=> 12
my_password == "my password" #=> true
my_password == "not my password" #=> false
I expect the file to successfully require bundler and bcrypt and run the code, producing no output. When I try to run the bcrypt.rb file I get the following error:
Traceback (most recent call last):
2: from bcrypt.rb:1:in `<main>'
1: from /Users/rturner/.rvm/rubies/ruby-2.6.4/lib/ruby/site_ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require'
/Users/rturner/.rvm/rubies/ruby-2.6.4/lib/ruby/site_ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require': cannot load such file -- bunder/setup (LoadError)
I noticed my GEM_HOME environment variable was set to /Users/rturner/.rvm/gems/ruby-2.6.4 which does not seem to contain gem files. I added a line after the rvm script loads in my .bash_profile file:
export GEM_HOME="/Users/rturner/.rvm/gems/default/gems"
This changed the GEM_HOME variable to a directory that contains gems but did not fix the problem. I am using rvm in my local setup, installed with brew and I have installed bundler with brew, run brew update and brew upgrade as well as trying general troubleshooting methods listed on the bundler page. Can anyone help? Thanks!
Looks like you have mistype here:
require 'bunder/inline'
it should be:
require 'bundler/inline'
I know the command for this is: require file name
but I guess this command is for ruby 1.9.4 and I am using ruby 2.0.0
the exact message is.
$require start.rb
LoadError: cannot load such file -- start.rb
from /home/aka/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:45:in `require'
from /home/aka/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:45:in `require'
from (irb):2
from /home/aka/.rvm/rubies/ruby-2.0.0-p247/bin/irb:13:in `<main>'
Thanks in advance
Inside irb
>> require './start'
=> true
The file start.rb being in the same folder as you are, and the required file being in quotes, prefaced with the relative location ./ as shown.
This should work for you in Ruby from 1.8 on to 2.0 and beyond.
According to the docs, require will look for start.rb in the $LOAD_PATH since it does not resolve to an absolute path. In this case, the LoadError is telling you that start.rb is not in your $LOAD_PATH.
I am guessing that start.rb is in your current directory. You can use
>> require '/path/to/start.rb'
or
>> require './start.rb'
or
>> require './start'
This saves you some typing:
$ irb -r ./start.rb
I'm trying to build my first gem. Using Ryan Biggs' tutorial as my guide, I did the following:
1) Created the gem scaffolding:
$ bundle gem hello_world
2) Edited the lib/hello_world.rb file:
require "hello_world/version"
module HelloWorld
def hi
"Hello world!"
end
end
3) Installed the gem via bundler:
$ cd hello_world
$ bundle install
At this point, if I run
$ bundle show hello_world
it shows
/Users/ykessler/gems/hello_world
so it looks like it installed.
But when I try to require the gem from irb:
require '/Users/ykessler/gems/hello_world'
it can't load it:
2.0.0-p195 :003 > require '/Users/ykessler/gems/hello_world'
LoadError: cannot load such file -- /Users/ykessler/gems/hello_world
from /Users/ykessler/.rvm/rubies/ruby-2.0.0-p195/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:45:in `require'
from /Users/ykessler/.rvm/rubies/ruby-2.0.0-p195/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:45:in `require'
from (irb):3
from /Users/ykessler/.rvm/rubies/ruby-2.0.0-p195/bin/irb:16:in `<main>'
Where am I going wrong?
You need to run gem build hello_world.gemspec
Then to install it, you run gem install hello_world from the root of your gem project. That will install your local gem using the .gem file that we just created in your directory (not the gem from rubygems.org if it exists).
Now, if you run gem list, you should see it. You should now be able to require your gem and and access your library from other ruby code. All you have to write is require 'hello_world'. There is no need to type the full path. In fact, that's a bad idea.
This is all explained pretty clearly in the rubygems.org documentation (http://guides.rubygems.org/make-your-own-gem/). It's very clear, helpeful, and it's where I learned how to make my first gem.
I've recently made the plunge to use RVM on Ubuntu.
Everything seems to have gone swimmingly...except for one thing. I'm in the process of developing a gem of mine that has a script placed within its own bin/ directory, all of the gemspec and things were generated by Jeweler.
The bin/mygem file contains the following code: -
#!/usr/bin/env ruby
begin
require 'mygem'
rescue LoadError
require 'rubygems'
require 'mygem'
end
app = MyGem::Application.new
app.run
That was working fine on the system version of Ruby.
Now...recently I've moved to RVM to manage my ruby versions a bit better, except now my gem doesn't appear to be working.
Firstly I do this: -
rvm 1.9.2
Then I do this: -
rvm 1.9.2 gem install mygem
Which installs fine, except...when I try to run the command for mygem
mygem
I just get the following exception: -
daniel#daniel-VirtualBox:~$ mygem
<internal:lib/rubygems/custom_require>:29:in `require': no such file to load -- mygem (LoadError)
from <internal:lib/rubygems/custom_require>:29:in `require'
from /home/daniel/.rvm/gems/ruby-1.9.2-p136/gems/mygem-0.1.4/bin/mygem:2:in `<top (required)>'
from /home/daniel/.rvm/gems/ruby-1.9.2-p136/bin/mygem:19:in `load'
from /home/daniel/.rvm/gems/ruby-1.9.2-p136/bin/mygem:19:in `<main>'mygem
NOTE: I have a similar RVM setup on MAC OSX and my gem works fine there so I think this might be something to do with Ubuntu?
Using:
rvm 1.9.2 gem install mygem
is different than how I do my installs of gems inside RVM.
Try:
rvm 1.9.2
gem install mygem
You might also want to try doing gem pristine mygem which will tell Gems to remove the executable and recompile it for the current Ruby.
Another thought: Were you previously using Ruby 1.8+, and just changed to Ruby 1.9+? In Ruby 1.9 the require acts differently when loading modules that are relative to the calling code, say, in a child directory, because '.' was removed from the search path. require_relative was added to give us that capability.
Does doing export RUBYOPT=rubygems help?
I am on Ubuntu10
sudo apt-get install ruby1.9.1-full
then download sources of rubygem 1.3.7 and install it
sudo ruby setup.rb
then, for example, install sinatra
sudo gem install sinatra
Finally open irb and type
require "rubygems"
require "sinatra"
and get error
LoadError: no such file to load -- sinatra
from (irb):2:in `require'
from (irb):2
from /usr/bin/irb:12:in `<main>'
I had exactly this problem. The problem is that gem and ruby disagree about where the gems live. Compare these:
ruby -e "puts Gem.path"
gem env
gem which sinatra
If you're like my setup, you'll notice that there's an entry in gem env's paths that isn't in Gem.path, and that's exactly where sinatra will claim to be. In my case, I had to add
export GEM_HOME=/usr/lib/ruby/gems/1.9.1
to my .profile. Then everyone was happy.
Execute
sudo gem install sinatra --verbose
and note the path where the gem is getting installed.
Then try this in irb
puts $LOAD_PATH
and make sure that gem is installed in one of the directories in $LOAD_PATH
And ideally just start using http://rvm.beginrescueend.com/
I usually hit this error when I forget:
require 'rubygems'
It'd be helpful if you provided the actual code sample, though, what gem you want to require, and what Ruby version you're using if this doesn't solve the problem.
This was before here on SO quite a few times. Problem is that you probably have two versions of ruby. The one is installing the gem and the other one is trying to use it. Do this in terminal:
$ which -a ruby
Or this:
$ which -a gem
to see if you have more than one version of ruby/gem installed. If so - remove one version (via $ rm or package manager of your system).
I use ruby gems 1.8.7 for a project. I was getting the same error. Use the line require 'rubygems'. It must always be the first require statement, otherwise you can get an error. In my code, I had
require 'watir'
require 'rubygems'
# more code
I got the error - in `require': no such file to load -- watir (LoadError).
When I put rubygems first, the error went away and everything worked. I don't know
why this happens.
Btw, I tried user24359 answer and it did not help me.
C:\code>ruby -e "puts Gem.path"
-e:1: uninitialized constant Gem (NameError)