【Rails】インストール時につまづきがちなエラー集

【Rails】インストール時につまづきがちなエラー集

Railsのセットアップでハマり、無駄に時間がかかってしまうとモチベーションもダダ下がりです。
(少なくともぼくがそうです…)

そこでこの記事では、エラーとその対処法を紹介します。
もしかしたら今困っている最中のエラーも紹介しているかもしれません。

Bundle install実行時にMysqlインストールでエラー

Fetching mysql2 0.4.10
Installing mysql2 0.4.10 with native extensions
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

current directory:
/Users/yuhiisk/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/mysql2-0.4.10/ext/mysql2
/Users/yuhiisk/.rbenv/versions/2.5.0/bin/ruby -r
./siteconf20180327-38437-1omr160.rb extconf.rb
--with-opt-lib=/usr/local/opt/openssl/lib
--with-opt-include=-I/usr/local/opt/openssl/include --local
checking for rb_absint_size()... yes
checking for rb_absint_singlebit_p()... yes
checking for ruby/thread.h... yes
checking for rb_thread_call_without_gvl() in ruby/thread.h... yes
checking for rb_thread_blocking_region()... no
checking for rb_wait_for_single_fd()... yes
checking for rb_hash_dup()... yes
checking for rb_intern3()... yes
checking for rb_big_cmp()... yes
checking for mysql_query() in -lmysqlclient... no
-----
mysql client is missing. You may need to 'brew install mysql' or 'port install
mysql', and try again.
-----
*** 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=${opt-dir}/include
--with-opt-lib=${opt-dir}/lib
--with-make-prog
--without-make-prog
--srcdir=.
--curdir
--ruby=/Users/yuhiisk/.rbenv/versions/2.5.0/bin/$(RUBY_BASE_NAME)
--with-mysql-dir
--without-mysql-dir
--with-mysql-include
--without-mysql-include=${mysql-dir}/include
--with-mysql-lib
--without-mysql-lib=${mysql-dir}/lib
--with-mysql-config
--without-mysql-config
--with-mysql-dir
--without-mysql-dir
--with-mysql-include
--without-mysql-include=${mysql-dir}/include
--with-mysql-lib
--without-mysql-lib=${mysql-dir}/lib
--with-mysqlclientlib
--without-mysqlclientlib

To see why this extension failed to compile, please check the mkmf.log which can
be found here:

/Users/yuhiisk/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/extensions/x86_64-darwin-17/2.5.0-static/mysql2-0.4.10/mkmf.log

extconf failed, exit code 1

Gem files will remain installed in
/Users/yuhiisk/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/mysql2-0.4.10 for
inspection.
Results logged to
/Users/yuhiisk/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/extensions/x86_64-darwin-17/2.5.0-static/mysql2-0.4.10/gem_make.out

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

In Gemfile:
mysql2

mysqlがインストールされていないときに出るエラーなので、mysqlをインストールしましょう。

$ brew install mysql

rmagickインストール時にエラー

Installing rmagick 2.16.0 with native extensions
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

current directory:
/Users/yuhiisk/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/rmagick-2.16.0/ext/RMagick
/Users/yuhiisk/.rbenv/versions/2.5.0/bin/ruby -r
./siteconf20180327-39028-1asircf.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/yuhiisk/.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/yuhiisk/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/extensions/x86_64-darwin-17/2.5.0-static/rmagick-2.16.0/mkmf.log

extconf failed, exit code 1

Gem files will remain installed in
/Users/yuhiisk/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/rmagick-2.16.0 for
inspection.
Results logged to
/Users/yuhiisk/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/extensions/x86_64-darwin-17/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

rmagick(gem)をインストールするには、別途ImageMagickが必要です。
また、Rmagick v2.16.0は ImageMagick v7(記事執筆時の最新版) に対応していないので、v6をbrewで入れる必要があります。

$ brew install imagemagick@6

そして .bash_rc.bash_profile などにPATHを追加します。

export PKG_CONFIG_PATH=/usr/local/opt/imagemagick@6/lib/pkgconfig

参考: https://qiita.com/niwasawa/items/1144f20a9f2de4b76977

ローカルサーバー起動後にMysqlエラー

Mysql2::Error (Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)):

この場合、mysqlサーバーは動いているでしょうか。
事前にmysqlサーバーを起動しなければいけません。

$ mysql.server start

データベースがないエラー

Mysql2::Error: Unknown database ‘****'

途中から参加するプロジェクトだったら、データベースの準備を行いましょう。

$ rake db:create
$ rake db:migrate

mysqlの場合はmigrateをするだけではダメで、はじめにデータベースを作成します。

参考: railsでデータベース作成&マイグレート(MySQL)

Webpackerでエラー

Webpacker::Manifest::MissingEntryError in hogehoge

Showing /Users/yuhiisk/Work/sites/smout/app/views/layouts/application.html.slim where line #10 raised:
Webpacker can't find application.js in /Users/yuhiisk/Work/sites/smout/public/packs/manifest.json. Possible causes:
1. You want to set webpacker.yml value of compile to true for your environment
unless you are using the `webpack -w` or the webpack-dev-server.
2. webpack has not yet re-run to reflect updates.
3. You have misconfigured Webpacker's config/webpacker.yml file.
4. Your webpack configuration is not creating a manifest.
Your manifest contains:
{
}

該当部分が以下の場合。

= javascript_pack_tag 'application'

まず yarn で必要なライブラリをインストールしましょう。

$ bin/yarn

その後、webpack でJavaScriptをコンパイルを行います。

$ bin/webpack

もしくはJS配信用のdevサーバーを起動します。(自動でwatch&コンパイルされる)

$ bin/webpack-dev-server

【Rails】インストール時につまづきがちなエラー集

更新情報はTwitterでも配信しています