使用 RVM 时如何安装 Ruby gems?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5758276/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I install Ruby gems when using RVM?
提问by Genadinik
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.
我设置了 RVM 并用它来安装 Ruby 和其他一些库。当我浏览各种教程和其他技术(如 Rails)的设置时,我开始对我应该通过 RVM 做什么以及我应该按照教程建议做什么感到困惑。
One example is the RubyGems tutorial here: http://rubygems.org/pages/download
一个例子是这里的 RubyGems 教程: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?
我应该下载他们正在谈论的那个 tar 文件吗?似乎没有必要,因为这就是我认为 RVM 的用途。我什至需要 RubyGems 吗?那真的是为了什么?
Also, how do I actually get Rails? Is there a precise RVM command to actually download and install Rails?
另外,我如何真正获得Rails?是否有一个精确的 RVM 命令来实际下载和安装 Rails?
回答by Upgradingdave
It helps me to think of RVM as a layer of abstraction between you and the ruby ecosystem.
它帮助我将 RVM 视为您与 ruby 生态系统之间的抽象层。
Without RVM: ruby, gems, and ruby related binaries (like rake, spec, gem, etc) are all installed directly into your operating system directories.
没有 RVM:ruby、gems 和 ruby 相关的二进制文件(如 rake、spec、gem 等)都直接安装到您的操作系统目录中。
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.
使用 RVM:ruby 相关的东西被 rvm 截获,以便 ruby、gems 和 ruby 相关的二进制文件以一种漂亮、干净、有组织的方式“安装”到 ~/.rvm 目录中。RVM 位于 ruby、gems 和相关的二进制文件和操作系统之间。它提供了一种在同一台机器上拥有多个 ruby 环境(具有不同的 gem 和二进制文件)的方法。
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.
因此,无论您是否安装了 rvm,您都应该能够几乎完全 (*) 运行这些命令,因为它们出现在网络上的任何教程中。换句话说,您可以“忘记”安装了 RVM;ruby 生态系统应该像没有安装一样工作。
So, yep, you're gonna have to run gem install rails, etc.
所以,是的,你必须跑gem install rails,等等。
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 时,您不应以 sudo 身份运行命令。
回答by the Tin Man
Should I download that tar file they are talking about?
我应该下载他们正在谈论的那个 tar 文件吗?
No. Ruby 1.9+ includes gems. RVM retrofits it for 1.8+.
不。Ruby 1.9+ 包括 gem。RVM 针对 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 sudoor as root.
一般来说,除非您有足够的经验来完全理解他们希望您做什么,否则请小心您在互联网上找到的任何说明如何安装任何内容。特别是,任何时候他们希望您使用sudo或作为 root安装某些东西。
Specifically, when working with RVM, you do NOT want to use sudoto 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. sudopushes 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.
具体来说,在使用 RVM 时,您不想sudo用来安装 Ruby或任何 gem。RVM 通过为您的开发设置沙箱来工作,并依赖于您的帐户环境,修改您的路径,以便任何 Ruby 请求都转到当前选择的 RVM 管理的 Ruby 或 gems 或它们安装的任何命令。sudo将您的正常环境推到一边,暂时替换 root 的,安装您要求它使用 root 权限执行的任何操作,然后恢复到您的环境。
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.
当你去运行命令,或者像你一样找到 gem 时,它不能被 RVM 的 Ruby 找到,因为该文件安装在 RVM 的沙箱之外,或者,它不能被读取或修改,因为它属于根。无论真正的原因是什么,最终的结果都是哭泣和咬牙切齿。
RVM doesn't subvert the gemfunctionality. gemis 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 gemworks as it always has, only it has "big brother", RVM, controlling its world.
RVM 不会破坏gem功能。gem用于安装和管理 Ruby gems,RVM 对其进行了调整,以使用沙箱进行所有操作。由于 RVM 对 gemset 的支持,您获得了额外的功能,但gem它一如既往地工作,只是它有“老大哥”,RVM,控制着它的世界。
回答by drewrobb
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 bundlerand then bundle installto get rails and other gem dependencies.
无需安装 rubygems。RVM 应该已经安装了 rubygems。RVM(在我看来)对于管理不同的 ruby 安装(比如 1.8.7 和 1.9.2)或不同的 gemset 很有用。如果您只有一个版本的 ruby 并且不关心不同的 gemset,那么 RVM 并没有太大的变化。如果您只想安装 rails,只需使用gem install rails. 如果您有现有的 rails 3 应用程序,请先安装 bundler gem install bundler,然后再bundle install获取 rails 和其他 gem 依赖项。

