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


当前回答

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

$ gem install rails -v 0.14.1
…
Successfully installed rails-0.14.1

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

$ gem install rails -v '~> 0.14.0'
…
Successfully installed rails-0.14.4

使用更新版本的rubygems,您可以收紧您的要求:

$ gem install rails -v '~> 0.14.0, < 0.14.4'
…
Successfully installed rails-0.14.3

因为现在你也可以指定多个宝石的版本:

$ gem install rails:0.14.4 rubyzip:'< 1'
…
Successfully installed rails-0.14.4
Successfully installed rubyzip-0.9.9

但这并不适用于每个宝石超过一个比较器。

其他回答

对于Ruby 1.9+使用冒号。

gem install sinatra:1.4.4 prawn:0.13.0

您可以使用-v或——version标志。例如

gem install bitclock -v '< 0.0.2'

要指定上下版本边界,可以指定——version标志两次

gem install bitclock -v '>= 0.0.1' -v '< 0.0.2'

或者使用语法(例如)

gem install bitclock -v '>= 0.0.1, < 0.0.2'

另一种方法是

gem install bitclock:'>= 0.0.1'

但是对于最后一个选项,不可能同时指定上边界和下界。

[gem 3.0.3和ruby 2.6.6]

正如其他人所注意到的,通常情况下,gem安装命令使用-v标志。

如果你在本地开发宝石,从你的宝石规格中切割宝石后:

$ gem install gemname-version.gem

假设版本是0.8,它看起来是这样的:

$ gem install gemname-0.8.gem

Linux

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

$ 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

然后安装:

$ sudo apt-get install ruby=1:1.9.3

要查看当前版本,运行:

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

如果版本仍然是旧的,你可以尝试使用ruby版本管理器(rvm)切换到新的版本:

rvm 1.9.3

注意:如果rvm是全局安装的,则可以使用sudo作为前缀。如果rvm命令不在全局路径中,则运行/usr/local/rvm/scripts/rvm。如果rvm安装过程失败,请参考故障处理。


故障排除:

If you still have the old version, you may try to install rvm (ruby version manager) via: 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. then if installed locally (only for current user), load rvm via: source /usr/local/rvm/scripts/rvm; rvm 1.9.3 if globally (for all users), then: sudo bash -c "source /usr/local/rvm/scripts/rvm; rvm 1.9.3" if you still having problem with the new ruby version, try to install it by rvm via: 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. if you'd like to install some gems globally and you have rvm already installed, you may try: rvmsudo gem install [gemname] instead of: 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.

在安装之前,可以使用list命令检查可用的版本。

gem list ^[gemname]$ --remote --all
gem install [gemname] -v [version]