1. 程式人生 > >MAC環境下配置Ruby環境

MAC環境下配置Ruby環境

步驟1 - 安裝 RVM

RVM 是幹什麼的這裡就不解釋了,後面你將會慢慢搞明白。

開啟終端輸入$ curl -L https://get.rvm.io | bash -s stable

期間可能會問你sudo管理員密碼,這裡可能需要等待一段時間後就可以成功安裝好 RVM。

案例資訊如下:

lihuifengdeMacBook-Pro:~ lihuifeng$ curl -L https://get.rvm.io | bash -s stable


  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   184  100   184    0     0      8      0  0:00:23  0:00:21  0:00:02     8
100 22817  100 22817    0     0    523      0  0:00:43  0:00:43 --:--:--  1056
Downloading https://github.com/wayneeseguin/rvm/archive/1.26.10.tar.gz
Downloading https://github.com/wayneeseguin/rvm/releases/download/1.26.10/1.26.10.tar.gz.asc
Found PGP signature at: 'https://github.com/wayneeseguin/rvm/releases/download/1.26.10/1.26.10.tar.gz.asc',
but no GPG software exists to validate it, skipping.

Upgrading the RVM installation in /Users/lihuifeng/.rvm/
    RVM PATH line found in /Users/lihuifeng/.profile /Users/lihuifeng/.bashrc /Users/lihuifeng/.zshrc.
    RVM sourcing line found in /Users/lihuifeng/.bash_profile /Users/lihuifeng/.zlogin.
Upgrade of RVM in /Users/lihuifeng/.rvm/ is complete.

# lihuifeng,
#
#   Thank you for using RVM!
#   We sincerely hope that RVM helps to make your life easier and more enjoyable!!!
#
# ~Wayne, Michal & team.

In case of problems: http://rvm.io/help and https://twitter.com/rvm_io

Upgrade Notes:

  * No new notes to display.


--這裡就表示已經成功安裝了--

然後,載入 RVM 環境

$ source ~/.rvm/scripts/rvm

檢查一下是否安裝正確 :輸入命令   rvm -v ,如果正確會列出RVM版本 如下所示:

$ rvm -v
A RVM version 1.26.10 (latest) is installed yet 1.25.28 (stable) is loaded.
Please do one of the following:
  * 'rvm reload'
  * open a new shell
  * 'echo rvm_auto_reload_flag=1 >> ~/.rvmrc' # for auto reload with msg.
  * 'echo rvm_auto_reload_flag=2 >> ~/.rvmrc' # for silent auto reload.

步驟2 - 用 RVM 安裝 Ruby 環境

  輸入命令  $ rvm install 2.0.0

$ rvm install 2.0.0

同樣繼續等待漫長的下載,編譯過程,完成以後就安裝好了。

A RVM version 1.26.10 (latest) is installed yet 1.25.28 (stable) is loaded.
Please do one of the following:
  * 'rvm reload'
  * open a new shell
  * 'echo rvm_auto_reload_flag=1 >> ~/.rvmrc' # for auto reload with msg.
  * 'echo rvm_auto_reload_flag=2 >> ~/.rvmrc' # for silent auto reload.

步驟3 - 設定 Ruby 版本

RVM 裝好以後,需要執行下面的命令將指定版本的 Ruby 設定為系統預設版本

$ rvm 2.0.0 --default

同樣,也可以用其他版本號,前提是你有用 rvm install 安裝過那個版本

測試是否正確

lihuifengdeMacBook-Pro:~ lihuifeng$ ruby -v
ruby 2.0.0p481 (2014-05-08 revision 45883) [x86_64-darwin13.1.0]
lihuifengdeMacBook-Pro:~ lihuifeng$ gem -v
2.2.2

這有可能是因為Ruby的預設源使用的是cocoapods.org,國內訪問這個網址有時候會有問題,網上的一種解決方案是將遠替換成淘寶的,替換方式如下:
$gem source -r https://rubygems.org/
$gem source -a https://ruby.taobao.org

lihuifengdeMacBook-Pro:~ lihuifeng$ gem source -r https://rubygems.org/
https://rubygems.org/ removed from sources
lihuifengdeMacBook-Pro:~ lihuifeng$ gem source -a https://ruby.taobao.org
https://ruby.taobao.org added to sources

 要想驗證是否替換成功了,可以執行:

    $ gem sources -l  

正常的輸出結果:

lihuifengdeMacBook-Pro:~ lihuifeng$ gem sources -l
*** CURRENT SOURCES ***

https://ruby.taobao.org

這裡就表示已經把Ruby環境成功的安裝到了Mac 上了。

用RVM升級Ruby

  1. #檢視當前ruby版本
  2. $ ruby -v  
  3. ruby 1.8.7  
  4. #列出已知的ruby版本
  5. $ rvm list known  
  6. #安裝ruby  2.3
  7. $ rvm install 2.3  
         設定當前版本號為預設:rvm use 2.3 --default