rbenvで複数バージョンのRubyを使う

rbenvを使ってruby 1.9.3-rc1、2.0.0-p247、2.1.1を使い分けられるようにします。

■rbenvのインストール

rbenv本体をインストール

$ git clone https://github.com/sstephenson/rbenv.git ~/.rbenv 

PATHの設定

$ vim ~/.bash_profile

# rbenv

export PATH=$PATH:$HOME/.rbenv/bin

eval "$(rbenv init -)"

 

確認

$ type rbenv

rbenv is a function

ruby-buildプラグインをインストール 

$ git clone https://github.com/sstephenson/ruby-build.git  ~/.rbenv/plugins/ruby-build

$ cd ~/.rbenv/plugins/ruby-build/
$ sudo ./install.sh

インストールしないとrbenv installコマンドが使えません。

$ rbenv install
rbenv: no such command `install'

■インストール

2.1.1のインストール

$ rbenv install 2.1.1

すんなり入りました。

 1.9.3のインストール

rbenv install 1.9.3-rc

ossl_pkey_ec.c:761: 警告: assignment makes pointer from integer without a cast

ossl_pkey_ec.c:815: error: ‘EC_GROUP_new_curve_GF2m’ undeclared (first use in this function)

ossl_pkey_ec.c:815: error: (Each undeclared identifier is reported only once

ossl_pkey_ec.c:815: error: for each function it appears in.)

 

opensslのエラーでインストールできないらしいので、パッチを使ってインストールします。

このバージョンで修正されているらしい

$ curl -fsSL https://bugs.ruby-lang.org/projects/ruby-trunk/repository/revisions/41808/diff?format=diff > 41808.patch

 
ChangeLogのところを手動で削除します

$ vim 41808.patch

 出来上がったパッチファイル

 $ cat 41808.patch

===================================================================
--- ext/openssl/ossl_pkey_ec.c  (revision 41807)
+++ ext/openssl/ossl_pkey_ec.c  (revision 41808)
@@ -762,8 +762,10 @@
                 method = EC_GFp_mont_method();
             } else if (id == s_GFp_nist) {
                 method = EC_GFp_nist_method();
+#if !defined(OPENSSL_NO_EC2M)
             } else if (id == s_GF2m_simple) {
                 method = EC_GF2m_simple_method();
+#endif
             }
 
             if (method) {
@@ -817,8 +819,10 @@
 
             if (id == s_GFp) {
                 new_curve = EC_GROUP_new_curve_GFp;
+#if !defined(OPENSSL_NO_EC2M)
             } else if (id == s_GF2m) {
                 new_curve = EC_GROUP_new_curve_GF2m;
+#endif
             } else {
                 ossl_raise(rb_eArgError, "unknown symbol, must be :GFp or :GF2m");
             }
Index: test/openssl/test_pkey_ec.rb
===================================================================
--- test/openssl/test_pkey_ec.rb        (revision 41807)
+++ test/openssl/test_pkey_ec.rb        (revision 41808)
@@ -7,28 +7,28 @@
     @data1 = 'foo'
     @data2 = 'bar' * 1000 # data too long for DSA sig
 
-    @group1 = OpenSSL::PKey::EC::Group.new('secp112r1')
-    @group2 = OpenSSL::PKey::EC::Group.new('sect163k1')
-    @group3 = OpenSSL::PKey::EC::Group.new('prime256v1')
+    @groups =
+    @keys =
 
-    @key1 = OpenSSL::PKey::EC.new
-    @key1.group = @group1
-    @key1.generate_key
+    OpenSSL::PKey::EC.builtin_curves.each do |curve, comment|
+      group = OpenSSL::PKey::EC::Group.new(curve)
 
-    @key2 = OpenSSL::PKey::EC.new(@group2.curve_name)
-    @key2.generate_key
+      key = OpenSSL::PKey::EC.new(group)
+      key.generate_key
 
-    @key3 = OpenSSL::PKey::EC.new(@group3)
-    @key3.generate_key
-
-    @groups = [@group1, @group2, @group3]
-    @keys = [@key1, @key2, @key3]
+      @groups << group
+      @keys << key
+    end
   end
 
   def compare_keys(k1, k2)
     assert_equal(k1.to_pem, k2.to_pem)
   end
 
+  def test_builtin_curves
+    assert(!OpenSSL::PKey::EC.builtin_curves.empty?)
+  end
+
   def test_curve_names
     @groups.each_with_index do |group, idx|
       key = @keys[idx]

 パッチを使ってインストール

 cat 41808.patch | rbenv install --patch 1.9.3-rc1

今度はインストールできました。

 

エラーが出る場合はyumで該当のものを入れるとよさげです。

/home/kashifuji/.rbenv/plugins/ruby-build/bin/ruby-build: line 849: patch: コマンドが見つかりません

$ sudo yum install patch
$ sudo yum install svn
$ sudo yum install autoconf
$ sudo yum install byacc
$ sudo yum install sqlite-devel
$ sudo yum install zlib-devel
$ sudo yum install openssl-devel
$ sudo yum install libyaml-devel
$ sudo yum install ImageMagick-devel
$ sudo yum install mysql-devel

ほかのバージョンも同じ手順でインストール

$ cat 41808.patch | rbenv install --patch 2.0.0-p247
$ cat 41808.patch | rbenv install --patch 1.9.3-p448

rbenvでrubyを切り替える

 

バージョンの確認
$ rbenv versions
* system (set by /home/kashifuji/.rbenv/version)
  1.9.3-p448
  1.9.3-rc1
  2.0.0-p247
  2.1.1
 
バージョンの切り替え
$ rbenv local 1.9.3-rc1
$ ruby  -v
ruby 1.9.3dev (2011-09-23 revision 33323) [x86_64-linux]

$ rbenv local 2.0.0-p247
$ ruby  -v
ruby 2.0.0p247 (2013-06-27 revision 41674) [x86_64-linux]
$ rbenv local 2.1.1
$ ruby -v
ruby 2.1.1p76 (2014-02-24 revision 45161) [x86_64-linux]
 
$ rbenv  local --unset
$ ruby -v
ruby 1.9.3p545 (2014-02-24 revision 45159) [x86_64-linux]
 

トラブルシューティング

1.rbenv install 2.1.1でインストール時にMissing the OpenSSL lib?と出た場合
$ rbenv install 2.1.1
BUILD FAILED
 
Inspect or clean up the working tree at /tmp/ruby-build.20140511065604.7388
Results logged to /tmp/ruby-build.20140511065604.7388.log
 
Last 10 log lines:
                              io-console 0.4.2
                              json 1.8.1
                              minitest 4.7.5
                              psych 2.0.3
                              rake 10.1.0
                              rdoc 4.1.0
                              test-unit 2.1.1.0
installing rdoc:              /home/kashifuji/.rbenv/versions/2.1.1/share/ri/2.1.0/system
installing capi-docs:         /home/kashifuji/.rbenv/versions/2.1.1/share/doc/ruby
The Ruby openssl extension was not compiled. Missing the OpenSSL lib?

→  openssl-develをインストールするといいです。

$ sudo yum install openssl-devel

2. pachをつけて実行したときにエラーになった場合

$ cat 41808.patch | rbenv install --patch 1.9.3-rc1

Installing ruby-1.9.3-rc1...

 

/home/kashifuji/.rbenv/plugins/ruby-build/bin/ruby-build: line 852: patch: command not found

→ pachコマンドをインストールします

$ sudo yum install patch

参考資料