【Rails6】新規プロジェクトを作成する際の手順、エラーの対処法

rails6、Mysqlで新規プロジェクトを作成する際につまづいた部分を書き残します。

```

$ cd environment

$ rails new database-test -d mysql
```

ここで、下記のエラー。

```
In Gemfile:
  mysql2
         run  bundle binstubs bundler
Could not find gem 'rails (~> 6.0.2, >= 6.0.2.1)' in any of the gem sources listed in your Gemfile.
         run  bundle exec spring binstub --all
Could not find gem 'rails (~> 6.0.2, >= 6.0.2.1)' in any of the gem sources listed in your Gemfile.
Run `bundle install` to install missing gems.
       rails  webpacker:install
Could not find gem 'rails (~> 6.0.2, >= 6.0.2.1)' in any of the gem sources listed in your Gemfile.
Run `bundle install` to install missing gems.
```

したがって下記の2コマンドをうつ。

```
$ gem install mysql2 -v '0.5.3'
$ brew info openssl
```

すると、下記のように出る。


```
openssl@1.1: stable 1.1.1g (bottled) [keg-only]
Cryptography and SSL/TLS Toolkit
https://openssl.org/
/usr/local/Cellar/openssl@1.1/1.1.1g (8,059 files, 18MB)
  Poured from bottle on 2020-04-24 at 16:06:07
From: https://github.com/Homebrew/homebrew-core/blob/master/Formula/openssl@1.1.rb
==> Caveats
A CA file has been bootstrapped using certificates from the system
keychain. To add additional certificates, place .pem files in
  /usr/local/etc/openssl@1.1/certs

and run
  /usr/local/opt/openssl@1.1/bin/c_rehash

openssl@1.1 is keg-only, which means it was not symlinked into /usr/local,
because macOS provides LibreSSL.

If you need to have openssl@1.1 first in your PATH run:
  echo 'export PATH="/usr/local/opt/openssl@1.1/bin:$PATH"' >> /Users/shogo/.bash_profile

For compilers to find openssl@1.1 you may need to set:
  export LDFLAGS="-L/usr/local/opt/openssl@1.1/lib"
  export CPPFLAGS="-I/usr/local/opt/openssl@1.1/include"

For pkg-config to find openssl@1.1 you may need to set:
  export PKG_CONFIG_PATH="/usr/local/opt/openssl@1.1/lib/pkgconfig"
```

で、下記のこの部分が大事らしいので、

```
For compilers to find openssl@1.1 you may need to set:
  export LDFLAGS="-L/usr/local/opt/openssl@1.1/lib"
  export CPPFLAGS="-I/usr/local/opt/openssl@1.1/include"
```

パスを通してあげる。

```
$ bundle config --local build.mysql2 "--with-cppflags=-I/usr/local/opt/openssl@1.1/include"
$ bundle config --local build.mysql2 "--with-ldflags=-L/usr/local/opt/openssl/lib"
You are replacing the current local value of build.mysql2, which is currently "--with-cppflags=-I/usr/local/opt/openssl@1.1/include"
```

で、bundle installする。

```
$ bundle install
```


そして、rails sをしたところ次のエラー。

```
	14: from /Users/shogo/environment/database-test/vendor/bundle/ruby/2.6.0/gems/railties-6.0.3.2/lib/rails/initializable.rb:61:in `block in run_initializers'
	13: from /Users/shogo/environment/database-test/vendor/bundle/ruby/2.6.0/gems/railties-6.0.3.2/lib/rails/initializable.rb:32:in `run'
	12: from /Users/shogo/environment/database-test/vendor/bundle/ruby/2.6.0/gems/railties-6.0.3.2/lib/rails/initializable.rb:32:in `instance_exec'
	11: from /Users/shogo/environment/database-test/vendor/bundle/ruby/2.6.0/gems/webpacker-4.2.2/lib/webpacker/railtie.rb:84:in `block in <class:Engine>'
	10: from /Users/shogo/environment/database-test/vendor/bundle/ruby/2.6.0/gems/webpacker-4.2.2/lib/webpacker.rb:35:in `bootstrap'
	 9: from /Users/shogo/environment/database-test/vendor/bundle/ruby/2.6.0/gems/webpacker-4.2.2/lib/webpacker/commands.rb:25:in `bootstrap'
	 8: from /Users/shogo/environment/database-test/vendor/bundle/ruby/2.6.0/gems/webpacker-4.2.2/lib/webpacker/manifest.rb:18:in `refresh'
	 7: from /Users/shogo/environment/database-test/vendor/bundle/ruby/2.6.0/gems/webpacker-4.2.2/lib/webpacker/manifest.rb:83:in `load'
	 6: from /Users/shogo/environment/database-test/vendor/bundle/ruby/2.6.0/gems/webpacker-4.2.2/lib/webpacker/configuration.rb:51:in `public_manifest_path'
	 5: from /Users/shogo/environment/database-test/vendor/bundle/ruby/2.6.0/gems/webpacker-4.2.2/lib/webpacker/configuration.rb:47:in `public_output_path'
	 4: from /Users/shogo/environment/database-test/vendor/bundle/ruby/2.6.0/gems/webpacker-4.2.2/lib/webpacker/configuration.rb:43:in `public_path'
	 3: from /Users/shogo/environment/database-test/vendor/bundle/ruby/2.6.0/gems/webpacker-4.2.2/lib/webpacker/configuration.rb:84:in `fetch'
	 2: from /Users/shogo/environment/database-test/vendor/bundle/ruby/2.6.0/gems/webpacker-4.2.2/lib/webpacker/configuration.rb:88:in `data'
	 1: from /Users/shogo/environment/database-test/vendor/bundle/ruby/2.6.0/gems/webpacker-4.2.2/lib/webpacker/configuration.rb:91:in `load'
/Users/shogo/environment/database-test/vendor/bundle/ruby/2.6.0/gems/webpacker-4.2.2/lib/webpacker/configuration.rb:95:in `rescue in load': Webpacker configuration file not found /Users/shogo/environment/database-test/config/webpacker.yml. Please run rails webpacker:install Error: No such file or directory @ rb_sysopen - /Users/shogo/environment/database-test/config/webpacker.yml (RuntimeError)
```

で、webpacker周りのエラーなのでnodeをインストール。インストールされていたが、アップデートしなければいけないそうなのでアップデートする。

```
$ brew install node
==> Downloading https://homebrew.bintray.com/bottles/node-14.4.0.catalina.bottle.tar.gz
Already downloaded: /Users/shogo/Library/Caches/Homebrew/downloads/ad92a0872e430f325b2a2dc58490e3b68e9d10817f5d9a94e2515455e25baadc--node-14.4.0.catalina.bottle.tar.gz
Error: node 13.7.0 is already installed
To upgrade to 14.4.0, run `brew upgrade node`.
$ brew upgrade node

#最後にError: No such file or directory - /usr/local/Cellar/yarn/1.21.1と出たが、無視する。
```

その状態でrails webpacker:installを行う。

```
$ rails webpacker:install

~省略~

Webpacker successfully installed 🎉 🍰
```


これでrails sをしておなじみの画面が表示されれば成功です。


参考
https://qiita.com/libertyu/items/1eb74adc817ab8971100
https://qiita.com/pharma_tech3/items/2ab578eb5b07ff0ca296