at posts/single.html

Unicorn環境でRubyを1.9.3-p194へアップデート

この日記を動かしているサーバで、Rubyを1.9.3-p194へアップデートした。今後のために手順を忘れないようにメモしておこう。

現在の環境

$ ruby --version
ruby 1.9.3p125 (2012-02-16 revision 34643) [x86_64-linux]
$ rbenv versions
* 1.9.3-p125 (set by /home/machu/.rbenv/version)
$ unicorn --version
unicorn v4.3.1

ruby-buildとrbenvの更新

gitで管理しているので、いずれも git pull で更新する。 ruby-build はスタンドアロンで導入しているので install.sh も実行する。本当は .rbenv/plugins に入れておいた方が更新が楽。

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

ruby 1.9.3-p194のインストール

-l オプションを付けないと一覧が出ないようになった。

$ rbenv install -l
Available versions:
  1.8.6-p383
  1.9.3-p125
  1.9.3-p194
$ rbenv install 1.9.3-p194

ruby 1.9.3-p194への切り替え

いきなり global のバージョンを変えるのは怖かったので、まずは shell のみを切り替え。

$ rbenv versions
* 1.9.3-p125 (set by /home/machu/.rbenv/version)
  1.9.3-p194
$ rbenv shell 1.9.3-p194
$ rbenv versions
  1.9.3-p125
* 1.9.3-p194 (set by RBENV_VERSION environment variable)

依存ライブラリのインストール

tDiaryのGemfileで指定している依存ライブラリを、bundlerを使ってインストールする。bundler自身はgemでインストールする必要あり。

$ gem install bundler
$ rbenv rehash
$ cd ~/var/tdiary/tdiary-core
$ bundle install
$ rbenv rehash

unicornの再起動

ちゃんと新しいunicornに変わっていることを確認。

$ rbenv which unicorn
/home/machu/.rbenv/versions/1.9.3-p194/bin/unicorn

unicorn は SIGUSR2 での再起動をサポートしているけど、 ruby のバージョンを変える場合は停止・再起動が必要だった。tDiaryはproduction環境で起動している(テスト・開発用のgemをロードさせたくないので)。

$ kill -QUIT `cat unicorn.pid`
$ RACK_ENV=production unicorn -c unicorn.conf -D
$ ps aux | grep unicorn
machu    31397  4.8  3.6 736740 36712 ?        Sl   06:24   0:01 unicorn master -c unicorn.conf -D                                                                                    
machu    31401  6.7  4.5 759176 46124 ?        Sl   06:24   0:01 unicorn worker[0] -c unicorn.conf -D                                                                                 
machu    31404  0.4  3.2 736740 33404 ?        Sl   06:24   0:00 unicorn worker[1] -c unicorn.conf -D                                                                                 
machu    31548  0.0  0.0  10376   928 pts/6    S+   06:24   0:00 grep unicorn

global での ruby バージョンの切り替え

日記が ruby 1.9.3-p194 で動いていることが確認できたら、 global 環境の ruby バージョンも切り替えておしまい。

$ rbenv global 1.9.3-p194

まとめ

昔のような手動コンパイルとgemコマンドでのライブラリ更新に比べたら、rbenv + bundler 環境のおかげで楽になったものだ。

参考

関連する日記