
“Living on the edge, stay in the beta world”是件有趣的事,可以試一些新玩具、新想法,但也很怕把系統弄爛又要花時間重灌,以前可能會用virtual machine來建立不同的環境,不過實在有點麻煩。在Ruby有個東西叫做RVM(Ruby Version Manager),可以讓你同時安裝多個不同版本的Ruby,看您是想要裝MRI版本的Ruby,或是REE(Ruby Enterprise Edition)、JRuby、MacRuby,都可以裝在RVM裡指後歡樂的切換。RVM裡每個版本的Ruby的gem也都是分開裝的,所以像之前Rails 3還沒正式release,想裝又不知道會不會怕把原來工作的環境搞爛,但不裝又覺得手很癢,這時候用RVM就對了。
安裝
安裝RVM會至少需要兩項工具:curl跟git,你可以檢查一下你電腦裡是否已經安裝,如果沒有的話就把這兩個裝起來吧,反正這兩個也是很棒也很常用的工具。 如果上面這兩個都已經安裝好的話,打開你的終端機,貼上這行:
bash < <(curl -s https://rvm.beginrescueend.com/install/rvm)
它應該就會開始去下載RVM的檔案回來安裝了。安裝完還需要做一下簡單的設定,看你用的shell是哪一套而要要改不同的檔案,例如zsh就改.zshrc,bash就改.bashrc,以此類推,把下面這行加進去:
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm"
假設你用的是ubuntu預設的bash shell的話,那可以直接貼下面這行:
echo '[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm"' >> ~/.bashrc
更詳細的安裝說明可以參考官網說明。
使用方法
簡單介紹幾個常用的RVM指令。
看看目前有哪些可以安裝的列表:
> rvm list known
安裝特定版本的Ruby:
> rvm install 1.8.7
會自動找1.8.7的最新版來裝,如果想再指定小版本:
> rvm install ruby-1.8.7-p334
列出目前已經安裝在RVM裡的Ruby版本:
> rvm list
rvm rubies
jruby-1.6.0 [ darwin-x86_64-java ]
ree-1.8.7-2010.02 [ x86_64 ]
ruby-1.8.7-p334 [ x86_64 ]
=> ruby-1.9.2-p0 [ x86_64 ]
可以看到我電腦上目前裝了4套(RVM的) + 1套(系統內建)的Ruby,目前正切換到1.9.2版本。如果要切換到Ruby 1.8.7的版本:
> rvm use 1.8.7
如果要切換到Ruby-1.8.7-p334的版本:
> rvm use ruby-1.8.7-p334
use也可以省略:
> rvm 1.8.7
但RVM會在下次terminal重開的時候會回到預設值(就是回到你系統內建的Ruby版本)。如果你希望每次開terminal的時候都會切到ruby-1.8.7的話:
> rvm 1.8.7 --default
這樣你每次開termnal就會自動切換到1.8.7版了。如果想切回到原來系統內建的版本:
> rvm system
不過今天要看的是RVM裡的gemset。
因為其實套件的版本可能一直有在變,所以可能同一個套件就裝了二、三個以上的版本,看了很討厭,而且也可知道會不會遇到版本衝突的問題,而gemset就是可以讓你在RVM裡面再建立獨立的空間來安裝套件用的。假設我想同時在REE-1.8.7版本的ruby底下同時安裝Rails 2.3.9跟3.0.6版本,來看看怎麼做:
先切換到REE之後,建立一個準備要來裝Rails 2.3.9版的gemset:
> rvm gemset create r239
'r239' gemset created (/Users/eddie/.rvm/gems/ree-1.8.7-2010.02@r239).
再來建一個給Rails 3.0.6版的:
> rvm gemset create r306
'r306' gemset created (/Users/eddie/.rvm/gems/ree-1.8.7-2010.02@r306).
看一下目前的已經建立的gemset:
> rvm gemset list
gemsets for ree-1.8.7-2010.02 (found in /Users/eddie/.rvm/gems/ ree-1.8.7-2010.02)
r239
r306
現在有2個gemset了,我們先切換到r239:
> rvm gemset use r239
Now using gemset 'r239'
來安裝一下Rails 2.3.9版:
> gem install rails -v='2.3.9' --no-rdoc --no-ri
Fetching: rake-0.8.7.gem (100%)
Fetching: activesupport-2.3.9.gem (100%)
Fetching: activerecord-2.3.9.gem (100%)
Fetching: rack-1.1.2.gem (100%)
Fetching: actionpack-2.3.9.gem (100%)
Fetching: actionmailer-2.3.9.gem (100%)
Fetching: activeresource-2.3.9.gem (100%)
Fetching: rails-2.3.9.gem (100%)
Successfully installed rake-0.8.7
Successfully installed activesupport-2.3.9
Successfully installed activerecord-2.3.9
Successfully installed rack-1.1.2
Successfully installed actionpack-2.3.9
Successfully installed actionmailer-2.3.9
Successfully installed activeresource-2.3.9
Successfully installed rails-2.3.9
8 gems installed
安裝完成,看起來沒問題,再來切換到r306:
> rvm gemset use r306
Now using gemset 'r306'
再來做一樣的動作,只是版號改成3.0.6:
> gem install rails -v='3.0.6' --no-rdoc --no-ri
Fetching: activesupport-3.0.6.gem (100%)
Fetching: builder-2.1.2.gem (100%)
Fetching: i18n-0.5.0.gem (100%)
Fetching: activemodel-3.0.6.gem (100%)
Fetching: rack-1.2.2.gem (100%)
Fetching: rack-test-0.5.7.gem (100%)
Fetching: rack-mount-0.6.14.gem (100%)
Fetching: tzinfo-0.3.26.gem (100%)
Fetching: abstract-1.0.0.gem (100%)
Fetching: erubis-2.6.6.gem (100%)
Fetching: actionpack-3.0.6.gem (100%)
Fetching: arel-2.0.9.gem (100%)
Fetching: activerecord-3.0.6.gem (100%)
Fetching: activeresource-3.0.6.gem (100%)
Fetching: mime-types-1.16.gem (100%)
Fetching: polyglot-0.3.1.gem (100%)
Fetching: treetop-1.4.9.gem (100%)
Fetching: mail-2.2.15.gem (100%)
Fetching: actionmailer-3.0.6.gem (100%)
Fetching: rake-0.8.7.gem (100%)
Fetching: thor-0.14.6.gem (100%)
Fetching: railties-3.0.6.gem (100%)
Fetching: bundler-1.0.11.gem (100%)
Fetching: rails-3.0.6.gem (100%)
Successfully installed activesupport-3.0.6
Successfully installed builder-2.1.2
Successfully installed i18n-0.5.0
Successfully installed activemodel-3.0.6
Successfully installed rack-1.2.2
Successfully installed rack-test-0.5.7
Successfully installed rack-mount-0.6.14
Successfully installed tzinfo-0.3.26
Successfully installed abstract-1.0.0
Successfully installed erubis-2.6.6
Successfully installed actionpack-3.0.6
Successfully installed arel-2.0.9
Successfully installed activerecord-3.0.6
Successfully installed activeresource-3.0.6
Successfully installed mime-types-1.16
Successfully installed polyglot-0.3.1
Successfully installed treetop-1.4.9
Successfully installed mail-2.2.15
Successfully installed actionmailer-3.0.6
Successfully installed rake-0.8.7
Successfully installed thor-0.14.6
Successfully installed railties-3.0.6
Successfully installed bundler-1.0.11
Successfully installed rails-3.0.6
24 gems installed
跟2.3.9不一樣,Rails 3.0.6版裝了一堆東西..兩個gemset都裝好了,如果你要從別的版本的Ruby直接切換到指定的gemset:
> rvm ree-1.8.7-2010.02@r239
看一下安裝的gem:
> gem list
*** LOCAL GEMS ***
actionmailer (2.3.9)
actionpack (2.3.9)
activerecord (2.3.9)
activeresource (2.3.9)
activesupport (2.3.9)
rack (1.1.2)
rails (2.3.9)
rake (0.8.7)
是2.3.9版的沒錯,再試著換到另一個gemset:
> rvm gemset use r306
Now using gemset 'r306'
> gem list
*** LOCAL GEMS ***
abstract (1.0.0)
actionmailer (3.0.6)
actionpack (3.0.6)
activemodel (3.0.6)
activerecord (3.0.6)
activeresource (3.0.6)
activesupport (3.0.6)
arel (2.0.9)
builder (2.1.2)
bundler (1.0.11)
erubis (2.6.6)
i18n (0.5.0)
mail (2.2.15)
mime-types (1.16)
polyglot (0.3.1)
rack (1.2.2)
rack-mount (0.6.14)
rack-test (0.5.7)
rails (3.0.6)
railties (3.0.6)
rake (0.8.7)
thor (0.14.6)
treetop (1.4.9)
tzinfo (0.3.26)
Good! 看起來沒問題。
你可以隨你高興的建立、切換gemset,各個gemset都是獨立的。如果玩膩了或玩爛了,想要把r239這個gemset清空的話:
> rvm gemset empty r239
WARN: Are you SURE you wish to remove the installed gemset for gemset 'ree-1.8.7-2010.02@r306' (/Users/eddie/.rvm/gems/ree-1.8.7-2010.02@r306)?
(anything other than 'yes' will cancel) > yes
清空只會把安裝的gem砍掉,gemset的名字還會在;如果想把整個gemset刪掉:
> rvm gemset delete r239
WARN: Are you SURE you wish to remove the entire gemset directory 'r239' (/Users/eddie/.rvm/gems/ree-1.8.7-2010.02@r239)?
(anything other than 'yes' will cancel) > yes
大概是這樣,RVM對我這種愛玩新玩具的人真的福音啊!
好RVM,不裝嗎?