Using RVM and installing rspec gem for Ruby

rubyrvmterminal

I think I installed rvm correctly. When I type in rvm -v it outputs

rvm 1.25.33 (stable) by Wayne E. Seguin <wayneeseguin@gmail.com>, Michal Papis <mpapis@gmail.com> [https://rvm.io/]

However, I am trying to install the gem rspec. I type in

gem install rspec

but the output is

ERROR:  While executing gem ... (Gem::FilePermissionError)
You don't have write permissions for the /Library/Ruby/Gems/2.0.0 directory.

What should I do? What is going wrong? I can't understand the rvm docs either!

Best Answer

RVM controls which Ruby your current shell points to, but it doesn't install a user-controlled Ruby for you by default. If you haven't installed any specific Ruby versions via RVM, the only Ruby you have is the one OS X installed and that requires sudo permissions to write to.

You can see available Rubies with:

> rvm list

rvm rubies

=* ruby-2.1.2 [ x86_64 ]

# => - current
# =* - current && default
#  * - default

In my case I have 2.1.2 installed and it's both the current Ruby and the default Ruby when I start a new shell:

> which ruby
/Users/ian/.rvm/rubies/ruby-2.1.2/bin/ruby

To install a Ruby version with RVM do:

> rvm install ruby-2.1
No binary rubies available for: osx/10.10/x86_64/ruby-2.1.3.
Continuing with compilation. Please read 'rvm help mount' to get more information on binary rubies.
...snip...
Install of ruby-2.1.3 - #complete
Ruby was built without documentation, to build it run: rvm docs generate-ri

Now that you have an RVM-managed Ruby installed that's owned by you specifically you can gem-install anything you like without needing to use sudo:

> rvm list

rvm rubies

 * ruby-2.1.2 [ x86_64 ]
=> ruby-2.1.3 [ x86_64 ]

# => - current
# =* - current && default
#  * - default
> rvm gemdir
/Users/ian/.rvm/gems/ruby-2.1.3
> gem install packer-config

That gem has been installed for the 2.1.3 version of Ruby that RVM put under ~/.rvm for me. If I was to switch to the 2.1.2 version via rvm use ruby-2.1.2 I would not see that gem, I'd have to re-install it to make it available to that Ruby version.

You can also manage gems as sets so they're not even shared with a Ruby install. See the gemset basics on the RVM web site for more details.