🐷

FlutterでCocoaPods not installed.のエラーを公式の方法で解消する!!!!

2023/09/23に公開

tl;dr

最新のRubyをインストールした上で、PATHを通す。
(なぜ、この情報がどこにも無いんだ、。。。)
結果、cocoapodsが認識できる模様。。。

背景

とある日、Flutter Doctorを掛けてみると、以下のようなログを出力された。

shell
[✓] Flutter (Channel stable, 3.13.3, on macOS 13.5 22G74 darwin-arm64, locale
    ja-JP)
[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.1)
[!] Xcode - develop for iOS and macOS (Xcode 15.0)
    ✗ CocoaPods not installed.
        CocoaPods is used to retrieve the iOS and macOS platform side's plugin code that responds to your plugin usage on the Dart side.
        Without CocoaPods, plugins will not work on iOS or macOS.
        For more info, see https://flutter.dev/platform-plugins
      To install see https://guides.cocoapods.org/using/getting-started.html#installation for instructions.
[✓] Chrome - develop for the web
[✓] Android Studio (version 2021.3)
[✓] Android Studio (version 2022.3)
[✓] VS Code (version 1.82.2)
[✓] Connected device (3 available)
[✓] Network resources

どうやら、cocoapodsがインストールされていないため、このような警告文が出ている模様。
そこで、認識するできるよう対応したが、中々これが苦労したので備忘録として対応歴を残す。

対応

まず、警告文にあった以下のサイトにアクセスして指示通りにインストールしてみる。

https://guides.cocoapods.org/using/getting-started.html#installation

そして、"flutter doctor"を叩く。

shell
[✓] Flutter (Channel stable, 3.13.3, on macOS 13.5 22G74 darwin-arm64, locale
    ja-JP)
[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.1)
[!] Xcode - develop for iOS and macOS (Xcode 15.0)
    ✗ CocoaPods not installed.
        CocoaPods is used to retrieve the iOS and macOS platform side's plugin code that responds to your plugin usage on the Dart side.
        Without CocoaPods, plugins will not work on iOS or macOS.
        For more info, see https://flutter.dev/platform-plugins
      To install see https://guides.cocoapods.org/using/getting-started.html#installation for instructions.
[✓] Chrome - develop for the web
[✓] Android Studio (version 2021.3)
[✓] Android Studio (version 2022.3)
[✓] VS Code (version 1.82.2)
[✓] Connected device (3 available)
[✓] Network resources

しかし、同様の警告が発生する。

そこで、インストールできているか確認するため、以下のgemにインストールされているライブラリを確認できるコマンドを叩いてみる。

shell
$ gem list

結果、インストールできていることを確認した。

(中略)
cocoapods (1.12.1)
cocoapods-core (1.12.1)
cocoapods-deintegrate (1.0.5)
cocoapods-downloader (1.6.3)
cocoapods-plugins (1.0.0)
cocoapods-search (1.0.1)
cocoapods-trunk (1.6.0)
cocoapods-try (1.2.0)
(中略)

なぜだ、。。。
なぜ認識しない、。。。。

解決策

結論としては、PATHの問題だったぽい。

以下のサイトを参考にcocoapodsのインストールするとうまくいった。
※具体的なやり方は省略

https://tech.amefure.com/swift-cocoapods-rbenv

キー となるのは以下の部分。
この記述を.zshrc(bashを使っている場合は.bashrc)にすることでPATHが通る。

shell
$ vi ~/.zshrc
// viで開いた中に記述
export PATH="$HOME/.rbenv/bin:$PATH"
eval "$(rbenv init -)"

一通り対応した後、再度'flutter doctor'をすると以下の通り成功し、flutterがcocoapodsを認識できるようになった!!

shell
[✓] Flutter (Channel stable, 3.13.3, on macOS 13.5 22G74 darwin-arm64, locale
    ja-JP)
[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.1)
[✓] Xcode - develop for iOS and macOS (Xcode 15.0)
[✓] Chrome - develop for the web
[✓] Android Studio (version 2021.3)
[✓] Android Studio (version 2022.3)
[✓] VS Code (version 1.82.2)
[✓] Connected device (3 available)
[✓] Network resources

まとめ

ライブラリが認識しないときはPATHを疑おう!!

余談

タイトルのエラーをGoogle検索すると、以下のサイトが出てくるが、以上の対応でもし解決できない場合に最終手段として活用してほしい。
https://qiita.com/yoshinyan/items/2c9a59e5a3bd80b9bdbd

解説(gemとbrewの違い)

前述のgemとbrewの違いは、管理するパッケージマネージャーという点である。
gemとは RubyGem のことでプログラミング言語 Ruby に同梱されているRuby向けのパッケージマネージャーのことである。cocoapods自体がRubyによって開発が行われているため、公式ではgemを使ってインストールを行っている。
それに対し、brewはmac(およびLinux)向けのパッケージマネージャーであり、より汎用的なパッケージマネージャーとして利用されている。

結論としては、brewでもgemでもcocoapodsはインストールすることが可能であり、どちらからインストールしても構わない。

ただし、brewでインストールした場合はmacで認識されているRubyのバージョン依存で動くため、何かしらの形でrubyのバージョンを変えた場合にトラブルが起こる可能性がある。(個人の考え)
管理上、rubyに同梱されているgemで管理した方がRubyのバージョンと一緒に管理できるため、よりトラブルが少なくなると思われる。

Discussion