Rails4系 で bundle install したら nokogiri と mysql でエラーが出た

# 背景
git clone して開発をするとき、cloneしたあと bundle install のコマンドをターミナルに入力するとnokogiriのエラーに当たった。。。
解決までの備忘録として載せます。

# 開発環境
- mac OC : Mojava
- バージョン : 10.14.1
- ruby : 2.3.1
- rails : 4.2.1
- mysql : Ver 14.14 Distrib 5.6.42

# コマンドとエラー文
※文中に出てくる、ターミナル内でのusernameは下記では[user_name]と記載しています。
例） ユーザーの名前がyamada-taroさんだった場合…

```
$ Users/yamada-taro/
　↓  
$ Users/[user_name]/　としています
```

## 1. bundle install する
すると下記のエラー文が出てきた

```
$ bundle install
Fetching gem metadata from https://rubygems.org/..........
Using rake 10.4.2
Using i18n 0.7.0
Using json 1.8.2
Using minitest 5.6.1
Using thread_safe 0.3.5
Using tzinfo 1.2.2
Using activesupport 4.2.1
Using builder 3.2.2
Using erubis 2.7.0
Using mini_portile 0.6.2
Fetching nokogiri 1.6.6.2
Installing nokogiri 1.6.6.2 with native extensions
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

current directory:
/Users/[user_name]/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/nokogiri-1.6.6.2/ext/nokogiri
/Users/[user_name]/.rbenv/versions/2.3.1/bin/ruby -r ./siteconf20181124-53525-3izfg3.rb
extconf.rb --use-system-libraries
--with-xml2-include=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/usr/include/libxml2/libxml
checking if the C compiler accepts ... yes
checking if the C compiler accepts
-Wno-error=unused-command-line-argument-hard-error-in-future... no
Building nokogiri using system libraries.
libxml2 version 2.6.21 or later is required!
*** 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/[user_name]/.rbenv/versions/2.3.1/bin/$(RUBY_BASE_NAME)
	--help
	--clean
	--use-system-libraries
	--with-zlib-dir
	--without-zlib-dir
	--with-zlib-include
	--without-zlib-include=${zlib-dir}/include
	--with-zlib-lib
	--without-zlib-lib=${zlib-dir}/lib
	--with-xml2-dir
	--without-xml2-dir
	--with-xml2-include=${xml2-dir}/include
	--with-xml2-lib
	--without-xml2-lib=${xml2-dir}/lib
	--with-xslt-dir
	--without-xslt-dir
	--with-xslt-include
	--without-xslt-include=${xslt-dir}/include
	--with-xslt-lib
	--without-xslt-lib=${xslt-dir}/lib
	--with-libxslt-config
	--without-libxslt-config
	--with-pkg-config
	--without-pkg-config
	--with-exslt-dir
	--without-exslt-dir
	--with-exslt-include
	--without-exslt-include=${exslt-dir}/include
	--with-exslt-lib
	--without-exslt-lib=${exslt-dir}/lib
	--with-libexslt-config
	--without-libexslt-config

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

/Users/[user_name]/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/extensions/x86_64-darwin-18/2.3.0-static/nokogiri-1.6.6.2/mkmf.log

extconf failed, exit code 1

Gem files will remain installed in
/Users/[user_name]/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/nokogiri-1.6.6.2 for
inspection.
Results logged to
/Users/[user_name]/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/extensions/x86_64-darwin-18/2.3.0-static/nokogiri-1.6.6.2/gem_make.out

An error occurred while installing nokogiri (1.6.6.2), and Bundler cannot
continue.
Make sure that `gem install nokogiri -v '1.6.6.2' --source 'https://rubygems.org/'`
succeeds before bundling.

In Gemfile:
  rails was resolved to 4.2.1, which depends on
    actionmailer was resolved to 4.2.1, which depends on
      actionpack was resolved to 4.2.1, which depends on
        actionview was resolved to 4.2.1, which depends on
          rails-dom-testing was resolved to 1.0.6, which depends on
            nokogiri
```

**gem install nokogiri -v '1.6.6.2'  しなさいと言われてるので、その通りにしても同じエラーになる。。。**


## 2. あらかじめあるGemfile.lockを削除する
git clone してきたファイルにあらかじめ設置されていたGemfile.lockがあるので、Gemfile.lockのファイルそのものを削除する

## 3. bundle update する
**エラー文が変わった(・o・)**

```
$ bundle update
Fetching gem metadata from https://rubygems.org/..........
Fetching gem metadata from https://rubygems.org/.
Resolving dependencies......
Using rake 12.3.1
Using concurrent-ruby 1.1.3
Using i18n 0.9.5
Using json 1.8.6
Using minitest 5.11.3
Using thread_safe 0.3.6
Using tzinfo 1.2.5
Using activesupport 4.2.1
Using builder 3.2.3
Using erubis 2.7.0
Using mini_portile2 2.3.0
Using nokogiri 1.8.5
Using rails-deprecated_sanitizer 1.0.3
Using rails-dom-testing 1.0.9
Using crass 1.0.4
Using loofah 2.2.3
Using rails-html-sanitizer 1.0.4
Using actionview 4.2.1
Using rack 1.6.11
Using rack-test 0.6.3
Using actionpack 4.2.1
Using globalid 0.4.1
Using activejob 4.2.1
Using mini_mime 1.0.1
Using mail 2.7.1
Using actionmailer 4.2.1
Using activemodel 4.2.1
Using arel 6.0.4
Using activerecord 4.2.1
Using bcrypt 3.1.12
Using debug_inspector 0.0.3
Using binding_of_caller 0.8.0
Using bundler 1.17.1
Using byebug 10.0.2
Using chunky_png 1.3.11
Using coderay 1.1.2
Using coffee-script-source 1.12.2
Using execjs 2.7.0
Using coffee-script 2.4.1
Using thor 0.20.3
Using railties 4.2.1
Using coffee-rails 4.1.1
Using fssm 0.2.10
Using sass 3.2.19
Using compass 0.12.7
Using compass-rails 2.0.0
Using orm_adapter 0.5.0
Using responders 2.4.0
Using warden 1.2.7
Using devise 4.5.0
Using hike 1.2.3
Using multi_json 1.13.1
Using jbuilder 2.8.0
Using jquery-rails 4.3.3
Using kaminari-core 1.1.1
Using kaminari-actionview 1.1.1
Using kaminari-activerecord 1.1.1
Using kaminari 1.1.1
Using method_source 0.9.2
Fetching mysql2 0.3.18
Installing mysql2 0.3.18 with native extensions
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

current directory:
/Users/[user_name]/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/mysql2-0.3.18/ext/mysql2
/Users/[user_name]/.rbenv/versions/2.3.1/bin/ruby -r ./siteconf20181124-53767-3dd4ey.rb
extconf.rb
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
-----
Using mysql_config at /usr/local/opt/mysql@5.6/bin/mysql_config
-----
checking for mysql.h... yes
checking for errmsg.h... yes
checking for mysqld_error.h... yes
-----
Don't know how to set rpath on your system, if MySQL libraries are not in path mysql2
may not load
-----
-----
Setting libpath to /usr/local/opt/mysql@5.6/lib
-----
creating Makefile

current directory:
/Users/[user_name]/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/mysql2-0.3.18/ext/mysql2
make "DESTDIR=" clean

current directory:
/Users//.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/mysql2-0.3.18/ext/mysql2
make "DESTDIR="
compiling infile.c
compiling client.c
compiling mysql2_ext.c
compiling result.c
linking shared-object mysql2/mysql2.bundle
ld: library not found for -lssl
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [mysql2.bundle] Error 1

make failed, exit code 2

Gem files will remain installed in
/Users/[user_name]/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/mysql2-0.3.18 for
inspection.
Results logged to
/Users/[user_name]/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/extensions/x86_64-darwin-18/2.3.0-static/mysql2-0.3.18/gem_make.out

An error occurred while installing mysql2 (0.3.18), and Bundler cannot
continue.
Make sure that `gem install mysql2 -v '0.3.18' --source 'https://rubygems.org/'`
succeeds before bundling.
```

**gem install mysql2 -v '0.3.18' しろっていうエラーに変わった**

## 4. bundlerの構成オプションを設定して下記のコマンドを入力

```
$ bundle config --local build.mysql2 --with-ldflags=-L/usr/local/opt/openssl/lib --with-cppflags=-I/usr/local/opt/openssl/include
```


## 5. OpenSSLを入れる

```
$ brew install openssl
Warning: openssl 1.0.2p is already installed and up-to-date
To reinstall 1.0.2p, run `brew reinstall openssl`
```

## 6. reinstall しろと言われるのでその通りにする

```
$ brew reinstall openssl
```

## 7. bundle update して、bundle install して解決！！！