Rvm Finding System Gems' Executables

RVM finding system gems' executables

Explanation of rubygems bin folders and PATH

Oh. You didn't have rails installed in your rvm ruby, but you did in your system ruby.

Individual gems, like rails can have a bin directory that will contain executable helper scripts. Your system default rubygems is making symlinks from your system /usr/bin/ dir into the gem's bin folder for these helper executables.

RVM provides a similar facility, except instead of polluting the system /usr/bin dir, it just appends its ~/.rvm/gems/#{rvm_gemset_string}/bin folder to the PATH environment variable.


Importing system Rubygems list into your new rvm rubies' gem directories

RVM by default will not import your gems from your system ruby installation into your rvm ruby installs. It makes a full clean fork of the entire ruby system including rubygems (the gem 'rubygems') and rubygems' gem list. When you rvm install 1.9.2 it's like you've made a completely new install of everything used with ruby.

If you'd like to get all your system ruby gems that you were previously using into your preferred rvm ruby, try this:

  rvm use system
rvm gemset export system.gems
rvm use 1.9.2
rvm gemset import system.gems
#You'll now have all your system gems reinstalled to your new ruby version

Original Answer/ Edits from @Telemachus

Try moving the lines that source rvm to the end of your ~/.bash_profile or ~/.bashrc (whichever you have it in):

'[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm" # Load RVM function'

.

bash: /usr/bin/rails: /usr/bin/ruby1.8: bad interpreter: No such file ...
| | ^--------------------------------\
^ Bash, not rvm; ^/usr/bin/rails, not ~/.rvm/gems/*/bin/rails; |
Some ruby leftover from a previous install in the os

You have rails installed in /usr/bin, which is probably before the rvm ruby bin path in your bash echo $PATH variable, so it's finding the system rails install (/usr/bin/rails, a ruby script) which starts like this:

#! /usr/bin/ruby18

You've gotta make the conflict stop happening, the best of all possible ways is making sure that RVM's bin dir is at the beginning of your PATH. This happens in the #Load rvm environment script that you added to your ~/.bash_profile when installing rvm. If you installed rvm as a system library rather than just for your user, this will be different.

If you get to that case, ask @Telemachus.

You'll then need to ensure you've gotten the rails gem installed in your new rvm ruby as above.

Acceptance Test:

You'll find that when you've done rvm use 1.9.2, then which ruby will return something like ~/.rvm/rubies/1.9.2/bin/ruby, and which rails should return something like ~/.rvm/gems/*/bin/rails.

How to find where gem files are installed

Use gem environment to find out about your gem environment:

RubyGems Environment:
- RUBYGEMS VERSION: 2.1.5
- RUBY VERSION: 2.0.0 (2013-06-27 patchlevel 247) [x86_64-darwin12.4.0]
- INSTALLATION DIRECTORY: /Users/ttm/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0
- RUBY EXECUTABLE: /Users/ttm/.rbenv/versions/2.0.0-p247/bin/ruby
- EXECUTABLE DIRECTORY: /Users/ttm/.rbenv/versions/2.0.0-p247/bin
- SPEC CACHE DIRECTORY: /Users/ttm/.gem/specs
- RUBYGEMS PLATFORMS:
- ruby
- x86_64-darwin-12
- GEM PATHS:
- /Users/ttm/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0
- /Users/ttm/.gem/ruby/2.0.0
- GEM CONFIGURATION:
- :update_sources => true
- :verbose => true
- :backtrace => false
- :bulk_threshold => 1000
- REMOTE SOURCES:
- https://rubygems.org/
- SHELL PATH:
- /Users/ttm/.rbenv/versions/2.0.0-p247/bin
- /Users/ttm/.rbenv/libexec
- /Users/ttm/.rbenv/plugins/ruby-build/bin
- /Users/ttm/perl5/perlbrew/bin
- /Users/ttm/perl5/perlbrew/perls/perl-5.18.1/bin
- /Users/ttm/.pyenv/shims
- /Users/ttm/.pyenv/bin
- /Users/ttm/.rbenv/shims
- /Users/ttm/.rbenv/bin
- /Users/ttm/bin
- /usr/local/mysql-5.6.12-osx10.7-x86_64/bin
- /Users/ttm/libsmi/bin
- /usr/local/bin
- /usr/bin
- /bin
- /usr/sbin
- /sbin
- /usr/local/bin

Notice the two sections for:

  • INSTALLATION DIRECTORY
  • GEM PATHS

rvm gem install error?

It looks like you've set the root user to use the rvm install which is in your home folder. This means when you install a gem with sudo, the files will belong to root. So, when you try and install a gem as you, you're getting permission errors.

To fix, you need to chown the rvm folder back to you (I'm assuming, from your file paths, that your username is 'da' here)...

sudo chown -R da:da /Users/da/.rvm

To avoid this happening in the future, I'd recommend setting root's path to be either its own install of rvm, or system gems.

Setting up RVM for gem installs in future for correct permissions and adding PATH correctly

First, stop using --user-install. You shouldn't need it with RVM as that's one of its primary purposes, keeping the gems and Ruby in your home directory.

Your path is not well constructed:

"/Users/dhruv/anaconda/bin:/Users/dhruv/anaconda/bin:/Users/dhruv/.rvm/gems/ruby-2.2.1/bin:/Users/dhruv/.rvm/gems/ruby-2.2.1@global/bin:/Users/dhruv/.rvm/rubies/ruby-2.2.1/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/TeX/texbin:/Users/dhruv/.gem/ruby/2.0.0/bin:/Users/dhruv/scripts:/Users/dhruv/.rvm/bin:/Users/dhruv/.rvm/bin:/usr/local/lib:/Users/dhruv/.gem/ruby/2.0.0/bin:/Users/dhruv/scripts:/Users/dhruv/.rvm/bin:/Users/dhruv/.rvm/bin:/usr/local/lib"

Which breaks down to:

/Users/dhruv/anaconda/bin
/Users/dhruv/anaconda/bin
/Users/dhruv/.rvm/gems/ruby-2.2.1/bin
/Users/dhruv/.rvm/gems/ruby-2.2.1@global/bin
/Users/dhruv/.rvm/rubies/ruby-2.2.1/bin
/usr/local/bin
/usr/bin
/bin
/usr/sbin
/sbin
/Library/TeX/texbin
/Users/dhruv/.gem/ruby/2.0.0/bin
/Users/dhruv/scripts
/Users/dhruv/.rvm/bin
/Users/dhruv/.rvm/bin
/usr/local/lib
/Users/dhruv/.gem/ruby/2.0.0/bin
/Users/dhruv/scripts
/Users/dhruv/.rvm/bin
/Users/dhruv/.rvm/bin
/usr/local/lib

You need to have RVM first in the path but you're getting in its way.

It should look something like:

/Users/dhruv/.rvm/gems/ruby-2.2.1/bin
/Users/dhruv/.rvm/gems/ruby-2.2.1@global/bin
/Users/dhruv/.rvm/rubies/ruby-2.2.1/bin
/Users/dhruv/scripts
/Users/dhruv/anaconda/bin
/usr/local/bin
/usr/bin
/bin
/usr/sbin
/sbin
/Library/TeX/texbin

I'd rewrite ~/.bash_profile to:

source ~/.bashrc

export MONGO_PATH=/usr/local
export SBT_OPTS="-Xmx2G -XX:+UseConcMarkSweepGC -XX:+CMSClassUnloadingEnabled -XX:MaxPermSize=2G -Xss2M -Duser.timezone=GMT"

export PATH=~/scripts:~/anaconda/bin:$PATH

# RVM needs to initialize last so it can adjust the path correctly...
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM into a shell session *as a function*

I'd rewrite ~/.bashrc to:

# [[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm"

I'd rewrite ~/.profile to:

# export PATH="$PATH:$HOME/.rvm/bin" # Add RVM to PATH for scripting
# [[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM into a shell session *as a function*

That should clean up your path, and probably will allow RVM to figure things out better.

You might need to do minor tweaks but keep it simple.

I'd recommend reading about how the shell uses PATH to find things and how to define PATH. Also, it'd be good to read about using ~/.bashrc, ~/.profile and ~/.bash_profile, especially on Mac OS. man bash at the command-line says, among other things:

When bash is invoked as an interactive login shell, or as a non-interactive shell with the --login option, it first reads and executes commands from the file /etc/profile, if
that file exists. After reading that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and reads and executes commands from the first one
that exists and is readable. The --noprofile option may be used when the shell is started to inhibit this behavior.

[...]

When an interactive shell that is not a login shell is started, bash reads and executes commands from ~/.bashrc, if that file exists. This may be inhibited by using the
--norc option. The --rcfile file option will force bash to read and execute commands from file instead of ~/.bashrc.

When you install RVM using their default installation, it will create the ~/.rvm directory with the correct permissions. You won't have to do a thing after that to have RVM and Ruby store gems in that directory. Scripts will be able to find gems you've installed without you doing anything special with gem install. If it gets weird or hard something is wrong.

How do I install Ruby gems when using RVM?

It helps me to think of RVM as a layer of abstraction between you and the ruby ecosystem.

Without RVM: ruby, gems, and ruby related binaries (like rake, spec, gem, etc) are all installed directly into your operating system directories.

With RVM: ruby related stuff is intercepted by rvm so that ruby, gems, and ruby related binares are "installed" into ~/.rvm dir in a nice, clean, organized way. RVM sits between ruby, gems, and related binaries and the operating system. It provides a way to have multiple ruby environments (with different gems and binaries) on the same machine.

So, no matter whether you have rvm installed or not, you should be able to run the commands almost exactly(*) as they appear in any tutorials out there on the web. In other words, you can sort of "forget" that RVM is installed; the ruby ecosystem should work just as if it wasn't installed.

So, yep, you're gonna have to run gem install rails, etc.

Hope that helps clear the confusion.

(*) There are some small differences. For example: you shouldn't run commands as sudo when RVM is installed.

RVM gem permissions error

You missed to use ruby, you can do it with:

rvm use 1.9.3 --install

In case 1.9.3 was not installed, this command will also install it!

It also looks you are using Ubuntu, make sure you are not using RVM from Ubuntu package - that thing is broken! You can find instruction how to fix it here: https://stackoverflow.com/a/9056395/497756

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

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!



Related Topics



Leave a reply



Submit