如何“激活”某个特定 gem 的不同版本?

我想在 Rails 2.3.10之间切换,作为我的操作系统的“活动”gem,这样我就可以在命令行中调用它。

有可能做到这一点吗? 我没有使用 Rvm。也许是时候开始了。

我试过 gem install rails --version=2.3.10,但那只是确保了 gem 的版本已经安装,它没有把它放到 /usr/bin/rails中。

(我已经在我的应用程序中使用了 bundler ——但是直到现在才需要在操作系统级别对 gem 进行任何精确的控制)

51154 次浏览

EDIT: Just saw your RVM mention in the post. Definitely the way to go.

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.

Here's the installation guide: 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.

You can use RVM

Then you can also use Bundler afterwards, which manages gem dependencies fine.

In your Gemfile

gem "rails", "2.3.10"

and in your application

require 'rubygems'
require 'bundler/setup'

and you're done.

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.

Hope it helps.

Use RVM

RVM allows you to manage different versions of Ruby and Gems. You can install a version of ruby using, for example

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.

If you want further namespacing you can set up gemsets; directories which will contain specific gems for a specific project.

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-version and .ruby-gemset files 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.

Installing gems into your gemset

Install your gems into your gemset in the usual way using bundler if you are using it:

bundle install

or just using the regular old:

gem install mygem

The gems will go in the right gemset.

RVM Alternatives

You might also want to check out rbenv, which does similar job.