bundle installに失敗するとき。

# rails sやbundle installが失敗
背景：一週間ほど前に、git cloneをして共同作業をした時にrailsのバージョンを少し触りました。
それが根本的な原因か分かりませんが、後日あらたに自主学習のためにlocalでフォルダを作成してrails new
を行い、rails s をするとエラーで怒られてしまいました。

1 railsのバージョンを触る
2 rails s や　bundle installをするとエラーで怒られる

`rails s`をすると

```terminal
Could not find gem 'rails (~> 5.1.7)' in any of the gem sources listed in your Gemfile.
Run `bundle install` to install missing gems.
```
そこで `gem install rails --version=“5.1.7”`を実行しました。
これは成功。

`bundle install`　が失敗する

```terminal

AkiyoshinoMacBook-Pro:hello_app aki$ bundle install --path vendor/bundle
The dependency tzinfo-data (>= 0) will be unused by any of the platforms Bundler is installing for. Bundler is installing for ruby but the dependency is only for x86-mingw32, x86-mswin32, x64-mingw32, java. To add those platforms to the bundle, run `bundle lock --add-platform x86-mingw32 x86-mswin32 x64-mingw32 java`.
Fetching gem metadata from https://rubygems.org/............
Fetching gem metadata from https://rubygems.org/.
Resolving dependencies....
(略)
Using coffee-rails 4.2.2
Fetching ffi 1.11.1
Installing ffi 1.11.1 with native extensions
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

    current directory: /Users/aki/Desktop/共同開発課題１/hello_app/vendor/bundle/ruby/2.6.0/gems/ffi-1.11.1/ext/ffi_c
/Users/aki/.rbenv/versions/2.6.2/bin/ruby -I /Users/aki/.rbenv/versions/2.6.2/lib/ruby/2.6.0 -r ./siteconf20190919-7011-1u1zt56.rb extconf.rb
checking for ffi.h... no
checking for ffi.h in /usr/local/include,/usr/include/ffi... no
checking for shlwapi.h... no
checking for rb_thread_call_without_gvl()... yes
checking for ruby_native_thread_p()... yes
checking for ruby_thread_has_gvl_p()... yes
creating extconf.h
creating Makefile

current directory: /Users/aki/Desktop/共同開発課題１/hello_app/vendor/bundle/ruby/2.6.0/gems/ffi-1.11.1/ext/ffi_c
make "DESTDIR=" clean

current directory: /Users/aki/Desktop/共同開発課題１/hello_app/vendor/bundle/ruby/2.6.0/gems/ffi-1.11.1/ext/ffi_c
make "DESTDIR="
Configuring libffi
clang: error: unsupported option '-print-multi-os-directory'
clang: error: no input files
cd "/Users/aki/Desktop/共同開発課題１/hello_app/vendor/bundle/ruby/2.6.0/gems/ffi-1.11.1/ext/ffi_c/libffi-x86_64-darwin18" &&
/Library/Developer/CommandLineTools/usr/bin/make
/Library/Developer/CommandLineTools/usr/bin/make 'AR_FLAGS=' 'CC_FOR_BUILD=' 'CFLAGS=-Wall -fexceptions' 'CXXFLAGS=-g -O2' 'CFLAGS_FOR_BUILD=' 'CFLAGS_FOR_TARGET='
'INSTALL=/usr/bin/install -c' 'INSTALL_DATA=/usr/bin/install -c -m 644' 'INSTALL_PROGRAM=/usr/bin/install -c' 'INSTALL_SCRIPT=/usr/bin/install -c' 'JC1FLAGS='
'LDFLAGS=' 'LIBCFLAGS=' 'LIBCFLAGS_FOR_TARGET=' 'MAKE=/Library/Developer/CommandLineTools/usr/bin/make' 'MAKEINFO=/bin/sh
/Users/aki/Desktop/共同開発課題１/hello_app/vendor/bundle/ruby/2.6.0/gems/ffi-1.11.1/ext/ffi_c/libffi/missing makeinfo ' 'PICFLAG=' 'PICFLAG_FOR_TARGET=' 'RUNTESTFLAGS='
'SHELL=/bin/sh' 'exec_prefix=/usr/local' 'infodir=/usr/local/share/info' 'libdir=/usr/local/lib' 'mandir=/usr/local/share/man' 'prefix=/usr/local' 'AR=ar' 'AS=as'
'CC=clang' 'CXX=g++' 'LD=ld' 'NM=/usr/bin/nm -B' 'RANLIB=ranlib' 'DESTDIR=' all-recursive
Making all in include
make[3]: *** No rule to make target `ffi_common.h', needed by `all-am'.  Stop.
make[2]: *** [all-recursive] Error 1
make[1]: *** [all] Error 2
make: *** ["/Users/aki/Desktop/共同開発課題１/hello_app/vendor/bundle/ruby/2.6.0/gems/ffi-1.11.1/ext/ffi_c/libffi-x86_64-darwin18"/.libs/libffi_convenience.a] Error 2

make failed, exit code 2

Gem files will remain installed in /Users/aki/Desktop/共同開発課題１/hello_app/vendor/bundle/ruby/2.6.0/gems/ffi-1.11.1 for inspection.
Results logged to /Users/aki/Desktop/共同開発課題１/hello_app/vendor/bundle/ruby/2.6.0/extensions/x86_64-darwin-18/2.6.0-static/ffi-1.11.1/gem_make.out

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

In Gemfile:
  spring-watcher-listen was resolved to 2.0.1, which depends on
    listen was resolved to 3.1.5, which depends on
      rb-inotify was resolved to 0.10.0, which depends on
        ffi
AkiyoshinoMacBook-Pro:hello_app aki$
```

どうも最後らへんのffiで怒られているぽっかたです。試行錯誤しましたが、自分では解決できず、いつもお世話になっている方に助け舟を出してもらいました。すると以下をググるとbundlerが悪いという記事を教えていだたき
解決へ。

```terminal

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



解決方法は以下を順にやっていくといけました。bundlerの公式に書いてあります。

```
# remove user-specific gems and git repos
rm -rf ~/.bundle/ ~/.gem/

# remove system-wide git repos and git checkouts
rm -rf $GEM_HOME/bundler/ $GEM_HOME/cache/bundler/

# remove project-specific settings and git repos
rm -rf .bundle/

# remove project-specific cached .gem files
rm -rf vendor/cache/

# remove the saved resolve of the Gemfile
rm -rf Gemfile.lock

# try to install one more time
bundle install
```



参考記事
https://stackoverflow.com/questions/10575679/bundle-update-fails-on-ffi