http://blog.163.com/digoal@126/blog/static/1638770402012425111617904/
rvm 全称Ruby Version Manager, 确实是一个非常好用的ruby版本管理以及安装工具.
下面介绍一下rvm的安装, 使用rvm, 安装ruby, 以及gem的使用.
一、安装rvm
官方网站上介绍得很简单, 但是使用官方网站安装会出现问题, SSL的问题. 所以我分两步进行, 第一步下载安装脚本. 第二步修改安装脚本. 第三步安装.
以下介绍的是使用root用户在CentOS中安装rvm的过程.
1. 下载脚本到ins文件中. 注意使用了curl的-k选项, 因为地址是https的. 不加-k会报异常.
[root@db-172-16-3-33 ~]# curl -k https://raw.github.com/wayneeseguin/rvm/master/binscripts/rvm-installer >./ins
2. 修改ins脚本中的curl都加上-k否则会无法使用.
vi ins需要修改两处curl.curl -sk https://api.github.com/repos/wayneeseguin/rvm/tagsif curl -kL ${_url} -o ${rvm_archives_path}/${_file}
3. 使用修改后的ins脚本安装rvm
[root@db-172-16-3-33 ~]# cat ins|bash -s stable输出结果 :Downloading RVM from wayneeseguin branch stable% Total % Received % Xferd Average Speed Time Time Time CurrentDload Upload Total Spent Left Speed100 1023k 100 1023k 0 0 173k 0 0:00:05 0:00:05 --:--:-- 383kInstalling RVM to /usr/local/rvm/Creating RVM system user group 'rvm'# RVM: Shell scripts enabling management of multiple ruby environments.# RTFM: https://rvm.io/# HELP: http://webchat.freenode.net/?channels=rvm (#rvm on irc.freenode.net)# Cheatsheet: http://cheat.errtheblog.com/s/rvm/# Screencast: http://screencasts.org/episodes/how-to-use-rvm# In case of any issues read output of 'rvm requirements' and/or 'rvm notes'Installation of RVM in /usr/local/rvm/ is almost complete:* First you need to add all users that will be using rvm to 'rvm' group,and logout - login again, anyone using rvm will be operating with `umask g+w`.* To start using RVM you need to run `source /etc/profile.d/rvm.sh`in all your open shell windows, in rare cases you need to reopen all shell windows.# root,## Thank you for using RVM!# I sincerely hope that RVM helps to make your life easier and more enjoyable!!!## ~Wayne
4. 修改root 下的环境变量
vi ~/.bash_profileexport PATH=/usr/local/rvm/bin:/opt/git1.7.10.2/bin:$PATH更新环境变量.. ~/.bash_profile这里就安装完rvm了
二、rvm的使用, 简单的介绍一下
[root@db5 ~]# rvm list helpUsage:rvm list [action]Actions:{help,known,gemsets,default [string],rubies,strings,known_strings,ruby_svn_tags}
1. 列出rvm管理的软件及版本
[root@db5 ~]# rvm list known# MRI Rubies[ruby-]1.8.6[-p420][ruby-]1.8.7[-p358][ruby-]1.8.7-head[ruby-]1.9.1[-p431][ruby-]1.9.2-p180[ruby-]1.9.2-p290[ruby-]1.9.2-p318[ruby-]1.9.2[-p320][ruby-]1.9.2-head[ruby-]1.9.3-preview1[ruby-]1.9.3-rc1[ruby-]1.9.3-p0[ruby-]1.9.3-p125[ruby-]1.9.3-[p194][ruby-]1.9.3-headruby-head# GoRubygoruby# TheCodeShop - MRI experimental patchestcs# JRubyjruby-1.2.0jruby-1.3.1jruby-1.4.0jruby-1.6.5jruby-1.6.5.1jruby-1.6.6jruby-1.6.7jruby[-1.6.7.2]jruby-headjruby-1.7.0.preview1# Rubiniusrbx-1.0.1rbx-1.1.1rbx-1.2.3rbx-1.2.4rbx[-head]rbx-2.0.testing# Ruby Enterprise Editionree-1.8.6ree[-1.8.7][-2012.02]# Kijikiji# MagLevmaglev[-head]maglev-1.0.0# Mac OS X Snow Leopard Or Newermacruby[-0.10]macruby-nightlymacruby-head# IronRuby -- Not implemented yet.ironruby-0.9.3ironruby-1.0-rc2ironruby-head
三、使用RVM安装ruby
从上面的输出我们能看到rvm管理的ruby版本中包含了当前的稳定版1.9.3-p194, 接下来就安装这个版本.
但是注意, 使用rvm安装的话需要联网, 怎么办呢? 可以不联网吗? 可能不可以, 因为要获得依赖关系什么的, 但是可以提前下载好安装包房到archives目录下, 这样可以省去下载包的过程.
1. 联网安装
[root@db5 ~]# rvm install ruby-1.9.3-p194输出Fetching yaml-0.1.4.tar.gz to /usr/local/rvm/archives% Total % Received % Xferd Average Speed Time Time Time CurrentDload Upload Total Spent Left Speed100 460k 100 460k 0 0 91728 0 0:00:05 0:00:05 --:--:-- 190kExtracting yaml-0.1.4.tar.gz to /usr/local/rvm/srcPrepare yaml in /usr/local/rvm/src/yaml-0.1.4.Configuring yaml in /usr/local/rvm/src/yaml-0.1.4.Compiling yaml in /usr/local/rvm/src/yaml-0.1.4.Installing yaml to /usr/local/rvm/usrInstalling Ruby from source to: /usr/local/rvm/rubies/ruby-1.9.3-p194, this may take a while depending on your cpu(s)...ruby-1.9.3-p194 - #fetchingruby-1.9.3-p194 - #downloading ruby-1.9.3-p194, this may take a while depending on your connection...% Total % Received % Xferd Average Speed Time Time Time CurrentDload Upload Total Spent Left Speed.... 略到安装rubygems的时候会出错, 无法获取版本号. 所以需要手工安装rubygems.-- 下载rubygems错误,貌似版本信息没有获取到.Retrieving rubygems-% Total % Received % Xferd Average Speed Time Time Time CurrentDload Upload Total Spent Left Speed0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0curl: (22) The requested URL returned error: 404The requested url does not exist: 'http://production.cf.rubygems.org/rubygems/rubygems-.tgz'Trying ftp:// URL instead.CTRL+C退出-- 手动安装rubygems, 见第3步
2. 下载好包后放到archives目录下再安装
如果下载太慢, 也可以先下载好yaml-0.1.4.tar.gz, ruby-1.9.3-p194.tar.bz2 放在/usr/local/rvm/archives下面
然后执行
rvm install ruby-1.9.3-p194
3. 安装rubygems
下载rubygems安装包.
wget http://production.cf.rubygems.org/rubygems/rubygems-1.8.24.tgz
解压
tar -zxvf rubygems-1.8.24.tgz
放到一个目录, (可选)
mv rubygems-1.8.24 /usr/local/rvm/src/
cd /usr/local/rvm/src/rubygems-1.8.24-- 由于是rvm管理的ruby环境, 所以在安装rubygems前, 先转到对应的ruby 环境, --default表示设置为默认的ruby环境[root@db-172-16-3-33 rubygems-1.8.24]# rvm use ruby-1.9.3-p194 --defaultUsing /usr/local/rvm/gems/ruby-1.9.3-p194干了什么呢? 弄了个软链接.[root@db5 rubies]# pwd /usr/local/rvm/rubies [root@db5 rubies]# ll total 4.0K lrwxrwxrwx 1 root rvm 37 May 25 10:47 default -> /usr/local/rvm/rubies/ruby-1.9.3-p194 drwxr-xr-x 6 root rvm 4.0K May 25 11:34 ruby-1.9.3-p194-- 安装rubygems[root@db-172-16-3-33 rubygems-1.8.24]# ruby setup.rb输出结果RubyGems 1.8.24 installed== 1.8.24 / 2012-04-27* 1 bug fix:* Install the .pem files properly. Fixes #320* Remove OpenSSL dependency from the http code path------------------------------------------------------------------------------RubyGems installed the following executables:/usr/local/rvm/rubies/ruby-1.9.3-p194/bin/gem
4. 列出当前rvm管理的软件
[root@db5 ruby-1.9.3-p194]# rvm listrvm rubies=* /usr/local/rvm/scripts/list: line 294: /usr/local/rvm/rubies/ruby-1.9.3-p194/config: No such file or directoryruby-1.9.3-p194 [ ]# => - current# =* - current && default# * - default
5. 重新安装
rvm reinstall ruby-1.9.3-p194
6. 移除已安装的软件
rvm remove ruby-1.9.3-p194
四、gem的使用介绍
1. 源管理,添加淘宝源
[root@db5 soft_bak]# gem source --add 'http://ruby.taobao.org/'http://ruby.taobao.org/ added to sources[root@db5 soft_bak]# gem source -l*** CURRENT SOURCES ***http://rubygems.org/http://ruby.taobao.org/
2. 更新源cache
[root@db5 soft_bak]# gem source -usource cache successfully updated
3. 查看源里有哪些软件
gem query -r -a-- 也可使用规则表达式[root@db5 soft_bak]# gem query -r -n 'redis'
*** REMOTE GEMS ***
active-redis (0.1.0 ruby)