Vagrantで構築したvm上でRailsアプリケーションをセットアップする時のbundle installがエラーな話
最近Rails触ってます。
と言ってもコーディングではなく環境構築周りですが。
Rubyの知識が浅いためかハマってしまったので、タイトルの件でメモ書きしておきます。
既存のプロジェクトがChefを使ってプロビジョニングを行っていたので、
少し時代に遅れてる感も抱きつつ、Vagrant+Chef Solo+Berkshelfでローカル開発環境を構築しようと思い、
試行錯誤しながらひと通りの設定を終えようとしていたのですが
vagrant up
一発でもろもろセットアップ完了して気持ちいいーと思っていたところ
vm上でRailsアプリケーションを動かすためにbundle install --path=vendor/bundle
を実行したら
Gem::Ext::BuildError: ERROR: Failed to build gem native extension. /opt/rbenv/versions/2.1.2/bin/ruby extconf.rb checking for ruby/util.h... *** extconf.rb failed *** Could not create Makefile due to some reason, probably lack of necessary libraries and/or headers. Check the mkmf.log file for more details. You may need configuration options. Provided configuration options: --with-opt-dir --without-opt-dir --with-opt-include --without-opt-include=${opt-dir}/include --with-opt-lib --without-opt-lib=${opt-dir}/lib --with-make-prog --without-make-prog --srcdir=. --curdir --ruby=/opt/rbenv/versions/2.1.2/bin/ruby /opt/rbenv/versions/2.1.2/lib/ruby/2.1.0/mkmf.rb:456:in `try_do': The compiler failed to generate an executable file. (RuntimeError) You have to install development tools first. from /opt/rbenv/versions/2.1.2/lib/ruby/2.1.0/mkmf.rb:587:in `try_cpp' from /opt/rbenv/versions/2.1.2/lib/ruby/2.1.0/mkmf.rb:1067:in `block in have_header' from /opt/rbenv/versions/2.1.2/lib/ruby/2.1.0/mkmf.rb:918:in `block in checking_for' from /opt/rbenv/versions/2.1.2/lib/ruby/2.1.0/mkmf.rb:351:in `block (2 levels) in postpone' from /opt/rbenv/versions/2.1.2/lib/ruby/2.1.0/mkmf.rb:321:in `open' from /opt/rbenv/versions/2.1.2/lib/ruby/2.1.0/mkmf.rb:351:in `block in postpone' from /opt/rbenv/versions/2.1.2/lib/ruby/2.1.0/mkmf.rb:321:in `open' from /opt/rbenv/versions/2.1.2/lib/ruby/2.1.0/mkmf.rb:347:in `postpone' from /opt/rbenv/versions/2.1.2/lib/ruby/2.1.0/mkmf.rb:917:in `checking_for' from /opt/rbenv/versions/2.1.2/lib/ruby/2.1.0/mkmf.rb:1066:in `have_header' from extconf.rb:14:in `<main>' extconf failed, exit code 1 Gem files will remain installed in /project_name/vendor/bundle/ruby/2.1.0/gems/bcrypt-3.1.7 for inspection. Results logged to /project_name/vendor/bundle/ruby/2.1.0/extensions/x86_64-linux/2.1.0-static/bcrypt-3.1.7/gem_make.out An error occurred while installing bcrypt (3.1.7), and Bundler cannot continue. Make sure that `gem install bcrypt -v '3.1.7'` succeeds before bundling.
上記のようなエラーにハマりました。
ひとまずbcryptのgemの指定バージョンを変えてみると、うまく通るようになるのですが
また別のunf_extのgemのインストールとかでも同じようにこけるんですよね。
いくつかのgemの指定バージョンをいじいじしてもうまくいかず、
Chef側のrbenvのインストールがなんかおかしいのかとかそっちまで遡ってわからNEEEEってなっていたのですが
結局原因はもっとシンプルなところにありました。
Vagrantfileに
config.vm.synced_folder "project_name", "/project_name"
って書いていて、vagrant ssh
したあとcd /project_name
してbundle install --path=vendor/bundle
していたのですが
どうやらsynced_folderでホストOSとゲストOSでの同期をしているディレクトリ内にbundle install
しようとすると
上記のようなエラーにハマるようです。。
対処法の例としては3つ、
- synced_folderの配下ではなく別の場所にプロジェクトをコピーしてから
bundle install
するパターン- ぱっと見は簡単ですが2重で同期しなきゃいけないので面倒かもしれない
同期方法をrsyncにしてvendor/bundleを除外するパターン。例えば下記の感じ
config.vm.synced_folder "project_name", "/var/www/project_name", type: "rsync", rsync__exclude: [ ".git/", "vendor/bundle" ], rsync__args: ["--verbose", "--archive", "--delete", "-z"]
bundle install --path=~/project_name_gems/vendor/bundle
のようにインストール先のパスを同期ディレクトリではないところに指定するパターン- プロジェクトと同じ場所に無いのがちょっと気持ち悪い
がワークアラウンドな方法としてあげられます。
根本解決にはなっていないかもしれませんが。。
もっと良い方法はないものか。