🖥️

Mac で pod コマンドが通らなくなったときの対応

2021/04/14に公開

環境

Mac
macOS Catalina 10.15.7
Ruby 2.6.3p62
(2019-04-16 revision 67580)
[universal.x86_64-darwin19]

問題

pod update しようとしたら通らず, 下記のようなエラーが出た。

-bash: /usr/local/bin/pod: /System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/bin/ruby: bad interpreter: No such file or directory

原因

エラーにある通り、CocoaPods が使おうとしてる Ruby が見当たらないらしい。
CocoaPods が見ている Ruby のバージョンが 2.3 なのに対し, Catalina では Ruby のバージョンはデフォルトで 2.6 になっていた。(※)
そのため, Ruby に合わせて CocoaPods のバージョンを上げる必要がある。

※ Ruby のバージョンを確認するコマンドはこちら

ruby --version

解決手順

  1. ターミナルを開く
  2. gem をアップデートする
    • 以下のコマンドを実行
      $ sudo gem update --system -n /usr/local/bin
      
    • 実行時のログ表示
      Password:
      
      Updating rubygems-update
      Fetching rubygems-update-3.2.16.gem
      Successfully installed rubygems-update-3.2.16
      Parsing documentation for rubygems-update-3.2.16
      Installing ri documentation for rubygems-update-3.2.16
      Installing darkfish documentation for rubygems-update-3.2.16
      Done installing documentation for rubygems-update after 171 seconds
      Parsing documentation for rubygems-update-3.2.16
      Done installing documentation for rubygems-update after 0 seconds
      Installing RubyGems 3.2.16
      ERROR:  While executing gem ... (Errno::EPERM)
          Operation not permitted @ rb_sysopen - /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/bin/gem
      
  3. CocoaPods をアップデートする
    • 以下のコマンドを実行
      $ sudo gem install -v1.8.4 cocoapods -n /usr/local/bin
      
    • 実行時のログ表示
      Password:
      
      Fetching xxx.gem
      Successfully installed xxx
      Parsing documentation for xxx
      Installing ri documentation for xxx
      Done installing documentation for xxxs after xxx seconds
      xxx gems installed
      

途中で困ったこと

CocoaPods をアップデートするとき, 最初は前述のものではなくバージョン指定なしのものを実行したらエラーが出た。

$ sudo gem install cocoapods -n /usr/local/bin
Password:
	
Fetching xxx.gem
Successfully installed xxx
Building native extensions. This could take a while...
ERROR:  Error installing cocoapods:
	ERROR: Failed to build gem native extension.
    current directory: /Library/Ruby/Gems/2.6.0/gems/ffi-1.15.0/ext/ffi_c
/System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/bin/ruby -I /Library/Ruby/Site/2.6.0 -r ./siteconf20210414-17533-vtgrh5.rb extconf.rb
*** 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: 
    --xxx
/System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/mkmf.rb:467:in `try_do': The compiler failed to generate an executable file. (RuntimeError)
You have to install development tools first.
	from /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/mkmf.rb:546:in `block in try_link0'
	from /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/tmpdir.rb:93:in `mktmpdir'
	from /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/mkmf.rb:543:in `try_link0'
	from /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/mkmf.rb:570:in `try_link'
	from /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/mkmf.rb:672:in `try_ldflags'
	from /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/mkmf.rb:1832:in `pkg_config'
	from extconf.rb:9:in `system_libffi_usable?'
	from extconf.rb:42:in `<main>'
To see why this extension failed to compile, please check the mkmf.log which can be found here:
  /Library/Ruby/Gems/2.6.0/extensions/universal-darwin-19/2.6.0/ffi-1.15.0/mkmf.log
extconf failed, exit code 1
Gem files will remain installed in /Library/Ruby/Gems/2.6.0/gems/ffi-1.15.0 for inspection.
Results logged to /Library/Ruby/Gems/2.6.0/extensions/universal-darwin-19/2.6.0/ffi-1.15.0/gem_make.out

律儀に mkmf.log ファイルも覗いてみたが, こちらはよく分からなかった。

似た症状を探した結果, 「バージョン指定なしで sudo gem install cocoapods -n /usr/local/bin を叩くと ERROR: Failed to build gem native extension. のエラーが出る」「Cocoapod のバージョンが新しすぎると, OS にデフォルトで入っているものよりも高いバージョンの Ruby が必要になる」ということだった。
現時点での Cocoapods の最新バージョンは 1.10.1 (2021/1/8) ではあるが, Cocoapods のバージョンに特にこだわりもなく動いてくれればよかったので, 調べていてよく見かけた 1.8.4 を指定して使ってみることにした。

備考

Catalina にしてから pod を使ってなかったため, pod コマンドが通らない状態になってるのに気づいていなかった。
OS アップデート時には環境を改めてチェックしといたほうが良さそう。

参考リンク

Discussion