How do RVM rubies find gems? - ruby

I'm using Ruby 1.8.6 under RVM on Mac OS X (10.9.2). I installed everything as a local user (not root), and then installed a gem using:
gem install gli -v 1.6
When I run a ruby with require('gli'), I get the error:
in `require': no such file to load -- gli (LoadError)
even though gem list shows gli (1.6.0).
I don't know exactly how RVM does its gemsets, but there is no gli.rb file in any of the directories in the $LOAD_PATH.
How does RVM interact with the $LOAD_PATH to find the necessary gems in the gemset?

Try adding a require 'rubygems' at the top of your source file. In 1.8 rubygems aren't required by default.
Thus said, regarding gem paths:
GEM_HOME indicates where gems are installed. To find out what it is for your case do:
$ echo $GEM_HOME
This should display an RVM-related path, since RVM changes this environment variable in order to make the $ gem install command install gems in its directories.

Related

Specify Path for Installing ruby gem

I have two versions of Ruby installed.
How can I install my ruby gem so that it gets installed in the version specified.
Normally you need to specify which Ruby you're using first, then call the appropriate gem command once that's set.
RVM, rbenv and others all work in different ways. For example, with RVM:
rvm use 2.4.1
gem install xyz -v 1.2.3
For rbenv it's a bit different:
rbenv local 2.4.1
gem install xyz -v 1.2.3
Where that generates a .ruby-version file in your current directory.
Since the gem command itself is Ruby, the proper Ruby environment must be set, which is where the multi-Ruby version managers come in.

Error kernel_require.rb:55:in `require': cannot load such file when trying to run ruby program

So, it seems like ruby isn't able to find gems that are installed? The error:
/Users/userName/.rbenv/versions/2.3.0/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in
`require': cannot load such file -- activesupport (LoadError)
from /Users/userName/.rbenv/versions/2.3.0/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in
`require'
from test.rb:3:in `<main>'
I had ruby installed via RVM and brew on my Mac (El Capitan). I don't remember what I had done when I started getting this error when trying to run my ruby program. At the top of my program I have require 'json' and require 'rest-client', and require 'activesupport' Nothing abnormal there.
After a lot of googling, I ended up doing rvm implode and removing the .rvm files from my computer to completely uninstall rvm. I then tried installing ruby via brew, but was getting the same error. I then uninstalled the the brew version of ruby, and installed with rbenv. Still got the same error, but this time with the rbenv path to the kernal_require.rb file instead of the brew or rvm paths that it had showed before.
So something tells me that this is not something to do with rvm or brew or rbenv because I was getting the same error with the versions of ruby I tried for each one. The gems are installed. Why isn't it able to load them? Uninstalling and reinstalling the gems didn't help. I had to reinstall them anyway every time I uninstalled and reinstalled ruby.
gem env home returns this:
/Users/userName/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0
brew config returns this for ruby:
Ruby: /Users/userName/.rbenv/shims/ruby => /Users/userName/.rbenv/versions/2.3.0/bin/ruby
I have these included in my PATH:
export PATH="$HOME/.rbenv/bin:$PATH"
eval "$(rbenv init -)"
ruby -v returns this:
ruby 2.3.0p0 (2015-12-25 revision 53290) [x86_64-darwin15]
I've checked for spaces in the file path, I've tried installing bundler and installing gems that way, but I'm still getting the same issue. What else can I look at to solve this problem?
You use
gem 'activerecord'
in the gemfile.
require 'active_support/all'
is used in the program.
It's slightly annoying when gems do this, but you can't always assume the files you require have the same name as in the Gemfile.

How to make ruby gems available to all users?

I installed a Ruby gem running this command from root:
gem install pushmeup
The gem works well when executing scripts from root. When I try to execute a script from non-root user, I see this error about such gem not found:
/usr/local/rvm/rubies/ruby-1.9.3-p551/lib/ruby/site_ruby/1.9.1/rubygems/core_ext/kernel_require.rb:54:in `require': cannot load such file -- pushmeup (LoadError)
from /usr/local/rvm/rubies/ruby-1.9.3-p551/lib/ruby/site_ruby/1.9.1/rubygems/core_ext/kernel_require.rb:54:in `require'
from pushAPNS.rb:2:in `<main>'
Edit:
RVM gives you compartmentalized independent ruby setups. This means
that ruby, gems and irb are all separate and self-contained - from the
system, and from each other.
https://rvm.io/gemsets/basics
============
Never use sudo with rvm.
Type rvm gemset list
Type rvm gemset use global
Type gem install some_gem
The gems in the global gemset will be available in all other gemsets. If you want to create a gemset for a specific project, then do:
rvm gemset create proj1_gems
rvm gemset list
rvm gemset use proj1_gems
To delete a gemset:
rvm gemset delete proj1_gems
rvm gemset list
Each version of ruby has its own gemsets. You can switch to a specific ruby version and one of the gemsets for that version with one command like this:
rvm use 2.0.0#proj1_gems
But generally, I do:
rvm use 2.0.0
rvm gemset list
rvm gemset use some_gemset_name_in_the_list

Installing gems in Mac OS X pre-installed ruby 1.8.7

Macs seem to all come with ruby 1.8.7 pre-installed. That's fine for my purposes, but when I install gems with either gem install gem-name or sudo gem install gem-name, I have to explicitly add the gem's lib directory to my $LOAD_PATH at the top of my ruby programs:
$LOAD_PATH.unshift File.join("/", "Users", "me", ".gems", "more_dirs", "lib")
Why do I have to do this? Am I installing gems wrong?
If I then install rvm and the latest ruby 1.9.3, I can install gems no problem with gem install gem-name.
With 1.8.7 where rubygems aren't built in. You need to
require 'rubygems'
at the top of your scripts in order to avoid manually setting the paths to your gems (pulling this in from the comments)

ruby gems under rvm on Ubuntu (failing to) install in the wrong location

I'm running Ubuntu 10.04 and originally installed ruby 1.9.1 (from source). I've just decided to try out ruby 1.9.2 and rails 3, and it seemed like a good time to use rvm to deal with the multiple ruby installs and gemsets.
rvm installed and seems to be working, I installed ruby 1.9.2 in rvm and made that my default ruby. However, every time I try to install a gem under ruby 1.9.2 I see this error.
mark#Steve-Austins-Penguin:~$ gem install haml
ERROR: While executing gem ... (Errno::EACCES)
Permission denied - /home/mark/.gem/specs
gem environment returns:
RubyGems Environment:
- RUBYGEMS VERSION: 1.3.7
- RUBY VERSION: 1.9.2 (2010-08-18 patchlevel 0) [x86_64-linux]
- INSTALLATION DIRECTORY: /home/mark/.rvm/gems/ruby-1.9.2-p0
- RUBY EXECUTABLE: /home/mark/.rvm/rubies/ruby-1.9.2-p0/bin/ruby
- EXECUTABLE DIRECTORY: /home/mark/.rvm/gems/ruby-1.9.2-p0/bin
- RUBYGEMS PLATFORMS:
- ruby
- x86_64-linux
- GEM PATHS:
- /home/mark/.rvm/gems/ruby-1.9.2-p0
- /home/mark/.rvm/gems/ruby-1.9.2-p0#global
<snip>
But if the correct installation, ruby and gem paths are showing here why is ruby trying to install gems to /home/mark/.gem? All the variables and paths shown by env look right, and I can't find a .gemrc file anywhere on my system that might contain conflicting path settings.
So I guess my question is what might be causing rubygems to attempt to install to /home/mark/.gems instead of the correct paths set by rvm?
In response to the question below:
mark#Steve-Austins-Penguin:~$ echo $PATH
/home/mark/.rvm/gems/ruby-1.9.2-p0/bin:/home/mark/.rvm/gems/ruby-1.9.2-p0#global/bin:/home/mark/.rvm/rubies/ruby-1.9.2-p0/bin:/home/mark/.rvm/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
mark#Steve-Austins-Penguin:~$ which gem
/home/mark/.rvm/rubies/ruby-1.9.2-p0/bin/gem
mark#Steve-Austins-Penguin:~$ rvm list
rvm rubies
ruby-1.8.7-p302 [ x86_64 ]
=> ruby-1.9.2-p0 [ x86_64 ]
mark#Steve-Austins-Penguin:~$ env
rvm_gemsets_path=/home/mark/.rvm/gemsets
MANPATH=:/home/mark/.rvm/man
rvm_scripts_path=/home/mark/.rvm/scripts
rvm_bin_path=/home/mark/.rvm/bin
GEM_HOME=/home/mark/.rvm/gems/ruby-1.9.2-p0
rvm_patchsets_path=/home/mark/.rvm/patchsets
SHELL=/bin/bash
IRBRC=/home/mark/.rvm/rubies/ruby-1.9.2-p0/.irbrc
rvm_patches_path=/home/mark/.rvm/patches
MY_RUBY_HOME=/home/mark/.rvm/rubies/ruby-1.9.2-p0
rvm_selfcontained=1
USER=mark
__shell_array_start=0
rvm_gems_cache_path=/home/mark/.rvm/gems/cache
rvm_config_path=/home/mark/.rvm/config
rvm_path=/home/mark/.rvm
USERNAME=mark
rvm_gemset_separator=#
rvm_ruby_args=
rvm_rubies_path=/home/mark/.rvm/rubies
PWD=/home/mark
rvm_hooks_path=/home/mark/.rvm/hooks
rvm_version=1.0.1
rvm_src_path=/home/mark/.rvm/src
HOME=/home/mark
rvm_gems_path=/home/mark/.rvm/gems
rvm_ruby_string=ruby-1.9.2-p0
BUNDLE_PATH=/home/mark/.rvm/gems/ruby-1.9.2-p0
rvm_tmp_path=/home/mark/.rvm/tmp
LOGNAME=mark
GEM_PATH=/home/mark/.rvm/gems/ruby-1.9.2-p0:/home/mark/.rvm/gems/ruby-1.9.2-p0#global
rvm_action=list
rvm_log_path=/home/mark/.rvm/log
rvm_interactive=1
RUBY_VERSION=ruby-1.9.2-p0
rvm_archives_path=/home/mark/.rvm/archives
rvm_repo_path=/home/mark/.rvm/repos
_=/usr/bin/env
Note that I've edited down the response from env slightly - simply to keep the size of this post under control - by removing all the entries relating to X, GNOME and SSH.
Following up the backtrace from running gem install -V --backtrace --debug haml (thanks zzzhc) and taking a closer look at the /home/mark/.gem directory I found that there were no gems installed there, just gemspec files and gem source files. In the spirit of experimentation I deleted /home/mark/.gem and re ran the gem install command and suddenly it was working. And that it had recreated /home/mark/.gem containing gemspecs for the gems I'd just installed.
It's clear now that I'd misinterpreted the error message in the original question; rubygems didn't fail while trying to install gems to /home/mark/.gem, it was simply using that directory as a temporary store for the gemspecs of newly downloaded gems about to be installed. Before installing rvm I'd been installing gems onto the system using sudo gem install... which would have created the /home/mark/.gem directory with root permissions. So rubygems run without sudo was unable to access the temp directory to store gemspecs in and was aborting before it could install the gems. Doh!
Another way is type which gem after getting into the ruby version. You will get something like this - /home/username/.rvm/rubies/ruby-1.9.2-p180/bin/gem. Now just type sudo /home/username/.rvm/rubies/ruby-1.9.2-p180/bin/gem install gem_name.
Specifying the complete path and adding sudo work fine.
check the first line of /home/mark/.rvm/rubies/ruby-1.9.2-p0/bin/gem and look at http://yehudakatz.com/2010/08/24/a-tale-of-abort-traps-or-always-question-your-assumptions/

Resources