LoginSignup
20
20

More than 1 year has passed since last update.

Chefを新しくしてrbenvをberkshellでvagrantのUbuntuにインストール

Last updated at Posted at 2013-07-11

はじめに

前回からの続きです.

前回の設定のままではchefのバージョンの古いもの(10.14.2)がvagrantから内部的に起動されました.これではrbenvがインストールできませんでした.また,関連するライブラリをインストールする前にapt-getupdateをするための設定を追加します.

vagrantにプラグインを追加

chefの最新版(Chef11.4.4)を利用できるようにするために,vagrantにもうひとつプラグインを追加します.

$ vagrant plugin install vagrant-omnibus
Installing the 'vagrant-omnibus' plugin.  This can take a few minutes...
Installed the plugin 'vagrant-omnibus (1.1.0)'!

現在のvagrant pulginの状況は次のとおりです.

$ vagrant plugin list
vagrant-berkshelf (1.3.2)
vagrant-omnibus (1.1.0)

chefの最新版を指定する

Vagrantfileでchef_versionを指定します.変更の差分は次のとおりです.

diff --git a/Vagrantfile b/Vagrantfile
index dbb240a..e3a5d5d 100644
--- a/Vagrantfile
+++ b/Vagrantfile
@@ -61,6 +61,7 @@ Vagrant.configure("2") do |config|
   # Enabling the Berkshelf plugin. To enable this globally, add this configuration
   # option to your ~/.vagrant.d/Vagrantfile file
   config.berkshelf.enabled = true
+  config.omnibus.chef_version = :latest

   # An array of symbols representing groups of cookbook described in the Vagrantfile
   # to exclusively install and copy to Vagrant's shelf.

一行追加しただけですね.

Berksfileの編集

ここではaptのcookbookを指定します.これは,apt-getupdateを実行させるためのものです.rbenvのインストールに必要な新しいバージョンのパッケージをインストールできるようにするためです.残りは,ruby_buildとrbenv関連の設定になります.Berksfileの差分は次のとおりです.

diff --git a/Berksfile b/Berksfile
index 6d86dcf..79cace7 100644
--- a/Berksfile
+++ b/Berksfile
@@ -1,4 +1,7 @@
 site :opscode

 metadata
+cookbook 'apt'
 cookbook 'git'
+cookbook 'ruby_build'
+cookbook 'rbenv', github: "fnichol/chef-rbenv"

Vagrantfileの修正

Vagrantfileも修正します.chef.run_listに,apt::defaultレシピを追加しました.これにより,apt-getupdateが実行されます.加えて,rbenvの設定も追加します.

diff --git a/Vagrantfile b/Vagrantfile
index e3a5d5d..ec6caf8 100644
--- a/Vagrantfile
+++ b/Vagrantfile
@@ -77,12 +77,27 @@ Vagrant.configure("2") do |config|
         :server_root_password => 'rootpass',
         :server_debian_password => 'debpass',
         :server_repl_password => 'replpass'
+      },
+      rbenv: {
+        user_installs: [{
+          user: "vagrant",
+          rubies: ["2.0.0-p195"],
+          global: "2.0.0-p195",
+          gems: {
+            "2.0.0-p195" => [
+              {name: "bundler"}
+            ]
+          }
+        }]
       }
     }

     chef.run_list = [
         "recipe[precise::default]",
-        "git"
+        "recipe[apt::default]",
+        "git",
+        "ruby_build",
+        "rbenv::user"
     ]
   end
 end

vagrantでprovisionを実行

$ vagrant provision

ずらーっとログが出て,rubyのコンパイルで相当待たされます.手元の環境で1430.25秒かかりました.

rubyの動作確認

でも,完了するとrbenv経由でrubyが利用できるようになります.

$ vagrant ssh
vagrant@precise:~$ rbenv versions
  system
* 2.0.0-p195 (set by /home/vagrant/.rbenv/version)
vagrant@precise:~$ ruby -v
ruby 2.0.0p195 (2013-05-14 revision 40734) [i686-linux]

きちんとrubyが動作しています.

おわりに

chefのバージョン違いの解消とapt-getupdateを実行させることの必要性に気づくために手間取りましたが,それ以外の設定は楽勝でした.びっくりですね.

次回は,berksコマンドで作成したCookbookの中身についてです.

参考文献


  • source /mnt/c/Users/yc/Dropbox/org/qiita/befe8ab4cd0765643628.org
20
20
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
20
20