How do I install Ruby gems when using RVM?

29,920

Solution 1

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.

Solution 2

Should I download that tar file they are talking about?

No. Ruby 1.9+ includes gems. RVM retrofits it for 1.8+.

In general, be careful with any directions you find on the internet explaining how to install anything, unless you have enough experience to understand completely what they want you to do. In particular, any time they want you to install something using sudo or as root.

Specifically, when working with RVM, you do NOT want to use sudo to install Ruby, or any gem. RVM works by setting up a sandbox for your development, and relies on your account's environment, modifying your path so any Ruby requests go to the currently selected RVM-managed Ruby or gems or any commands they install. sudo pushes your normal environment to the side, substituting root's temporarily, installs whatever you asked it to do with root's permissions, then reverts to your environment.

When you go to run the command, or find the gem, as you, it can't be found by RVM's Ruby, because the file was installed outside RVM's sandbox, or, it can't be read or modified, because it's owned by root. Whatever the actual cause, the end result will be weeping and gnashing of teeth.

RVM doesn't subvert the gem functionality. gem is used to install and manage Ruby gems, and RVM tweaks it to use the sandbox for all its machinations. You get added functionality because of RVM's support of gemsets, but gem works as it always has, only it has "big brother", RVM, controlling its world.

Solution 3

No need to install rubygems. RVM should have already installed rubygems. RVM is (in my opinion) useful for managing different installations of ruby (say 1.8.7 and 1.9.2) or different gemsets. If you just have one version of ruby and don't care about different gemsets, RVM isn't really that much of a change. If you want to just install rails, just use gem install rails. If you have an existing rails 3 app, install bundler first gem install bundler and then bundle install to get rails and other gem dependencies.

Share:
29,920
Genadinik
Author by

Genadinik

Thank you to everyone who has helped me. I am extremely appreciative of the amazing people on StackOverflow who have helped me :) I currently run https://www.problemio.com under which I offer mobile apps, books, coaching, and online courses, and even a B2B course licensing business: https://www.problemio.com/udemy/white-labeling-or-buying-udemy-courses.html I also run a funny t-shirt store: https://www.waveifyoulike.com

Updated on April 23, 2020

Comments

  • Genadinik
    Genadinik about 4 years

    I set up RVM and used it to install Ruby and a few other libraries. As I was going through various tutorials and set-ups of other technologies like Rails, I began getting confused about what I should do via RVM and what I should just do as the tutorials suggest.

    One example is the RubyGems tutorial here: http://rubygems.org/pages/download

    Should I download that tar file they are talking about? Seems unnecessary since that is what I thought RVM was for. Do I even need RubyGems? What is that for really?

    Also, how do I actually get Rails? Is there a precise RVM command to actually download and install Rails?

  • Genadinik
    Genadinik about 13 years
    So just for consistency sake, is there a way to install rails via RVM? What does "gem install rails" do? Does it use the ruby install which knows how to install/download rails? How does it all work? lol
  • Zabba
    Zabba about 13 years
    Do rvm gemdir. It will tell you where things with gem install will get installed. gem install rails will then be installed to that directory. All installed rubies are in ~/.rvm/rubies. The gems are in ~/.rvm/gems. Infact everything to do with rvm is in ~/.rvm. HTH
  • PJP
    PJP about 13 years
    "you should be able to run the commands exactly as they appear in any tutorials out there on the web.". No. Any tutorial recommending you use sudo to install a gem will fail. RVM specifically says "DO NOT use sudo...".
  • PJP
    PJP about 13 years
    Ruby 1.9+ installs RubyGems as part of its normal install. For Ruby < 1.9 I think RVM steps in to be helpful and includes gems.
  • KomodoDave
    KomodoDave about 11 years
    @theTinMan Your link's broken.
  • PJP
    PJP about 11 years
    Then you should have added the correct one in your comment. 'RVM specifically says "DO NOT use sudo...".
  • Adam Marshall
    Adam Marshall over 9 years
    Yeah, it's the sudo thing that threw me. I was so used to sudo'ing all my gems, and couldn't work out why installs kept failing! Now I get it.