【Rails環境構築】bundle installでエラーになる時の対処法!
こんにちは。opiyoです。
- 昨日まで普通に動いていたのに
- 教科書、本と通りに設定しているはずなのに
- 何もしてないのに動かないぞー
って経験無いでしょうか?
僕も昨日新しくMacが手に入ったのでRails環境をローカルに構築して今作っているRailsアプリを動かそうとしたら...
2時間近くはまってしまいました。
元々使ってたMacと全く同じ設定のはずなのに何故動かないんだーって状況でした。
どうにかこうにか解決する事が出来たので、その方法について共有できればと思います。
エラーが出た時に「どうすれば良いか」が分からなくて諦めてしまう。ってのが結構あると思うのですが答えは必ずエラーの中に書かれている ので頑張ってGoogle翻訳使って一つずつ解決していくのが大事だと思いますので僕がどうやって進めていったのかのログです。
動かしたいRailsアプリについて
僕の場合は、こんな感じ。
$ ruby -v ruby 2.5.0p0 (2017-12-25 revision 61468) [x86_64-darwin15] $ rails -v Rails 5.1.4
bundle installができない
$ bundle install Gem::Ext::BuildError: ERROR: Failed to build gem native extension. current directory: /Users/tnakano/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/pg-0.21.0/ext /Users/tnakano/.rbenv/versions/2.5.0/bin/ruby -r ./siteconf20180125-28967-e942ot.rb extconf.rb checking for pg_config... no No pg_config... trying anyway. If building fails, please try again with --with-pg-config=/path/to/pg_config checking for libpq-fe.h... no Can't find the 'libpq-fe.h header *** 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=/Users/tnakano/.rbenv/versions/2.5.0/bin/$(RUBY_BASE_NAME) --with-pg --without-pg --enable-windows-cross --disable-windows-cross --with-pg-config --without-pg-config --with-pg_config --without-pg_config --with-pg-dir --without-pg-dir --with-pg-include --without-pg-include=${pg-dir}/include --with-pg-lib --without-pg-lib=${pg-dir}/lib To see why this extension failed to compile, please check the mkmf.log which can be found here: /Users/tnakano/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/extensions/x86_64-darwin-16/2.5.0-static/pg-0.21.0/mkmf.log extconf failed, exit code 1 Gem files will remain installed in /Users/tnakano/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/pg-0.21.0 for inspection. Results logged to /Users/tnakano/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/extensions/x86_64-darwin-16/2.5.0-static/pg-0.21.0/gem_make.out An error occurred while installing pg (0.21.0), and Bundler cannot continue. Make sure that `gem install pg -v '0.21.0'` succeeds before bundling. In Gemfile: pg
これだけ見ても何が何だか分からないですよね。 ですが諦めずに良く良く表示されたメッセージを見てください。
ポイントは最後辺りのMake sure that
gem install pg -v '0.21.0'succeeds before bundling.
ですね。
直訳するとバンドルの前に
gem install pg -v '0.21.0'が成功していることを確認してください。`になります。
じゃー次にgem pg
って何だってなると思いますので、これを調べてみるとPostgreSQL
にアクセスするために必要なことだと分かります。
「あっそもそもPostgreSQLインストールしてねーじゃん」ってことに僕は気づきましたので、PostgreSQL
をインストールします。
postgresqlをインストールする
$ brew install postgresql ==> Installing postgresql
PostgreSQL
がインストール出来たので、もっかいbundle install
実行!
$ bundle install Gem::Ext::BuildError: ERROR: Failed to build gem native extension. current directory: /Users/tnakano/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/rmagick-2.16.0/ext/RMagick /Users/tnakano/.rbenv/versions/2.5.0/bin/ruby -r ./siteconf20180125-42802-14ohnoi.rb extconf.rb checking for clang... yes checking for Magick-config... no checking for pkg-config... yes Package MagickCore was not found in the pkg-config search path. Perhaps you should add the directory containing `MagickCore.pc' to the PKG_CONFIG_PATH environment variable No package 'MagickCore' found checking for outdated ImageMagick version (<= 6.4.9)... *** 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=/Users/tnakano/.rbenv/versions/2.5.0/bin/$(RUBY_BASE_NAME) To see why this extension failed to compile, please check the mkmf.log which can be found here: /Users/tnakano/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/extensions/x86_64-darwin-16/2.5.0-static/rmagick-2.16.0/mkmf.log extconf failed, exit code 1 Gem files will remain installed in /Users/tnakano/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/rmagick-2.16.0 for inspection. Results logged to /Users/tnakano/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/extensions/x86_64-darwin-16/2.5.0-static/rmagick-2.16.0/gem_make.out An error occurred while installing rmagick (2.16.0), and Bundler cannot continue. Make sure that `gem install rmagick -v '2.16.0'` succeeds before bundling. In Gemfile: rmagick
おーエラーの内容が変わりましたね。一歩前進です。
次にエラーになっている原因を探すと、さっきと同じようなメッセージが出ていますね。
Make sure that
gem install rmagick -v '2.16.0'succeeds before bundling.
これを直訳するとバンドルする前に
gem install rmagick -v '2.16.0'が成功していることを確認してください。
ってなります。
同じようにgem rmagick
を調べて解決方法を探します。するとImagemagick
ってのをインストールしないといけないっぽいですね。
という感じで表示されたメッセージを良く読んで、エラー文をそのままGoogleさんに聞くと同じように悩んで解決したって記事がいっぱい見つかると思うので一つずつ試していきます。
imagemagickがインストールできない
$ brew install imagemagick ==> Installing imagemagick
Imagemagick
がインストール出来たので、もっかいbundle install
実行します。
$ bundle install . . . checking for outdated ImageMagick version (<= 6.4.9)... no checking for presence of MagickWand API (ImageMagick version >= 6.9.0)... no checking for Ruby version >= 1.8.5... yes
またエラーになりますね。
なんだかこの辺りが怪しそうなので、色々調べてみるとImageMagick
のバージョンが7系だとダメみたい。
$ convert --version Version: ImageMagick 7.0.7-22 Q16 x86_64 2018-01-22 http://www.imagemagick.org
うん。ダメぽ。
$ brew uninstall imagemagick Uninstalling /usr/local/Cellar/imagemagick/7.0.7-22... (1,527 files, 23.3MB) $ convert --version -bash: /usr/local/bin/convert: No such file or directory
うん。削除できてるっぽいですね。
$ brew install imagemagick@6 ==> Downloading https://homebrew.bintray.com/bottles/imagemagick@6-6.9.9-34.sierra.bottle.tar.gz ######################################################################## 100.0% ==> Pouring imagemagick@6-6.9.9-34.sierra.bottle.tar.gz ==> Caveats This formula is keg-only, which means it was not symlinked into /usr/local, because this is an alternate version of another formula. If you need to have this software first in your PATH run: echo 'export PATH="/usr/local/opt/imagemagick@6/bin:$PATH"' >> ~/.bash_profile For compilers to find this software you may need to set: LDFLAGS: -L/usr/local/opt/imagemagick@6/lib CPPFLAGS: -I/usr/local/opt/imagemagick@6/include For pkg-config to find this software you may need to set: PKG_CONFIG_PATH: /usr/local/opt/imagemagick@6/lib/pkgconfig ==> Summary 🍺 /usr/local/Cellar/imagemagick@6/6.9.9-34: 1,472 files, 22.8MB
$ convert --version -bash: /usr/local/bin/convert: No such file or directory
あれっなんだ。おかしいぞ。 もっかい見直してみる。
This formula is keg-only, which means it was not symlinked into /usr/local, because this is an alternate version of another formula. If you need to have this software first in your PATH run: echo 'export PATH="/usr/local/opt/imagemagick@6/bin:$PATH"' >> ~/.bash_profile For compilers to find this software you may need to set: LDFLAGS: -L/usr/local/opt/imagemagick@6/lib CPPFLAGS: -I/usr/local/opt/imagemagick@6/include For pkg-config to find this software you may need to set: PKG_CONFIG_PATH: /usr/local/opt/imagemagick@6/lib/pkgconfig
この辺りがエラーだろう。とりあえず翻訳だー
この式はkeg-onlyであり、/ usr / localにシンボリックリンクされていないことを意味し、 これは別の式の代替バージョンであるためです。 PATHでこのソフトウェアを最初に実行する必要がある場合: echo 'export PATH = "/ usr / local / opt / imagemagick @ 6 / bin:$ PATH"' >>〜/ .bash_profile
ふむふむ。パスが通ってないってことだと思うので...
$ echo 'export PATH="/usr/local/opt/imagemagick@6/bin:$PATH"' >> ~/.bash_profile $ source .bash_profile
もっかいimagemagick
をインストール
$ brew uninstall imagemagick@6 $ brew install imagemagick@6 $ convert --version Version: ImageMagick 6.9.9-34 Q16 x86_64 2018-01-22 http://www.imagemagick.org
よしよし。これで6.9になったのでもっかい
$ bundle install Gem::Ext::BuildError: ERROR: Failed to build gem native extension. current directory: /Users/tnakano/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/rmagick-2.16.0/ext/RMagick /Users/tnakano/.rbenv/versions/2.5.0/bin/ruby -r ./siteconf20180125-47095-pm8xl8.rb extconf.rb checking for clang... yes checking for Magick-config... yes checking for outdated ImageMagick version (<= 6.4.9)... no checking for presence of MagickWand API (ImageMagick version >= 6.9.0)... no Package MagickWand was not found in the pkg-config search path. Perhaps you should add the directory containing `MagickWand.pc' to the PKG_CONFIG_PATH environment variable No package 'MagickWand' found Package MagickWand was not found in the pkg-config search path. Perhaps you should add the directory containing `MagickWand.pc' to the PKG_CONFIG_PATH environment variable No package 'MagickWand' found Package MagickWand was not found in the pkg-config search path. Perhaps you should add the directory containing `MagickWand.pc' to the PKG_CONFIG_PATH environment variable No package 'MagickWand' found Package MagickWand was not found in the pkg-config search path. Perhaps you should add the directory containing `MagickWand.pc' to the PKG_CONFIG_PATH environment variable No package 'MagickWand' found . . An error occurred while installing rmagick (2.16.0), and Bundler cannot continue. Make sure that `gem install rmagick -v '2.16.0'` succeeds before bundling. In Gemfile: rmagick
くそーダメぽ。
もっかいエラーと睨めっこしてみるとPKG_CONFIG_PATH
ってのがNo
言われてる。
これを設定すれば良いのか?
$ echo 'export PKG_CONFIG_PATH="/usr/local/opt/imagemagick@6/lib/pkgconfig"' >> ~/.bash_profile $ source .bash_profile
今度こそ〜神様〜
$ bundle install Installing rmagick 2.16.0 with native extensions . . Bundle complete! 26 Gemfile dependencies, 95 gems now installed. Use `bundle info [gemname]` to see where a bundled gem is installed.
できたー!!!!!
まとめ
ハマるポイントとしては
imagemagic
は6系をインストールする必要がある!- インストールされた6系に対してパスを通してあげる必要がある
- エラーをよく読む!
答えはいつも「エラー」にある!
英語大事...