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/tags
if 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 Current
Dload Upload Total Spent Left Speed
100 1023k 100 1023k 0 0 173k 0 0:00:05 0:00:05 --:--:-- 383k
Installing 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_profile
export PATH=/usr/local/rvm/bin:/opt/git1.7.10.2/bin:$PATH
更新环境变量.
. ~/.bash_profile
这里就安装完rvm了
二、rvm的使用, 简单的介绍一下
[root@db5 ~]# rvm list help
Usage:
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-head
ruby-head
# GoRuby
goruby
# TheCodeShop - MRI experimental patches
tcs
# JRuby
jruby-1.2.0
jruby-1.3.1
jruby-1.4.0
jruby-1.6.5
jruby-1.6.5.1
jruby-1.6.6
jruby-1.6.7
jruby[-1.6.7.2]
jruby-head
jruby-1.7.0.preview1
# Rubinius
rbx-1.0.1
rbx-1.1.1
rbx-1.2.3
rbx-1.2.4
rbx[-head]
rbx-2.0.testing
# Ruby Enterprise Edition
ree-1.8.6
ree[-1.8.7][-2012.02]
# Kiji
kiji
# MagLev
maglev[-head]
maglev-1.0.0
# Mac OS X Snow Leopard Or Newer
macruby[-0.10]
macruby-nightly
macruby-head
# IronRuby -- Not implemented yet.
ironruby-0.9.3
ironruby-1.0-rc2
ironruby-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 Current
Dload Upload Total Spent Left Speed
100 460k 100 460k 0 0 91728 0 0:00:05 0:00:05 --:--:-- 190k
Extracting yaml-0.1.4.tar.gz to /usr/local/rvm/src
Prepare 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/usr
Installing 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 - #fetching
ruby-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 Current
Dload Upload Total Spent Left Speed
.... 略
到安装rubygems的时候会出错, 无法获取版本号. 所以需要手工安装rubygems.
-- 下载rubygems错误,貌似版本信息没有获取到.
Retrieving rubygems-
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0
curl: (22) The requested URL returned error: 404
The 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 --default
Using /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 list
rvm rubies
=* /usr/local/rvm/scripts/list: line 294: /usr/local/rvm/rubies/ruby-1.9.3-p194/config: No such file or directory
ruby-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 -u
source 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)
adapter-redis (0.5.1 ruby)
bayes_on_redis (0.2.2 ruby)
bayes_on_redis_internal (0.0.1 ruby)
cache-machine-redis (0.0.1 ruby)
cs-em-hiredis (0.1.2 ruby)
datalogger-redis (0.0.2 ruby)
datastore_redis (0.0.8 ruby)
dm-redis-adapter (0.6.2 ruby)
dsander-redis (1.0.6 ruby)
dsander-redis-store (0.3.8 ruby)
easyredis (0.0.6 ruby)
em-hiredis (0.1.1 ruby)
em-redis (0.3.0 ruby)
em-redislite (0.2.2 ruby)
eventmachine-redis (0.1.3 ruby)
extraloop-redis-storage (0.0.12 ruby)
eycloud-recipe-redis (0.2.0 ruby)
fakeredis (0.3.1 ruby)
faye-redis (0.1.0 ruby)
feedzirra-redis (0.1.1 ruby)
fluent-plugin-redis (0.2.0 ruby)
goncalossilva_redis-session-store (0.1.8.1 ruby)
guard-redis (0.1.0 ruby)
hiera-redis (0.1.3 ruby)
hiera-redis-backend (0.0.1 ruby)
hiredis (0.4.5 ruby java java)
honkster-redis-store (0.3.16 ruby)
i18n_redis (0.0.2 ruby)
integration_test_redis (0.0.2 ruby)
jashmenn-redis-lock (0.1.1 ruby)
joffice_redis (0.1.1 ruby)
kali-redis (0.1.1 ruby)
mlanett-redis-lock (0.1.1 ruby)
mock_redis (0.4.1 ruby)
namespaced_redis (1.0.2 ruby)
newrelic-redis (1.3.2 ruby)
nono-redis-store (1.0.0 ruby)
opengotham_redis-namespace (0.4.4 ruby)
openid-redis-store (0.0.2 ruby)
orm-redis (0.0.0 ruby)
qu-redis (0.1.4 ruby)
rack-session-redis (0.0.3 ruby)
rails3-redis-session-store (0.3.0 ruby)
rails_redis_cache (0.2.0 ruby)
redis (3.0.0 ruby)
redis-actionpack (3.2.3 ruby)
redis-activesupport (3.2.3 ruby)
redis-aid (0.1.0 ruby)
redis-cache (1.0.1 ruby)
redis-classy (1.0.2 ruby)
redis-column (0.0.3 ruby)
redis-connection-ssl (0.0.1 ruby)
redis-connection-universal (0.0.1 ruby)
redis-diff_match_patch (1.1.1 ruby)
redis-dump (0.3.2 ruby)
redis-expiring-set (0.1.2 ruby)
redis-expiring_counter (1.0.0.0 ruby)
redis-friendships (0.2 ruby)
redis-health (0.0.1 ruby)
redis-i18n (0.6.0 ruby)
redis-load (0.2 ruby)
redis-lock (0.1.0 ruby)
redis-messages (0.1 ruby)
redis-mmm (0.1.1 ruby)
redis-model (0.1.3 ruby)
redis-model-extension (0.3.3 ruby)
redis-mutex (1.2.3 ruby)
redis-namespace (1.1.0 ruby)
redis-native_hash (0.2.1 ruby)
redis-objective (0.1.0 ruby)
redis-objects (0.5.2 ruby)
redis-orm (0.0.4 ruby)
redis-persistence (0.0.7 ruby)
redis-rack (1.4.1 ruby)
redis-rack-cache (1.2 ruby)
redis-rails (3.2.3 ruby)
redis-repeater (0.2.6 ruby)
redis-retry (0.1.0 ruby)
redis-roc (0.5.3 ruby)
redis-scheduler (0.4 ruby)
redis-scripted (0.0.3 ruby)
redis-search (0.8.0 ruby)
redis-semaphore (0.1.1 ruby)
redis-server (0.0.5 ruby)
redis-session-store (0.1.9 ruby)
redis-settings (0.2.1 ruby)
redis-sinatra (1.3.2 ruby)
redis-slave (0.0.3 ruby)
redis-spawn (0.0.5 ruby)
redis-store (1.1.0 ruby)
redis-structured-multi (0.0.2 ruby)
redis-textsearch (0.2.0 ruby)
redis-timeline (0.1.6 ruby)
redis-types (0.1.7 ruby)
redis-ui (0.1.3 ruby)
redis2-session-store (0.0.3 ruby)
redis_autocomplete (0.1.4 ruby)
redis_buddy (1.0.0 ruby)
redis_cache (0.0.1 ruby)
redis_cache_mailer_delivery (0.0.6 ruby)
redis_directory (1.0.4 ruby)
redis_failover (0.8.3 ruby)
redis_feature_control (0.0.2 ruby)
redis_feed (0.0.1 ruby)
redis_geo (0.0.0 ruby)
redis_knock (0.0.2 ruby)
redis_logger (0.1.0 ruby)
redis_master_slave (0.0.1 ruby)
redis_on_heroku (0.0.1 ruby)
redis_orm (0.6.2 ruby)
redis_props (0.3 ruby)
redis_record (0.0.1 ruby)
redis_resque_initializer (0.0.1 ruby)
redis_ring (0.1.3 ruby)
redis_ring_client (0.1.2 ruby)
redis_selector (0.1.0 ruby)
redis_session_store (0.0.2 ruby)
redis_snippets (0.0.5 ruby)
redis_status (0.1.1 ruby)
redis_storage (0.4.0 ruby)
redis_store_jr (0.2.1 ruby)
redis_support (0.0.18 ruby)
redis_throttle (0.1.0 ruby)
redis_voteable (0.1.2 ruby)
redis_wrapper (0.2.0 ruby)
redis_wrapper_rails (0.2.0 ruby)
redisarray (0.0.1 ruby)
rediscover (0.0.2 ruby)
redisearch (0.1.0 ruby)
redised (0.2.0 ruby)
redish (0.0.1 ruby)
redisk (0.2.2 ruby)
redislog (1.0.0 ruby)
redismapper (0.0.1 ruby)
redismodel (0.1.8 ruby)
redismq (0.0.6 ruby)
redisque (1.0.1 ruby)
redisrecord (0.1 ruby)
redisrpc (0.3.5 ruby)
redistat (0.5.0 ruby)
redisted (0.0.2 ruby)
redistry (0.1.3 ruby)
rspec-redis_helper (0.1.2 ruby)
ruby-redis (0.0.2 ruby)
ruby-redis-portertech (0.0.3 ruby)
ruby_redis_lock (0.1.0 ruby)
ruote-redis (2.2.0 ruby)
ryansch-mock_redis (0.3.0 ruby)
simple-redis-server (0.0.1 ruby)
simple_redis_cache (0.0.2 ruby)
simredis (0.0.5 ruby)
sinatra-redis (0.3.0 ruby)
sinatra-redis-auth (0.1.1 ruby)
sprockets-cache-redis (0.0.3 ruby)
superfeedr-em-redis (0.3.7 ruby)
synapse_redis_logger (0.1.0 ruby)
tavern-redis (0.0.2 ruby)
unified-redis (0.0.2 ruby)
uri-redis (0.4.2 ruby)
xlymian-redis-store (0.3.8 ruby)
yam-redis-with-retries (2.2.2.1 ruby)
4. 其他gem命令
[root@db5 ~]# gem help commands
GEM commands are:
build Build a gem from a gemspec
cert Manage RubyGems certificates and signing settings
check Check installed gems
cleanup Clean up old versions of installed gems in the local
repository
contents Display the contents of the installed gems
dependency Show the dependencies of an installed gem
environment Display information about the RubyGems environment
fetch Download a gem and place it in the current directory
generate_index Generates the index files for a gem server directory
help Provide help on the 'gem' command
install Install a gem into the local repository
list Display gems whose name starts with STRING
lock Generate a lockdown list of gems
outdated Display all gems that need updates
owner Manage gem owners on RubyGems.org.
pristine Restores installed gems to pristine condition from files
located in the gem cache
push Push a gem up to RubyGems.org
query Query gem information in local or remote repositories
rdoc Generates RDoc for pre-installed gems
search Display all gems whose name contains STRING
server Documentation and gem repository HTTP server
sources Manage the sources and cache file RubyGems uses to search
for gems
specification Display gem specification (in yaml)
stale List gems along with access times
uninstall Uninstall gems from the local repository
unpack Unpack an installed gem to the current directory
update Update the named gems (or all installed gems) in the local
repository
which Find the location of a library file you can require
For help on a particular command, use 'gem help COMMAND'.
Commands may be abbreviated, so long as they are unambiguous.
e.g. 'gem i rake' is short for 'gem install rake'.
【参考】