LoginSignup
4
4

More than 5 years have passed since last update.

Vagrantfile内でdotenv経由で値を読み込めるようにする

Posted at

複数のプロジェクトに関わっていると開発環境をローカルにいれておくとなんだかごちゃごちゃしてくるのでvagrantを使うようにしています。

RailsやDBなどがはいっている環境をリポジトリにいれておいて、そのリポジトリをcloneして、プロジェクトに関するファイルを.envで記述するようにしてきます。

例えば、Railsのプロジェクトであれば下記を使います。

GitHubからcloneしてきます。

$ git clone git@github.com:hirotaka/rails4_box.git project_name_box

.envにソースがあるディレクトリなどプロジェクト固有の内容を記述します。

$ berks vendor
$ vagrant up

この手順でできるように、Vagrantfileでdotenvを使えるようにしたっかたのですが、若干はまったので手順をまとめておきたいと思います。

まず、gemでdotenvをいれるのですが、vagrantの場合は組込のrubyを使うのでgem installではなく、vagrant plugin installをしていれる必要があります。

$ vagrant plugin install dotenv
Installing the 'dotenv' plugin. This can take a few minutes...
Waiting for cleanup before exiting...Waiting for cleanup before exiting...

Waiting for cleanup before exiting...Waiting for cleanup before exiting...Waiting for cleanup before exiting...


Waiting for cleanup before exiting...
Bundler, the underlying system Vagrant uses to install plugins,
reported an error. The error is shown below. These errors are usually
caused by misconfigured plugin installations or transient network
issues. The error from Bundler is:

An error occurred while installing nokogiri (1.6.4.1), and Bundler cannot continue.
Make sure that `gem install nokogiri -v '1.6.4.1'` succeeds before bundling.

nokogiriでエラーが出てしまいました。

依存しているライブラリ類をいれます。

$ brew install libxml2 libxslt libiconv
$ brew link --force libxml2
$ brew link --force libxslt

環境変数を設定した上で、vagrant plugin installします。

$ export NOKOGIRI_USE_SYSTEM_LIBRARIES=true
$ vagrant plugin install nokogiri
$ vagrant plugin install dotenv
4
4
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
4
4