在ubuntu中安装及使用rvm管理ruby版本

阅读更多
RVM的主要作用是方便的管理系统中的多个ruby版本而不至于混乱。

我们来看看如何安装RVM,我使用的ubuntu12.04LTS。

准备工作
后面需要使用curl,用dpkg -s curl命令检查一下系统中有没有安装


[plain] view plaincopyprint?
01.abbuggy@abbuggy-ubuntu:~$ dpkg -s curl 
02.Package: curl 
03.Status: install ok installed 
04.Priority: optional 
05.Section: web 
06.Installed-Size: 338 
07.Maintainer: Ubuntu Developers  
08.Architecture: i386 
09.Version: 7.22.0-3ubuntu4 
10.Replaces: curl-ssl 
11.Provides: curl-ssl 
12.Depends: libc6 (>= 2.7), libcurl3 (>= 7.16.2-1), zlib1g (>= 1:1.1.4) 
13.Description: Get a file from an HTTP, HTTPS or FTP server 
14. curl is a client to get files from servers using any of the supported 
15. protocols. The command is designed to work without user interaction 
16. or any kind of interactivity. 
17. . 
18. curl offers a busload of useful tricks like proxy support, user 
19. authentication, FTP upload, HTTP post, file transfer resume and more. 
20.Homepage: http://curl.haxx.se 
21.Original-Maintainer: Ramakrishnan Muthukrishnan  
abbuggy@abbuggy-ubuntu:~$ dpkg -s curl
Package: curl
Status: install ok installed
Priority: optional
Section: web
Installed-Size: 338
Maintainer: Ubuntu Developers
Architecture: i386
Version: 7.22.0-3ubuntu4
Replaces: curl-ssl
Provides: curl-ssl
Depends: libc6 (>= 2.7), libcurl3 (>= 7.16.2-1), zlib1g (>= 1:1.1.4)
Description: Get a file from an HTTP, HTTPS or FTP server
curl is a client to get files from servers using any of the supported
protocols. The command is designed to work without user interaction
or any kind of interactivity.
.
curl offers a busload of useful tricks like proxy support, user
authentication, FTP upload, HTTP post, file transfer resume and more.
Homepage: http://curl.haxx.se
Original-Maintainer: Ramakrishnan Muthukrishnan

上面的会先说明我是已经安装过了的,如果没有安装应该是类似如下的回显。


[plain] view plaincopyprint?
01.abbuggy@abbuggy-ubuntu:~$ dpkg -s curl 
02.系统没有安装软件包 curl,因而没有相关的信息。 
03.使用 dpkg --info (= dpkg-deb --info) 来检测打包好的文件, 
04.还可以通过 dpkg --contents (= dpkg-deb --contents) 来列出它们的内容。 
abbuggy@abbuggy-ubuntu:~$ dpkg -s curl
系统没有安装软件包 curl,因而没有相关的信息。
使用 dpkg --info (= dpkg-deb --info) 来检测打包好的文件,
还可以通过 dpkg --contents (= dpkg-deb --contents) 来列出它们的内容。

那么安装就行了sudo apt-get install curl


安装rvm
用rvm官方推荐的方式安装curl -L get.rvm.io | bash -s stable

回显提示我们,RVM被安装在$HOME/.vrm中;并且需要在终端中加载脚本$HOME/.rvm/scripts/rvm


[plain] view plaincopyprint?
01.abbuggy@abbuggy-ubuntu:~$ curl -L get.rvm.io | bash -s stable 
02.  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current 
03.                                 Dload  Upload   Total   Spent    Left  Speed 
04.100   185  100   185    0     0    144      0  0:00:01  0:00:01 --:--:--   906 
05.100 10235  100 10235    0     0   3929      0  0:00:02  0:00:02 --:--:-- 10888 
06.Downloading RVM from wayneeseguin branch stable 
07.  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current 
08.                                 Dload  Upload   Total   Spent    Left  Speed 
09.100   131  100   131    0     0     62      0  0:00:02  0:00:02 --:--:--   143 
10.100 1124k  100 1124k    0     0   127k      0  0:00:08  0:00:08 --:--:--  269k 
11. 
12.Installing RVM to /home/abbuggy/.rvm/ 
13.    RVM PATH line found in /home/abbuggy/.bashrc /home/abbuggy/.zshrc. 
14.    RVM sourcing line found in /home/abbuggy/.bash_profile /home/abbuggy/.zprofile. 
15. 
16.# RVM:  Shell scripts enabling management of multiple ruby environments. 
17.# RTFM: https://rvm.io/ 
18.# HELP: http://webchat.freenode.net/?channels=rvm (#rvm on irc.freenode.net) 
19.# Cheatsheet: http://cheat.errtheblog.com/s/rvm/ 
20.# Screencast: http://screencasts.org/episodes/how-to-use-rvm 
21. 
22.# In case of any issues read output of 'rvm requirements' and/or 'rvm notes' 
23. 
24.Installation of RVM in /home/abbuggy/.rvm/ is almost complete: 
25. 
26.  * To start using RVM you need to run `source /home/abbuggy/.rvm/scripts/rvm` 
27.    in all your open shell windows, in rare cases you need to reopen all shell windows. 
28. 
29.# abbuggy, 
30.# 
31.#   Thank you for using RVM! 
32.#   I sincerely hope that RVM helps to make your life easier and more enjoyable!!! 
33.# 
34.# ~Wayne 
abbuggy@abbuggy-ubuntu:~$ curl -L get.rvm.io | bash -s stable
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   185  100   185    0     0    144      0  0:00:01  0:00:01 --:--:--   906
100 10235  100 10235    0     0   3929      0  0:00:02  0:00:02 --:--:-- 10888
Downloading RVM from wayneeseguin branch stable
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   131  100   131    0     0     62      0  0:00:02  0:00:02 --:--:--   143
100 1124k  100 1124k    0     0   127k      0  0:00:08  0:00:08 --:--:--  269k

Installing RVM to /home/abbuggy/.rvm/
    RVM PATH line found in /home/abbuggy/.bashrc /home/abbuggy/.zshrc.
    RVM sourcing line found in /home/abbuggy/.bash_profile /home/abbuggy/.zprofile.

# 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 /home/abbuggy/.rvm/ is almost complete:

  * To start using RVM you need to run `source /home/abbuggy/.rvm/scripts/rvm`
    in all your open shell windows, in rare cases you need to reopen all shell windows.

# abbuggy,
#
#   Thank you for using RVM!
#   I sincerely hope that RVM helps to make your life easier and more enjoyable!!!
#
# ~Wayne
应该请把这句话加在$HOME/.bash_profile文件中,以便在开启一个终端会话时候加载RVM


[plain] view plaincopyprint?
01.[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm" # This loads RVM into a shell session. 
[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm" # This loads RVM into a shell session.
如果对shell变成不熟悉,这里简单解释一下这是干什么用的。

[[condition]],两层的方括号中间括着条件返回条件是不是真。-s是判断给定的文件是否存在的命令。这样一来,不就是在[[ -s "$HOME/.vrm/scripts/vrm"]]判断刚才安装的RVM是否存在吗?

接下来的&&符号是“短路的与”,当前面的条件是真的时候,执行后面的语句,返回这两个语句是不是全是真。在这里,利用了“短路”特性。也就是说当RVM已经安装的话,执行后面的. "$HOME/.rvm/scripts/rvm"命令。这条命令和source "$HOME/.rvm/scripts/rvm"是一个意思:加载rvm的启动脚本。

#符号后面是注释信息。

使用RVM
刚才我们通过修改.bash_profile增加的内容需要重新打开终端窗口时加载。我们有两个选择,一个是关闭当前的终端窗口重新打开,另一个是在当前窗口执行一遍. "$HOME/.rvm/scripts/rvm"。

察看RVM的版本


[plain] view plaincopyprint?
01.abbuggy@abbuggy-ubuntu:~$ rvm -v 
02. 
03.rvm 1.16.20 (stable) by Wayne E. Seguin , Michal Papis [https://rvm.io/] 
abbuggy@abbuggy-ubuntu:~$ rvm -v

rvm 1.16.20 (stable) by Wayne E. Seguin , Michal Papis [https://rvm.io/]

通过rvm requirements命令,可以察看安装各版本时候的前提条件。其中这句是需要关注的。这是安装依赖的第三方包,没有这个安装不成功岂不是很悲剧?



[plain] view plaincopyprint?
01.Additional Dependencies: 
02.# For Ruby / Ruby HEAD (MRI, Rubinius, & REE), install the following: 
03.  ruby: /usr/bin/apt-get install build-essential openssl libreadline6 libreadline6-dev curl git-core zlib1g zlib1g-dev libssl-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt-dev autoconf libc6-dev ncurses-dev automake libtool bison subversion pkg-config 
Additional Dependencies:
# For Ruby / Ruby HEAD (MRI, Rubinius, & REE), install the following:
  ruby: /usr/bin/apt-get install build-essential openssl libreadline6 libreadline6-dev curl git-core zlib1g zlib1g-dev libssl-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt-dev autoconf libc6-dev ncurses-dev automake libtool bison subversion pkg-config
这样一来,安装ruby的准备工作就完成了。察看当前RVM中已经安装的ruby版本,现在应该还没有。


[plain] view plaincopyprint?
01.abbuggy@abbuggy-ubuntu:~$ rvm list 
02. 
03.rvm rubies 
04. 
05.# No rvm rubies installed yet. Try 'rvm help install'. 
abbuggy@abbuggy-ubuntu:~$ rvm list

rvm rubies

# No rvm rubies installed yet. Try 'rvm help install'.

察看RVM可供安装的ruby版本


[plain] view plaincopyprint?
01.abbuggy@abbuggy-ubuntu:~$ rvm list known 
02.# MRI Rubies 
03.[ruby-]1.8.6[-p420] 
04.[ruby-]1.8.7-p370 
05.[ruby-]1.8.7[-p371] 
06.[ruby-]1.9.1[-p431] 
07.[ruby-]1.9.2-p180 
08.[ruby-]1.9.2-p290 
09.[ruby-]1.9.2-p318 
10.[ruby-]1.9.2[-p320] 
11.[ruby-]1.9.2-head 
12.[ruby-]1.9.3-preview1 
13.[ruby-]1.9.3-rc1 
14.[ruby-]1.9.3-p0 
15.[ruby-]1.9.3-p125 
16.[ruby-]1.9.3-p194 
17.[ruby-]1.9.3-p286 
18.[ruby-]1.9.3-[p327] 
19.[ruby-]1.9.3-head 
20.[ruby-]2.0.0-preview1 
21.ruby-head 
abbuggy@abbuggy-ubuntu:~$ rvm list known
# MRI Rubies
[ruby-]1.8.6[-p420]
[ruby-]1.8.7-p370
[ruby-]1.8.7[-p371]
[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-p286
[ruby-]1.9.3-[p327]
[ruby-]1.9.3-head
[ruby-]2.0.0-preview1
ruby-head

安装ruby 1.9.3-head,在不发生歧义的情况下方括号内的东西可以不必敲。


[plain] view plaincopyprint?
01.abbuggy@abbuggy-ubuntu:~$ rvm install 1.9.3-head 
abbuggy@abbuggy-ubuntu:~$ rvm install 1.9.3-head

之后等呀等呀自动安装了1.8.7和1.9.3


[plain] view plaincopyprint?
01.abbuggy@abbuggy-ubuntu:~$ rvm list 
02. 
03.rvm rubies 
04. 
05.   ruby-1.8.7-p371 [ i686 ] 
06.   ruby-1.9.3-head [ i686 ] 
07. 
08.# Default ruby not set. Try 'rvm alias create default '. 
abbuggy@abbuggy-ubuntu:~$ rvm list

rvm rubies

   ruby-1.8.7-p371 [ i686 ]
   ruby-1.9.3-head [ i686 ]

# Default ruby not set. Try 'rvm alias create default '.
选择1.9.3作为当前的使用版本,并且设置为缺省


[plain] view plaincopyprint?
01.abbuggy@abbuggy-ubuntu:~$ rvm use ruby-1.9.3-head --default 
02.Using /home/abbuggy/.rvm/gems/ruby-1.9.3-head 
abbuggy@abbuggy-ubuntu:~$ rvm use ruby-1.9.3-head --default
Using /home/abbuggy/.rvm/gems/ruby-1.9.3-head

设置好之后察看ruby版本


[plain] view plaincopyprint?
01.abbuggy@abbuggy-ubuntu:~$ ruby -v 
02.ruby 1.9.3p327 (2012-11-10) [i686-linux] 
abbuggy@abbuggy-ubuntu:~$ ruby -v
ruby 1.9.3p327 (2012-11-10) [i686-linux]
察看ruby的路径,就是RVM帮我们安装的


[plain] view plaincopyprint?
01.abbuggy@abbuggy-ubuntu:~$ which ruby 
02./home/abbuggy/.rvm/rubies/ruby-1.9.3-head/bin/ruby 
abbuggy@abbuggy-ubuntu:~$ which ruby
/home/abbuggy/.rvm/rubies/ruby-1.9.3-head/bin/ruby

短路RVM

刚才是用RVM进行ruby版本管理,当然了通过其他渠道例如apt-get也可以安装ruby,可以对RVM设置短路以便使用系统默认的ruby版本。


[plain] view plaincopyprint?
01.abbuggy@abbuggy-ubuntu:~$ rvm use system 
02.Now using system ruby. 
03.abbuggy@abbuggy-ubuntu:~$ ruby -v 
04.ruby 1.8.7 (2011-06-30 patchlevel 352) [i686-linux] 
05.abbuggy@abbuggy-ubuntu:~$ which ruby 
06./usr/bin/ruby 
abbuggy@abbuggy-ubuntu:~$ rvm use system
Now using system ruby.
abbuggy@abbuggy-ubuntu:~$ ruby -v
ruby 1.8.7 (2011-06-30 patchlevel 352) [i686-linux]
abbuggy@abbuggy-ubuntu:~$ which ruby
/usr/bin/ruby

卸载RVM

不想玩了,我要卸载。这个命令会移除$HOME/.rvm目录下面的所有东西即RVM管理的版本们。


[plain] view plaincopyprint?
01.rvm implode 

你可能感兴趣的:(ruby,ubuntu)