Ruby cannot find required libraries even though gem is installed - ruby

I have spent literally days trying to install ruby 1.9.2 and get it working with gems :-/ I eventually gave up on my Mac OSX 10.6 machine and below is the current state on my Ubuntu machine. Any advice would be greatly appreciated!
# ruby test.rb
<internal:lib/rubygems/custom_require>:29:in `require': no such file to load -- mongo (LoadError)
from <internal:lib/rubygems/custom_require>:29:in `require'
from test.rb:1:in `<main>'
# cat test.rb
require 'mongo'
db = Mongo::Connection.new.db("mydb")
# gem which mongo
/usr/local/rvm/gems/ruby-1.9.2-p0/gems/mongo-1.1.2/lib/mongo.rb
# cat /etc/lsb-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=10.10
DISTRIB_CODENAME=maverick
DISTRIB_DESCRIPTION="Ubuntu 10.10"
According to this page: http://docs.rubygems.org/read/chapter/19
I symlinked which ruby I was using to match that which gem is using:
# which ruby
/usr/local/rvm/bin/ruby
# ls -l `which ruby`
lrwxrwxrwx 1 root root 44 2010-11-17 13:25 /usr/local/rvm/bin/ruby -> /usr/local/rvm/rubies/ruby-1.9.2-p0/bin/ruby
# gem env | grep 'RUBY EXECUTABLE'
- RUBY EXECUTABLE: /usr/local/rvm/rubies/ruby-1.9.2-p0/bin/ruby
# which gem
/usr/local/rvm/bin/gem
# gem -v
1.3.7
# ruby -v
ruby 1.9.2p0 (2010-08-18 revision 29036) [i686-linux]

Try putting the following line at the beginning
require "rubygems"

Why is "rvm" displaying in your /usr/local/rvm/ path? Did you do a system-wide install, as a system administrator using administering Ruby system wide for multiple users?
Did you add [[ -s '/usr/local/lib/rvm' ]] && source '/usr/local/lib/rvm' to your ~/.bashrc, ~/.bash_profile or ~/.profile (whichever you have configured)?
For normal, every day use, I recommend RVM's default setup:
RVM installation, RVM gems management.
Note to self: Buy stock in RVM. It's too cool.

Does it work under Ruby 1.8.7, which is pre-installed by default on OS X?
If so, one difference between 1.9.1 and 1.9.2 is that "." isn't part of $:'s path any more.

I recommend that you do rvm implode and delete the current setup. Then use the railsready script to setup RVM and Ruby properly for you on Ubuntu. It's important to understand that until you know what you are doing you should run the script as a user. Hope that helps.

On linux and OS X, I have always had to put require "rubygems" in the beginning. However it has always worked fine without this line on windows.

Related

Homebrew install Ruby keg-only can't find gem

How do I get irb to work after installing Ruby with Homebrew?
When I try to run irb, I get an error:
$ irb
Traceback (most recent call last):
2: from /usr/local/opt/ruby/bin/irb:23:in `<main>'
1: from /usr/local/lib/ruby/site_ruby/2.6.0/rubygems.rb:302:in `activate_bin_path'
/usr/local/lib/ruby/site_ruby/2.6.0/rubygems.rb:283:in `find_spec_for_exe': can't find gem irb (>= 0.a) with executable irb (Gem::GemNotFoundException)
I tried:
$ brew link ruby
Warning: Refusing to link macOS-provided software: ruby
If you need to have ruby first in your PATH run:
echo 'export PATH="/usr/local/opt/ruby/bin:$PATH"' >> ~/.bash_profile
For compilers to find ruby you may need to set:
export LDFLAGS="-L/usr/local/opt/ruby/lib"
export CPPFLAGS="-I/usr/local/opt/ruby/include"
I have the lines below at the top of my /etc/paths file:
/usr/local/bin
/usr/local/opt/ruby/bin
/usr/local/lib/ruby/gems/2.6.0/bin
irb doesn't show up in the output of gem list, but:
$ find /usr/local -name irb
/usr/local/lib/ruby/2.6.0/irb
/usr/local/Cellar/ruby/2.6.0_1/bin/irb
/usr/local/Cellar/ruby/2.6.0_1/lib/ruby/2.6.0/irb
/usr/local/Cellar/ruby/2.6.0_1/lib/ruby/gems/2.6.0/gems/irb-1.0.0/exe/irb
/usr/local/Cellar/ruby/2.6.0_1/share/ri/2.6.0/system/lib/irb
I'm also having a similar issue with ri & rdoc.
Run: gem install irb and you now good to go.
Assuming you're using Homebrew Ruby...
The irb executable is located at:
/usr/local/opt/ruby/lib/ruby/gems/2.6.0/gems/irb-1.0.0/exe/irb
You can use that line directly, symlink it into your $PATH, alias it, or whatever.
Alternatively, you may patch /usr/local/opt/ruby/bin/irb around line 22.
# patch
class Gem::BasicSpecification
def self.default_specifications_dir
File.join(Gem.private_dir, "specifications", "default")
end
end
# /patch
# Next line looks like this. Don't change this.
# if Gem.respond_to?(:activate_bin_path)
You may do the same in /usr/local/opt/ruby/bin/ri and /usr/local/opt/ruby/bin/rdoc to patch those commands as well.
Why?
See https://github.com/Homebrew/homebrew-core/blob/955497722b9bf65069957b0e7c903b96939cdd99/Formula/ruby.rb#L112
The Homebrew Ruby formula assumes all gems will be installed in the "global gem directory" /usr/local/lib/ruby/gems/2.6.0/. So when you uninstall-reinstall Homebrew Ruby, the gems stick around - you don't have to re-install them as well (kinda annoying since I have gems installed for Ruby versions I don't even have installed anymore, but that's another issue).
But Ruby's default gems aren't in the global gem dir. They're inside the Ruby installation dir (what the Homebrew formula refers to as the private_dir) : /usr/local/opt/ruby/lib/ruby/gems/2.6.0/.
So Homebrew Ruby can't find them.
Homebrew patches Rubygems, so this snippet patches Rubygems again, but deeper. You can also patch-patch like this:
module Gem
def self.default_dir
private_dir
end
end
But default_dir is used in other places and I didn't want to break anything.

Can't get awesome_print gem to work

awesome_print looks like a pretty nice gem, so I wanted to try it out.
I went to one of my projects and did:
gem install awesome_print
and it says one gem installed, documentation installed, etc.
Then, while I am in that project, I went to my Rails console to try it out, but when I did a require "awesome_print" as their help file says, I get a "cannot load such file".
Has anyone got this to work?
gem install will put the gem code on your computer, but unless the gem's source code files are on your load path, require won't be able to find them. bundle exec looks at the nearest Gemfile.lock and adds the source code for all the gems listed there to your load path. Rails initialization includes getting Bundler to do this for you.
One solution is to add awesome_print to your Gemfile. However, this will cause your application to have awesome_print as a dependency. Alternatively you can manually add the awesome_print library to your load path after starting up the Rails console and then requiring it:
$ rails c
> $LOAD_PATH << path/to/awesome_print-x.x.x/lib
> require 'awesome_print'
> ap {foo: {bar: {baz: :qux}}}
If you're using RVM, the path is likely to be something like:
~/.rvm/rubies/ruby-x.x.x-pxxx#your_gemset_name/gems/awesome_print-x.x.x/lib
Add it to your Gemfile like this:
gem 'awesome_print', :require => 'ap'
I add it to the development group, since that's the only time I need it. The gem doesn't have any other gem dependencies, so I routinely add it to my Gemfile.
Also, add these two lines to your ~/.irbrc file to set ap to be your default pager:
require "awesome_print"
AwesomePrint.irb!
Note that if you use this, however, any projects where awesome_print is not installed in its Gemfile will raise this error when you run rails c:
cannot load such file -- awesome_print
Depending on whatever else you may have in your ~/.irbrc file, this can cause other side effects, such as messing up your prompt. To avoid these, simply add the two lines to the very end of that file.
install it :
$ gem install awesome_print
include it in you GemFile, if you want :
gem 'awesome_print', :require => 'ap'
add this line to the file ~/.irbrc :
require 'awesome_print'
AwesomePrint.irb!
restart your shell!
just a note: I did this and it didnt work right away, probably need to restart the computer... or I just needed to close all shell tabs and open the terminal again!
Install the gem on your machine
gem install awesome_print
Get the path to which it has installed
gem which awesome_print
Add the following configuration to your ~/.irbrc and ~/.pryrc. This will load Awesome Print whenever you fire an IRB or a pry session.
*Remember $LOAD_PATH will hold whatever you got from typing gem which awesome_print
# ~/.irbc and ~/.pryrc
$LOAD_PATH << "~/.asdf/installs/ruby/2.6.3/lib/ruby/gems/2.6.0/gems/awesome_print-1.8.0/lib/"
require "awesome_print"
AwesomePrint.irb!
If you are looking to install it without having it in your Gemfile, this is how to do it:
$ gem install awesome_print
I was running into an issue where it was installing successfully but it not in the right directory.
In that case just put this in your .bashrc, this will set the load path:
export PATH="/home/user/.gem/ruby/2.3.0/bin:$PATH"
PATH="`ruby -e 'puts Gem.user_dir'`/bin:$PATH"
replace 2.3.0 with the version of ruby you are working with.
replace user with your username or if you are using vagrant then replace with vagrant
reload your .bashrc or exit the Terminal to reload changes, then install the gem again.
In my case, I struggled with PATHs and such, while missing something obvious!
# which ruby
/usr/bin/ruby
# ruby -v
ruby 2.3.7p456 (2018-03-28 revision 63024) [universal.x86_64-darwin17]
# locate bin/ruby
/usr/bin/ruby
/usr/local/Cellar/ruby/2.7.2/bin/ruby
/usr/local/opt/ruby/bin/ruby
# /usr/local/opt/ruby/bin/ruby -v
ruby 2.7.2p137 (2020-10-01 revision 5445e04352) [x86_64-darwin17]
#
Aha! Version crud. I was running an old ruby. Thanks, Apple!
# sudo mv /usr/bin/ruby /usr/bin/ruby_2.3.7
# sudo ln /usr/local/opt/ruby/bin/ruby /usr/bin/ruby
Solved the problem!
There is probably something I could have told brew to do to fix things, but I was impatient. :-)

watir on ubuntu 11.10

I've just updated my system from ubuntu 11.04 to 11.10 and...surprise!
Now if I try to use watir, doing
require 'rubygems'
require 'watir-webdriver'
I obtain this error
require 'watir-webdriver'
Invalid gemspec in [/var/lib/gems/1.8/specifications/json_pure-1.6.1.gemspec]:
invalid date format in specification: "2011-09-18 00:00:00.000000000Z"
Invalid gemspec in [/var/lib/gems/1.8/specifications/watir-webdriver-0.3.5.gemspec]:
invalid date format in specification: "2011-10-05 00:00:00.000000000Z"
LoadError: no such file to load -- watir-webdriver
from /usr/lib/ruby/vendor_ruby/1.8/rubygems/custom_require.rb:36:in `gem_original_require'
from /usr/lib/ruby/vendor_ruby/1.8/rubygems/custom_require.rb:36:in `require'
from (irb):3
from /usr/lib/ruby/1.8/x86_64-linux/rbconfig.rb:22
How to make it work again?
All I know about running Watir on Linux is here:
https://github.com/zeljkofilipin/watirbook/blob/master/installation/ubuntu.md
Did you check if Ubuntu update broke your Ruby installation? Maybe it uninstalled watir-webdriver gem or something.
What do you get for gem list watir?
Your version of ruby has problems. Install RVM, and get it to install a fresh ruby:
1. bash < <(curl -s https://raw.github.com/wayneeseguin/rvm/master/binscripts/rvm-installer )
2. echo '[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm" # Load RVM function' >> ~/.bash_profile
3. source .bash_profile
4. rvm install 1.9.2
5. rvm use 1.9.2
I have had the same problem and could fix it by adjusting the jason spec file:
/var/lib/gems/1.8/specifications/json_pure-1.6.1.gemspec
In the date format line I removed everything which looked like this "00:00:00.000000000Z"
and kept just the plain date format.
After again installing watir (see watir installation web page)
everything worked properly:
sudo apt-get install rubygems
gem install watir --no-rdoc --no-ri
I've seen this issue with the json gem (and now that I've upgraded to 11.10 other gems). The fix I'd previously seen for the json gem was to go into the gem file and listed in the error and remove the "00:00:00.000000000Z" string wherever you found it.
I've used it on every gem I've received this error on (which was only a couple but I don't remember which exactly). The the gems seem to work just fine. I haven't noticed any problems since removing that string.

Problem with RVM and gem that has an executable

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?

Build and run ruby without installing it to system directories

I've cloned the ruby 1.8.7 source tree. I can build ruby. But I can't figure out how to run it without installing it in system directories. How can I do it?
Background: I want to use "git bisect" to figure out which build of Ruby introduced a new behavior in my code. I need to build and run ruby against a test program, multiple times, but I don't want to clobber the ruby that the Debian package installed.
Here's what I get if I try to run the ruby I built from source:
$ ./ruby -e 'puts RUBY_VERSION'
ÀÇ ÀÇ : ÀÇ ÀÇ : cannot open shared object file: No such file or directory - ÀÇ ÀÇ (LoadError)
I've also tried installing it, but not to the system directories, and got a stack trace:
$ ./configure --prefix=/home/wayne/tmp/ruby/installed --exec-prefix=/home/wayne/tmp/ruby/installed
$ make
$ make install
$ /home/wayne/tmp/ruby/installed/bin/ruby -e 'puts RUBY_VERSION'
/home/wayne/tmp/ruby/installed/lib/ruby/1.8/openssl/ssl.rb:26: uninitialized constant OpenSSL::SSL::VERIFY_PEER (NameError)
from /home/wayne/tmp/ruby/installed/lib/ruby/1.8/openssl.rb:23:in `require'
from /home/wayne/tmp/ruby/installed/lib/ruby/1.8/openssl.rb:23
I've got the feeling that I'm close, but a miss is as good as a mile.
rvm is a great tool. and should be able to take care of the heavy lifting for switching between different ruby version (or even sets of gems).
Installation is very easy:
$ gem install rvm && rvm-install
$ echo "if [[ ! -z $HOME/.rvm ]] ; then source $HOME/.rvm ; fi" >> ~/.bash_profile
Then to install a specific version & patch level:
rvm install ruby-1.8.7-p160
Then to switch between versions:
$ rvm 1.8.7-p160
$ ruby -v
ruby 1.8.7 (2009-06-12 patchlevel 160) [i686-darwin10.0.0]
$ rvm 1.8.7-p174
ruby 1.8.7 (2009-06-12 patchlevel 174) [i686-darwin10.0.0]
When I need to do something like that, I use the chroot command. Create a temporary directory, install ruby into a sbin subdirectory, and chroot into the temp folder. Depending on what you are testing, you may also have to copy some system libraries into the temporary directory tree (before you chroot in).
It's probably easier to use rvm to test your app against different rubies.

Resources