LoginSignup
19
18

More than 5 years have passed since last update.

サーバへのRubyインストールはrbenvよりRPMの方が簡単だった

Posted at

経緯

構成管理ツール(chefとかItamae等)でサーバ(CentOS)にRuby環境を整えようとしたんだけど、rbenvのリポジトリをcloneしなきゃいけなかったりgccやその他諸々の依存モジュールをインストールするレシピを書かなきゃいけなかったりとかなり面倒だった。
RPMで入れるとすこぶる楽だという記事をいくつか読んだので試してみた。

手順

specが公開されているリポジトリをforkする

specが公開されてるのでforkしてcloneする。hansode/ruby-2.1.x-rpm
forkしておく理由は、これを雛形にしてRubyの新しいバージョンのspecを書けたり、ビルドしたrpmをGitHub上でホスティングできるメリットがあるためである。
cloneしたら、VagrantでCentOS6環境(6.5)を構築する。
以下のようなディレクトリ構成になるはずだ。

.
├── README.md
├── Vagrantfile
└── ruby21x.spec

ruby21x.specを書き換える

筆者はRuby 2.1.5は不要で、Ruby 2.2.2をインストールしたいと思ったのでspecを書き換えた。
2.2.2なのでファイル名も変えた。

% git diff
diff --git a/ruby22x.spec b/ruby22x.spec
index 190672d..4d1d8d1 100644
--- a/ruby22x.spec
+++ b/ruby22x.spec
@@ -1,5 +1,5 @@
-%define rubyver         2.1.5
-%define rubyabi         2.1
+%define rubyver         2.2.2
+%define rubyabi         2.2

 Name:           ruby
 Version:        %{rubyver}
@@ -64,6 +64,9 @@ rm -rf $RPM_BUILD_ROOT
 %{_libdir}/*

 %changelog
+* Wed Jul 22 2015 Yukiyan <yukiyan@hogehoge.com> - 2.2.2
+- Update ruby version to 2.2.2
+
 * Tue Apr 14 2015 Johnson Earls <johnson.earls@oracle.com> - 2.1.5-2
 - Fix Obsoletes header lines to allow for ruby package updates

RPMをビルドする

$ vagrant ssh
$ mkdir -p ~/rpmbuild/{BUILD,BUILDROOT,RPMS,SOURCES,SPECS,SRPMS}
$ cd ~/rpmbuild/SOURCES && curl -LO http://cache.ruby-lang.org/pub/ruby/2.2/ruby-2.2.2.tar.gz
$ cp /vagrant/ruby22x.spec ~/rpmbuild/SPECS
$ sudo yum update -y
$ sudo yum install -y rpm-build
$ sudo yum install -y readline-devel ncurses-devel gdbm-devel glibc-devel gcc openssl-devel libyaml-devel libffi-devel zlib-devel tcl-devel db4-devel byacc
$ rpmbuild -ba ~/rpmbuild/SPECS/ruby22x.spec
書き込み完了: /home/vagrant/rpmbuild/SRPMS/ruby-2.2.2-2.el6.src.rpm
書き込み完了: /home/vagrant/rpmbuild/RPMS/x86_64/ruby-2.2.2-2.el6.x86_64.rpm

RPMをGitHubでReleaseする

一連の変更をコミットしてpushし、VMでビルドした各RPMをReleaseする。
GitHub上でのReleaseの方法は直感的で簡単にできるので、詳細は説明しないが詳しくは以下を参照すると良い。
GitHubのリリース機能を使う - Qiita
リリースできると、このようになる。

150722-0002.png

今回作成したRPMを使ってItamaeでRuby環境を構築する

Itamaeの使い方はすごくシンプルなので、以下の資料を読めばすぐ理解できるはず。

RubyをインストールしたいVMを準備してitamaeを実行する。
$ itamae ssh -h sample_ruby --vagrant --node-json node/node.json recipes/ruby/default.rb

recipes/ruby/default.rb
version  = '2.2.2'
ruby_rpm = "ruby-#{version}-2.el6.x86_64.rpm"

package "https://github.com/yukiyan/ruby-rpm/releases/download/#{version}/#{ruby_rpm}" do
  not_if "rpm -q ruby-#{version}"
end

gem_package 'bundler'
node/node.json
{
  "ruby": {
    "version": "2.2.2"
  }
}

spec/sample_ruby/ruby_spec.rb
require 'spec_helper'

describe package 'ruby' do
  it { should be_installed.with_version '2.2.2' }
end

describe package 'bundler' do
  it { should be_installed.by('gem') }
end

意識が高いのでちゃんとServerspecも書いた。
テストが通り無事、Ruby環境が整ったことがわかった :thumbsup:

% rake

Package "ruby"
  should be installed

Package "bundler"
  should be installed

Finished in 0.75034 seconds (files took 0.27158 seconds to load)
2 examples, 0 failures

参考資料

19
18
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
19
18