rvm ruby 多版本控制

Posted by wxianfeng Mon, 22 Nov 2010 21:07:00 GMT

最终环境:ubuntu10.10 + ruby 1.8.7 + ruby 1.9.2 + rails 2.3.5 + rails 3.0.3

新的项目,使用最新的ruby1.9.2 + rails3.X开发,所以 环境必须重新搭建 ,但是又要能使之前的rails2.X项目跑起来,所以,就用到了rvm来控制,rvm确实不错,一直用部署的capistrano差不多,很方便,rvm的原理是动态的改变系统的path,即ruby的bin目录,来调用不同的ruby版本的,都有自己独立的文件夹,这样管理rails的那些gem也就独立开来了,核心是一个script,有兴趣的可以看下,$HOME/.rvm/scripts/rvm , 这个shell脚本,下面介绍安装过程:

需要用到 git 和 curl

sudo apt-get install git
sudo apt-get install curl

注意首先把原来的ruby bin目录从PATH中去掉

安装 rvm

cd ~
bash < <( curl http://rvm.beginrescueend.com/releases/rvm-install-head )
cd .rvm/src/rvm
./install

查看 rvm 安装目录

wxianfeng@ubuntu:~/.rvm/gemsets/ruby$ echo $rvm_path
/home/wxianfeng/.rvm

~/.bashrc中添加下面代码(PATH之用) 很重要

if [[ -s "$HOME/.rvm/scripts/rvm" ]]  ; then source "$HOME/.rvm/scripts/rvm" ; fi  

安装 ruby 1.9.2

>rvm notes
sudo apt-get install build-essential bison openssl libreadline5 libreadline5-dev curl git zlib1g zlib1g-dev libssl-dev libyaml-dev libsqlite3-0 libsqlite3-dev sqlite3 libxml2-dev autoconf
rvm install 1.9.2

查看:

wxianfeng@ubuntu:~$ rvm list
rvm rubies
   ruby-1.9.2-p0 [ i386 ]

设置默认的 ruby 版本(必须设置)

>rvm --default ruby-1.9.2-p0

可以看到 相关信息

wxianfeng@ubuntu:~$ rvm info

ruby-1.9.2-p0:

  system:
    uname:       "Linux ubuntu 2.6.35-22-generic #33-Ubuntu SMP Sun Sep 19 20:34:50 UTC 2010 i686 GNU/Linux"
    bash:        "/bin/bash => GNU bash, version 4.1.5(1)-release (i686-pc-linux-gnu)"
    zsh:         " => not installed"

  rvm:
    version:      "rvm 1.1.0 by Wayne E. Seguin (wayneeseguin@gmail.com) [http://rvm.beginrescueend.com/]"

  ruby:
    interpreter:  "ruby"
    version:      "1.9.2p0"
    date:         "2010-08-18"
    platform:     "i686-linux"
    patchlevel:   "2010-08-18 revision 29036"
    full_version: "ruby 1.9.2p0 (2010-08-18 revision 29036) [i686-linux]"

  homes:
    gem:          "/home/wxianfeng/.rvm/gems/ruby-1.9.2-p0"
    ruby:         "/home/wxianfeng/.rvm/rubies/ruby-1.9.2-p0"

  binaries:
    ruby:         "/home/wxianfeng/.rvm/rubies/ruby-1.9.2-p0/bin/ruby"
    irb:          "/home/wxianfeng/.rvm/rubies/ruby-1.9.2-p0/bin/irb"
    gem:          "/home/wxianfeng/.rvm/rubies/ruby-1.9.2-p0/bin/gem"
    rake:         "/home/wxianfeng/.rvm/gems/ruby-1.9.2-p0/bin/rake"

  environment:
    PATH:         "/home/wxianfeng/.rvm/gems/ruby-1.9.2-p0/bin:/home/wxianfeng/.rvm/gems/ruby-1.9.2-p0@global/bin:/home/wxianfeng/.rvm/rubies/ruby-1.9.2-p0/bin:/home/wxianfeng/.rvm/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/system/mysql/bin:/usr/local/system/jdk1.6.0_22/bin"
    GEM_HOME:     "/home/wxianfeng/.rvm/gems/ruby-1.9.2-p0"
    GEM_PATH:     "/home/wxianfeng/.rvm/gems/ruby-1.9.2-p0:/home/wxianfeng/.rvm/gems/ruby-1.9.2-p0@global"
    MY_RUBY_HOME: "/home/wxianfeng/.rvm/rubies/ruby-1.9.2-p0"
    IRBRC:        "/home/wxianfeng/.rvm/rubies/ruby-1.9.2-p0/.irbrc"
    RUBYOPT:      ""
    gemset:       ""

这时发现你的PATH已经变了

wxianfeng@ubuntu:~$ echo $PATH
/home/wxianfeng/.rvm/gems/ruby-1.9.2-p0/bin:/home/wxianfeng/.rvm/gems/ruby-1.9.2-p0@global/bin:/home/wxianfeng/.rvm/rubies/ruby-1.9.2-p0/bin:/home/wxianfeng/.rvm/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/system/mysql/bin:/usr/local/system/jdk1.6.0_22/bin
wxianfeng@ubuntu:~$ ruby -v
ruby 1.9.2p0 (2010-08-18 revision 29036) [i686-linux]

ruby 1.9.2 安装成功

安装rails

wxianfeng@ubuntu:~$ gem install rails --pre --no-ri --no-rdoc
/home/wxianfeng/.rvm/rubies/ruby-1.9.2-p0/bin/gem:4: warning: Insecure world writable dir /usr/local/system in PATH, mode 040777
Successfully installed activesupport-3.0.0.rc2
Successfully installed builder-2.1.2
Successfully installed i18n-0.4.2
Successfully installed activemodel-3.0.0.rc2
Successfully installed rack-1.2.1
Successfully installed rack-test-0.5.6
Successfully installed rack-mount-0.6.13
Successfully installed tzinfo-0.3.23
Successfully installed abstract-1.0.0
Successfully installed erubis-2.6.6
Successfully installed actionpack-3.0.0.rc2
Successfully installed arel-1.0.0.rc1
Successfully installed activerecord-3.0.0.rc2
Successfully installed activeresource-3.0.0.rc2
Successfully installed mime-types-1.16
Successfully installed polyglot-0.3.1
Successfully installed treetop-1.4.9
Successfully installed mail-2.2.10
Successfully installed actionmailer-3.0.0.rc2
Successfully installed thor-0.14.6
Successfully installed railties-3.0.0.rc2
Successfully installed bundler-1.0.0.rc.6
Successfully installed rails-3.0.0.rc2
23 gems installed

有提示我path中有777权限目录,看着不顺眼,解决掉:

sudo chmod go-w /usr/local/system

发现安装上的是 3.0.0.rc2 不是我想要的,目前最新版是 3.0.3

咋办,用shell脚本全部remove掉

shell:

#!/bin/bash 

for i in `gem list`
do
        gem uninstall $i
done

最后发现rake无法uninstall掉,好那就进入目录把rake delete掉

wxianfeng@ubuntu:~$ gem list -d rake
/home/wxianfeng/.rvm/rubies/ruby-1.9.2-p0/bin/gem:4: warning: Insecure world writable dir /usr/local/system in PATH, mode 040777

*** LOCAL GEMS ***

rake (0.8.7)
    Author: Jim Weirich
    Rubyforge: http://rubyforge.org/projects/rake
    Homepage: http://rake.rubyforge.org
    Installed at: /home/wxianfeng/.rvm/gems/ruby-1.9.2-p0@global

    Ruby based make-like utility.


>cd /home/wxianfeng/.rvm/gems/ruby-1.9.2-p0@global

wxianfeng@ubuntu:~/.rvm/gems/ruby-1.9.2-p0@global$ ll
total 28
drwxr-xr-x 7 wxianfeng wxianfeng 4096 2010-11-23 14:14 ./
drwxr-xr-x 5 wxianfeng wxianfeng 4096 2010-11-23 14:14 ../
drwxr-xr-x 2 wxianfeng wxianfeng 4096 2010-11-23 15:21 bin/
drwxr-xr-x 2 wxianfeng wxianfeng 4096 2010-11-23 15:22 cache/
drwxr-xr-x 2 wxianfeng wxianfeng 4096 2010-11-23 15:22 doc/
drwxr-xr-x 2 wxianfeng wxianfeng 4096 2010-11-23 15:20 gems/
drwxr-xr-x 2 wxianfeng wxianfeng 4096 2010-11-23 15:21 specifications/

把bin cache doc gems specifications 下和rake相关的信息全部remove掉

重新安装成功 rails 3.0.3

wxianfeng@ubuntu:~$ gem install rails -v=3.0.3 --no-ri --no-rdoc
/home/wxianfeng/.rvm/rubies/ruby-1.9.2-p0/bin/gem:4: warning: Insecure world writable dir /usr/local/system in PATH, mode 040777
Successfully installed activesupport-3.0.3
Successfully installed builder-2.1.2
Successfully installed i18n-0.4.2
Successfully installed activemodel-3.0.3
Successfully installed rack-1.2.1
Successfully installed rack-test-0.5.6
Successfully installed rack-mount-0.6.13
Successfully installed tzinfo-0.3.23
Successfully installed abstract-1.0.0
Successfully installed erubis-2.6.6
Successfully installed actionpack-3.0.3
Successfully installed arel-2.0.4
Successfully installed activerecord-3.0.3
Successfully installed activeresource-3.0.3
Successfully installed mime-types-1.16
Successfully installed polyglot-0.3.1
Successfully installed treetop-1.4.9
Successfully installed mail-2.2.10
Successfully installed actionmailer-3.0.3
Successfully installed rake-0.8.7
Successfully installed thor-0.14.6
Successfully installed railties-3.0.3
Successfully installed bundler-1.0.7
Successfully installed rails-3.0.3
24 gems installed
wxianfeng@ubuntu:~$ rails -v
Rails 3.0.3

安装 ruby 1.8.7

>rvm install 1.8.7

切换到 1.8.7

wxianfeng@ubuntu:~/.rvm/gemsets/ruby$ rvm 1.8.7
wxianfeng@ubuntu:~/.rvm/gemsets/ruby$ gem list
/home/wxianfeng/.rvm/rubies/ruby-1.8.7-p302/bin/gem:4: warning: Insecure world writable dir /usr/local/system in PATH, mode 040777

*** LOCAL GEMS ***

rake (0.8.7)

悲剧的发现 其实 默认rake是安装上的,之前我还把它 给干掉 重新安装了

安装 rails 2.3.5

>gem install rails -v=2.3.5 --no-ri --no-rdoc

以后切换的话 就用 rvm —default 1.8.7 或者 rvm —default 1.9.2 切换默认的ruby版本,可以使用 rvm 1.8.7 切换当前终端版本到1.8.7 , 也就是可以同时开ruby 1.8.7 , ruby 1.9.2 的项目了

安装之前ruby 1.8.7 + rails 2.3.5 项目的 所有gems (注意把ruby版本切到1.8.7), 如果已经安装了,就不安装:

shell:

#!/bin/bash 
# 安装所有的gems,如果已经安装了就不安装

cd /usr/local/system/ruby/lib/ruby/gems/1.8/cache 


for i in `ls`;do
        gem=`echo $i | awk -F'-' '{print $1}'`
        version=`echo $i | grep -o "\-[0-9].*" | sed 's/^-//;s/.gem//'`
        is_gem_exist=`gem list $gem -v=$version`
        if [ -z "$is_gem_exist" ]; then # 注意[] 内部两边留空格
                `gem install $i`
        else
                echo "$i have installed"
        fi
done

这样,之前ruby 1.8.7+rails2.3.4的项目 和现在的 ruby 1.9.2 + rails 3.0.3 都兼容了…oh…YEAR

问题: netbeans 配置使用 ruby 1.8.7 + rails 2.3.5 OR ruby 1.9.2 + rails 3.0.3 ?
更改netbeans 的ruby path ,选择 autodetect 会把ruby的bin全部找出来,但是注意gem path 有可能错误,通过 rvm info 看下 gem 的位置,然后手动更改即可~!

SEE:

http://thoughtrails.com/episodes/1-installing-rails-3-on-ubuntu-10-04-lucid-lynx-with-rvm
http://rvm.beginrescueend.com/rvm/install/