Ruby-on-rails 如何“激活”特定 gem 的不同版本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4373128/
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 "activate" a different version of a particular gem?
提问by John Bachir
I want to switch between rails 2.3.10 as the "active" gem for my OS, so that I can invoke it at the command line.
我想在 rails 2.3.10 之间切换作为我操作系统的“活动” gem,以便我可以在命令行中调用它。
Is it possible to do this? I'm not using rvm. Maybe it's time to start.
是否有可能做到这一点?我没有使用 rvm。也许是时候开始了。
I tried gem install rails --version=2.3.10, but that just makes sure that version of the gem is installed, it doesn't put it in /usr/bin/rails.
我试过了gem install rails --version=2.3.10,但这只是确保安装了 gem 的版本,它不会把它放在/usr/bin/rails.
(I do already use bundler for my apps -- but haven't needed any precise control over gems at the OS level until now)
(我确实已经为我的应用程序使用了 bundler——但直到现在还不需要在操作系统级别对 gem 进行任何精确控制)
回答by Daniel Vartanov
If your problem is to run binaries of a certain version, then:
如果您的问题是运行某个版本的二进制文件,则:
rails --version # => the latest version
rails _2.3.10_ --version # => Rails 2.3.10
This pattern (gem-binary _gem-version_) works for any gem binary.
此模式 ( gem-binary _gem-version_) 适用于任何 gem 二进制文件。
Hope it helps.
希望能帮助到你。
回答by superluminary
Use RVM
使用 RVM
RVM allows you to manage different versions of Ruby and Gems. You can install a version of ruby using, for example
RVM 允许您管理不同版本的 Ruby 和 Gems。例如,您可以使用安装一个版本的 ruby
rvm install 1.9.2
You can then use it using:
然后您可以使用它:
rvm use 1.9.2
Use specific gems on a per project basis with gemsets.
使用 gemsets 在每个项目的基础上使用特定的 gem。
If you want further namespacing you can set up gemsets; directories which will contain specific gems for a specific project.
如果你想要进一步的命名空间,你可以设置 gemsets;将包含特定项目的特定 gem 的目录。
rvm gemset create myproject
then you can use them like so:
那么你可以像这样使用它们:
rvm use 1.9.2@myproject
Automation
自动化
To automate the process of switching gems, pop .ruby-versionand .ruby-gemsetfiles in your project root. Pop the version of Ruby and name of the gemset you want to use inside them and RVM wil select the correct gemset when you cd into your project directory.
要自动化切换 gem 的过程,请在项目根目录中弹出.ruby-version和.ruby-gemset文件。弹出 Ruby 的版本和要在其中使用的 gemset 的名称,当您 cd 进入项目目录时,RVM 将选择正确的 gemset。
Installing gems into your gemset
将 gems 安装到您的 gemset 中
Install your gems into your gemset in the usual way using bundler if you are using it:
如果您正在使用 bundler,以通常的方式将您的 gem 安装到您的 gemset 中:
bundle install
or just using the regular old:
或者只是使用常规的旧:
gem install mygem
The gems will go in the right gemset.
宝石将进入正确的宝石组。
RVM Alternatives
RVM 替代品
You might also want to check out rbenv, which does similar job.
您可能还想查看执行类似工作的 rbenv。
回答by Chubas
回答by Sam Ritchie
EDIT: Just saw your RVM mention in the post. Definitely the way to go.
编辑:刚刚在帖子中看到您提到的 RVM。绝对是要走的路。
You're going to want to install RVM-- it's an amazing package that will let you manage different Rubys and different sets of gems on the same machine. You can switch back and forth with total ease.
您将要安装RVM—— 这是一个了不起的软件包,可以让您在同一台机器上管理不同的 Ruby 和不同的 gems。您可以轻松地来回切换。
Here's the installation guide: http://rvm.beginrescueend.com/rvm/install/
这是安装指南:http: //rvm.beginrescueend.com/rvm/install/
Once you got everything get up, you can see all of your installed rubys at the command line with with rvm list, and switch with rvm use ruby-head, for example. RVM keeps the gems on each ruby separate, which should help with your question.
一切就绪后,您可以在命令行中使用 withrvm list和 switch来查看所有已安装的 ruby rvm use ruby-head,例如。RVM 将每个 ruby 上的 gem 分开,这应该有助于解决您的问题。

