如何安装特定版本的ruby gem?

How to install a specific version of a ruby gem?

使用命令行gem工具,如何安装gem的特定版本?


使用-v标志:

1
$ gem install fog -v 1.8


使用--version参数(快捷方式-v):

1
$ gem install rails -v 0.14.1

您还可以使用版本比较器,如>=~>

1
$ gem install rails -v '~> 0.14.0'

或者是宝石的新版本:

1
2
3
4
$ gem install rails:0.14.4 rubyzip:'< 1'

Successfully installed rails-0.14.4
Successfully installed rubyzip-0.9.9


对于Ruby1.9+使用冒号。

1
gem install sinatra:1.4.4 prawn:0.13.0


用于安装gem install gemname -v versionnumber

用于卸载gem uninstall gemname -v versionnumber


正如其他人所指出的,一般情况下,-v标志用于gem install命令。

如果你在本地开发一块宝石,从你的宝石中切下一块宝石后:

1
$ gem install gemname-version.gem

假设版本为0.8,则如下所示:

1
$ gem install gemname-0.8.gem


Linux

要安装不同版本的Ruby,请使用apt检查包的最新版本,如下所示:

1
2
3
$ apt-cache madison ruby
      ruby |    1:1.9.3 | http://ftp.uk.debian.org/debian/ wheezy/main amd64 Packages
      ruby |        4.5 | http://ftp.uk.debian.org/debian/ squeeze/main amd64 Packages

然后安装:

1
$ sudo apt-get install ruby=1:1.9.3

要检查当前版本,请运行:

1
2
$ gem --version # Check for the current user.
$ sudo gem --version # Check globally.

如果版本仍然是旧的,您可以尝试通过以下方式将版本切换到新的版本:使用Ruby版本管理器(rvm

1
rvm 1.9.3

注意:如果在全球安装了rvm,您可以使用sudo作为前缀。或者运行/usr/local/rvm/scripts/rvm,如果您的命令rvm不在您的全局PATH中。如果RVM安装过程失败,请参阅故障排除部分。

故障排除:

  • 如果仍然有旧版本,可以尝试通过以下方式安装rvm(Ruby版本管理器):

    1
    2
    3
    sudo apt-get install curl # Install curl first
    curl -sSL https://get.rvm.io | bash -s stable --ruby # Install only for the user.
    #or:# curl -sSL https://get.rvm.io | sudo bash -s stable --ruby # Install globally.

    然后,如果在本地安装(仅限当前用户),则通过以下方式加载RVM:

    1
    source /usr/local/rvm/scripts/rvm; rvm 1.9.3

    如果是全局的(对于所有用户),则:

    1
    sudo bash -c"source /usr/local/rvm/scripts/rvm; rvm 1.9.3"
  • 如果新的Ruby版本仍然有问题,请尝试通过以下方式通过rvm进行安装:

    1
    2
    source /usr/local/rvm/scripts/rvm && rvm install ruby-1.9.3 # Locally.
    sudo bash -c"source /usr/local/rvm/scripts/rvm && rvm install ruby-1.9.3" # Globally.
  • 如果您想在全球范围内安装一些gems,并且已经安装了rvm,您可以尝试:

    1
    rvmsudo gem install [gemname]

    而不是:

    1
    2
      gem install [gemname] # or:
      sudo gem install [gemname]

Note: It's prefered to NOT use sudo to work with RVM gems. When you do
sudo you are running commands as root, another user in another shell
and hence all of the setup that RVM has done for you is ignored while
the command runs under sudo (such things as GEM_HOME, etc...). So to
reiterate, as soon as you 'sudo' you are running as the root system
user which will clear out your environment as well as any files it
creates are not able to be modified by your user and will result in
strange things happening.