New FAMILUG

The PyMiers

Friday 9 November 2012

Cài đặt và quản lý ruby qua rvm


RVM - Ruby Version Manager. Để cài đặt và quản lý được nhiều version của ruby trên máy tính.
Tại sao rvm -> Without RVM, it’s pretty difficult to have more than one version of Ruby on your computer - tutsplus.com

1. Cài đặt

$ sudo apt-get install build-essential git-core curl
Cho các máy chưa có các gói này

$ curl -L https://get.rvm.io | bash -s

Để cài rvm, yêu cầu máy đã có sẵn gói curl

echo '[[ -s "/home/lamdt/.rvm/scripts/rvm" ]] && source "/home/lamdt/.rvm/scripts/rvm"' >> ~/.bashrc

Để thêm dòng [[ -s "/home/lamdt/.rvm/scripts/rvm" ]] && source "/home/lamdt/.rvm/scripts/rvm" vào file ~/.bashrc (làm tay cũng được).
Lưu ý: Thay lamdt bằng tên user.


$ rvm requirements

Để cài các gói rvm yêu cầu cho linux

$ rvm list known
Để liệt kê các version của ruby có thể cài




# 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-head
[ruby-]2.0.0-preview1
ruby-head

rvm install 1.9.3
để cài bản mới nhất 1.9.3
console sẽ chạy @#$%@$ gì đó
đến khi
Install of ruby-1.9.3-p286 - #complete
Tức là thành công. Ta có thể thử
$ ruby -v
ruby 1.9.3p286 (2012-10-12 revision 37165) [x86_64-linux]

Có thể cài thêm các version khác dùng rvm install


2. Quản lý các version
Để cài các version ruby khác
rvm install version_name

Để set 1 version làm mặc định
rvm use --default version_name
Eg:
$ rvm use --default 1.9.3
Using /home/lamdt/.rvm/gems/ruby-1.9.3-p286

3. Cài gems

rvm version_name do gem install gem_name

Eg:
$ rvm 1.9.3 do gem install mysql
/home/lamdt/.rvm/rubies/ruby-1.9.3-p286/lib/ruby/1.9.1/yaml.rb:56:in `<top (required)>':
It seems your ruby installation is missing psych (for YAML output).
To eliminate this warning, please install libyaml and reinstall your ruby.
ERROR:  Loading command: install (LoadError)
    cannot load such file -- zlib
ERROR:  While executing gem ... (NameError)
    uninitialized constant Gem::Commands::InstallCommand

Ồ, vậy là có lỗi. Mình sẽ giải quyết từng lỗi một

* It seems your ruby installation is missing psych (for YAML output).
Thiếu package "psych"
Mình cài thêm nó bằng:
$ sudo apt-get install libyaml-dev

* ERROR:  Loading command: install (LoadError)
    cannot load such file -- zlib

zlib là thằng thổ tả nào?
$ sudo apt-get install zlib1g-dev
Cho nó chết.

Ta cài lại gem mysql

$ rvm 1.9.3 do gem install mysql
Fetching: mysql-2.8.1.gem (100%)
Building native extensions.  This could take a while...
ERROR:  Error installing mysql:
    ERROR: Failed to build gem native extension.

        /home/lamdt/.rvm/rubies/ruby-1.9.3-p286/bin/ruby extconf.rb
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lm... yes
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lz... yes
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lsocket... no
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lnsl... yes
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lmygcc... no
checking for mysql_query() in -lmysqlclient... no
*** extconf.rb failed ***

WTF, vẫn có lỗi. Bạn cứ từ từ. Bê đống lỗi này lên Google, xem có thằng nào gặp phải chưa.
.....
.....
....
..
.
$ sudo apt-get install libmysqlclient-dev

Thử lại lần nữa
$ rvm 1.9.3 do gem install mysql
Building native extensions.  This could take a while...
Successfully installed mysql-2.8.1
1 gem installed
Installing ri documentation for mysql-2.8.1...
Installing RDoc documentation for mysql-2.8.1...

Success rồi :D

Kết luận. Nội dung quan trọng nhất của bài này là gì?
Nếu gặp 1 lỗi nào đó, trong lập trình, hay phần cứng, hay OS báo lỗi. Nghĩ mãi không ra, hãy tra Google.
Done.
Nguồn Google

5 comments:

  1. mới xem dòng đầu thôi.
    Nhưng câu lệnh này quá tởm :))
    bash -s stable < <(curl -s https://raw.github.com/wayneeseguin/rvm/master/binscripts/rvm-installer)

    chưa bao giờ mình nhìn thấy luôn @@

    ReplyDelete
    Replies
    1. Đã thay:
      bash -s stable < <(curl -s https://raw.github.com/wayneeseguin/rvm/master/binscripts/rvm-installer)

      bằng:
      curl -L https://get.rvm.io | bash -s

      cho ngắn gọn

      Delete
    2. giờ thấy cái <(curl ....) cũng không hẳn là tởm lắm :))
      <(command) là 1 tính năng của bash cho phép tạo file tạm. Ví dụ lệnh diff cần sử dụng 2 file để so sánh, có thể dùng như sau:
      diff <(ls /tmp/yyy) <(ls /tmp/xxx)

      Delete
  2. ^ có nguồn chứ t tự nghĩ dell đâu.

    ReplyDelete
  3. Tại sao rvm -> Without RVM, it’s pretty difficult to have more than one version of Ruby on your computer - tutsplus.com

    theo t chỗ này cần giải thích tại sao cần nhiều version của ruby hơn là trả lời như bọn tutsplus.

    Python thì quá rõ ràng vì nó có 2.7 vs 3.x là khác nhau rất lớn

    ReplyDelete