5
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

rvmな人が流行にのっかってrbenv使って、ついでにChefったときにひかかったんで、

Last updated at Posted at 2014-02-02

サーバ環境をChefってる途中なんですけど、どうせならついでにrvmからrbenvに乗り換えようかなって思ってですね、ためしてみましたよ。

訂正

同じような内容をもっと詳しく書いて下さっている記事がありました。
もっと早く見つけてれば、、、涙
今日作りたくなる簡単cookbook [Kim'sキッチン] rbenv at 2013-04-18

リファレンス

  1. お手軽環境構築 !chefでrubyのバージョンを2.0.0-p195にする方法 ~CentOS~ at 2013-07-04
  2. Chef Soloでrubyのバージョンを2.0.0-p195にする方法 ~Debian、Ubuntu編~ at 2014-01-14

やってみた(未読スルー可)

リファレンスの記事を読んだりしながらやってみました。

まず、1.の記事通りやったら、rbenv install 2.1.0で「rbenvが無いよ」っておこられる。
で、2.の記事にフルパスにすりゃいいよってあったので、フルパス書いたら今度はインストールはできるんだけど、ゲストOSにsshで入ってruby -vしてみるとsystemの1.8.7とかが使われたままで、rbenv versionsしても2.1.0が表示されないーー

なんで。。。

調べる(未読スルー可)

「ruby本体はどこにいったの?」って調べてみたら、/root/.rbenv/versions/2.1.0/rubyにいる訳で。
で、初心者にはこれで合ってるのかわからなくて、レシピを読み直してみた。
どうやらシステム全体にインストールしようとしていて、/usr/local/rbenv/versionsに入れようとしていたらしいです、笑
たぶん、ルートの下に入ってしまったのはルートでインストールしてるからで、インストール先のパスが通ってないんだろうなって推測。
レシピ見ると確かに、templateで作った環境変数が反映されていない状態でrbenvを使ってるっぽい。
ってことでこんな感じになりました。

最終的なレシピ

  • source /etc/profile.d/rbenv.shで環境変数を毎回読み込み
  • 伊藤本がbashだったので、executeからbashにしました(ヒアドキュメント使えていいし)
  • rubyのバージョンはnode.jsonで設定

このあたりがポイントかなー

git "/usr/local/rbenv" do
  repository "git://github.com/sstephenson/rbenv.git"
  reference "master"
  action :sync
end

# 各バージョンのrubyはversions/以下におかれる
# 使用中のrubyがshims/以下におかれる
%w{/usr/local/rbenv/shims /usr/local/rbenv/versions}.each do |dir|
  directory dir do
    action :create
  end
end

# rbenv自体にはrubyのインストール機能は含まれないんだってさ
git "/usr/local/ruby-build" do
  repository "git://github.com/sstephenson/ruby-build.git"
  reference "master"
  action :sync
end

bash "install_ruby_build" do
  not_if 'which ruby-build'
  cwd "/usr/local/ruby-build"
  code <<-EOH
    ./install.sh
  EOH
end

# 環境変数の設定
template "rbenv.sh" do
  path "/etc/profile.d/rbenv.sh"
  owner "root"
  group "root"
  mode "0644"
  source "rbenv.sh.erb"
end

# rubyのインストールに必要な物を入れる
# EPEL必須
%w{make gcc zlib-devel openssl-devel readline-devel ncurses-devel gdbm-devel db4-devel libffi-devel tk-devel libyaml-devel}.each do |pkg|
  yum_package pkg do
    action :install
  end
end

# rubyインストール(ビルドに数10分かかる、、、)
# 【注意】この段階では環境変数が適用されていないので
# rbenv.shを読み込むことで環境変数を適用
bash "install ruby (This may take several tens of minutes...)" do
  code <<-EOC
    source /etc/profile.d/rbenv.sh
    rbenv install #{node['ruby']['version']}
  EOC
  # ここにrubyの本体がいる
  creates "/usr/local/rbenv/versions/#{node['ruby']['version']}"
end

bash "ruby change to #{node['ruby']['version']}" do
  # 「設定されていたら何もしない」を不細工に表現w
  not_if "source /etc/profile.d/rbenv.sh; rbenv versions | grep '* #{node.ruby.version}'"
  code <<-EOC
    source /etc/profile.d/rbenv.sh
    rbenv global #{node['ruby']['version']}
    rbenv rehash
  EOC
end

素人丸出し記事ですみません笑
Qiitaってこんなんでいいのかな。。(2回目の投稿)

あと、nvm版のレシピも作ったので明日時間があったらあげますー

5
5
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
5
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?