CocoaPods環境構築

# CocoaPods環境構築

## はじめに
Macを買い替えるたびに、Cocoapodsのインストールをする必要があり、
毎回忘れてしまうので、メモとして残します。

## 実行環境
Mac mini 2023 M2チップ搭載モデル
macOS：Sonoma14.5

## 結論
下記コマンドで導入できました。
homebrewインストール後、homebrew経由でcocoapodsをインストールします。

※下記の２行目の部分に関しては、一部自分自身のパスに変更する必要があります。
　takayanagiryoutaと記載されていますが、変更してください。
```objc
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
(echo; echo 'eval "$(/opt/homebrew/bin/brew shellenv)"') >> /Users/takayanagiryouta/.zprofile
eval "$(/opt/homebrew/bin/brew shellenv)"
brew install cocoapods
```

## エビデンス
※下記は誤りです。pod installしてみたところ上手く動作しないことがわかりました。
~~下記の通り、インストールを実施します。~~
```objc
//Podfileを作成するフォルダに移動
Mac-mini Desktop % cd rakucode 

//フォルダ内確認
Mac-mini rakucode % ls
Rakucode		RakucodeTests
Rakucode.xcodeproj	RakucodeUITests

//pod initコマンドでPodfile作成しようとするが、cocoapodsがインストールされていないとエラー。
Mac-mini rakucode % pod init
zsh: command not found: pod

//下記コマンドでcocoapodsをインストールする
Mac-mini rakucode % sudo gem install cocoapods
Password:
Fetching gh_inspector-1.1.3.gem
Fetching fourflusher-2.3.1.gem
Fetching nap-1.1.0.gem
Fetching ruby-macho-2.5.1.gem
Fetching addressable-2.8.7.gem
Fetching colored2-3.1.2.gem
Fetching escape-0.0.4.gem
Fetching public_suffix-4.0.7.gem
Fetching rexml-3.2.9.gem
Fetching nanaimo-0.3.0.gem
Fetching claide-1.1.0.gem
Fetching atomos-0.1.3.gem
Fetching xcodeproj-1.24.0.gem
Fetching molinillo-0.8.0.gem
Fetching cocoapods-try-1.2.0.gem
Fetching netrc-0.11.0.gem
Fetching cocoapods-trunk-1.6.0.gem
Fetching cocoapods-search-1.0.1.gem
Fetching cocoapods-plugins-1.0.0.gem
Fetching cocoapods-downloader-2.1.gem
Fetching cocoapods-deintegrate-1.0.5.gem
Fetching ffi-1.17.0-x86_64-darwin.gem
Fetching ethon-0.16.0.gem
Fetching typhoeus-1.4.1.gem
Fetching concurrent-ruby-1.3.3.gem
Fetching httpclient-2.8.3.gem
Fetching algoliasearch-1.27.5.gem
Fetching fuzzy_match-2.0.4.gem
Fetching drb-2.2.1.gem
Fetching base64-0.2.0.gem
Fetching connection_pool-2.4.1.gem
Fetching tzinfo-2.0.6.gem
Fetching i18n-1.14.5.gem
Fetching activesupport-7.1.3.4.gem
Fetching cocoapods-core-1.15.2.gem
Fetching cocoapods-1.15.2.gem
Successfully installed public_suffix-4.0.7
Successfully installed addressable-2.8.7
Successfully installed ruby-macho-2.5.1
Successfully installed nap-1.1.0
Successfully installed gh_inspector-1.1.3
Successfully installed fourflusher-2.3.1
Successfully installed escape-0.0.4
Successfully installed colored2-3.1.2
Successfully installed rexml-3.2.9
Successfully installed nanaimo-0.3.0
Successfully installed claide-1.1.0
Successfully installed atomos-0.1.3
Successfully installed xcodeproj-1.24.0
Successfully installed molinillo-0.8.0
Successfully installed cocoapods-try-1.2.0
Successfully installed netrc-0.11.0
Successfully installed cocoapods-trunk-1.6.0
Successfully installed cocoapods-search-1.0.1
Successfully installed cocoapods-plugins-1.0.0
Successfully installed cocoapods-downloader-2.1
Successfully installed cocoapods-deintegrate-1.0.5
Successfully installed ffi-1.17.0-x86_64-darwin
Successfully installed ethon-0.16.0
Successfully installed typhoeus-1.4.1
Successfully installed concurrent-ruby-1.3.3
Successfully installed httpclient-2.8.3
A new major version is available for Algolia! Please now use the https://rubygems.org/gems/algolia gem to get the latest features.
Successfully installed algoliasearch-1.27.5
Successfully installed fuzzy_match-2.0.4
ERROR:  Error installing cocoapods:
	The last version of drb (>= 0) to support your Ruby & RubyGems was 2.0.6. Try installing it with `gem install drb -v 2.0.6` and then running the current command again
	drb requires Ruby version >= 2.7.0. The current ruby version is 2.6.10.210.

//上記のエラー文に書かれた通り、インストールを実行する
Mac-mini rakucode % gem install drb -v 2.0.6
Fetching ruby2_keywords-0.0.5.gem
Fetching drb-2.0.6.gem
ERROR:  While executing gem ... (Gem::FilePermissionError)
    You don't have write permissions for the /Library/Ruby/Gems/2.6.0 directory.

//スーパーユーザで改めてインストールを実行する
Mac-mini rakucode % sudo gem install drb -v 2.0.6
Fetching drb-2.0.6.gem
Fetching ruby2_keywords-0.0.5.gem
Successfully installed ruby2_keywords-0.0.5
Successfully installed drb-2.0.6
Parsing documentation for ruby2_keywords-0.0.5
Installing ri documentation for ruby2_keywords-0.0.5
Parsing documentation for drb-2.0.6
Installing ri documentation for drb-2.0.6
Done installing documentation for ruby2_keywords, drb after 0 seconds
2 gems installed

//もう一度cocoapodsのインストールを試す
Mac-mini rakucode % sudo gem install cocoapods   
Successfully installed base64-0.2.0
Successfully installed connection_pool-2.4.1
Successfully installed tzinfo-2.0.6
Successfully installed i18n-1.14.5
ERROR:  Error installing cocoapods:
	The last version of activesupport (>= 5.0, < 8) to support your Ruby & RubyGems was 6.1.7.8. Try installing it with `gem install activesupport -v 6.1.7.8` and then running the current command again
	activesupport requires Ruby version >= 2.7.0. The current ruby version is 2.6.10.210.

//上記のエラー文に記載された通り、インストールを実施する
Mac-mini rakucode % gem install activesupport -v 6.1.7.8
Fetching zeitwerk-2.6.17.gem
Fetching activesupport-6.1.7.8.gem
ERROR:  While executing gem ... (Gem::FilePermissionError)
    You don't have write permissions for the /Library/Ruby/Gems/2.6.0 directory.

//スーパーユーザで改めてインストールを実行する
Mac-mini rakucode % sudo gem install activesupport -v 6.1.7.8
Fetching activesupport-6.1.7.8.gem
Fetching zeitwerk-2.6.17.gem
Successfully installed zeitwerk-2.6.17
Successfully installed activesupport-6.1.7.8
Parsing documentation for zeitwerk-2.6.17
Installing ri documentation for zeitwerk-2.6.17
Parsing documentation for activesupport-6.1.7.8
Installing ri documentation for activesupport-6.1.7.8
Done installing documentation for zeitwerk, activesupport after 1 seconds
2 gems installed

//もう一度cocoapodsのインストールを試す
Mac-mini rakucode % sudo gem install cocoapods               
Successfully installed cocoapods-core-1.15.2
Successfully installed cocoapods-1.15.2
Parsing documentation for cocoapods-core-1.15.2
Installing ri documentation for cocoapods-core-1.15.2
Parsing documentation for cocoapods-1.15.2
Installing ri documentation for cocoapods-1.15.2
Done installing documentation for cocoapods-core, cocoapods after 1 seconds
2 gems installed

//cocoapodsのインストールができたため、pod initでPodfileを作成する
Mac-mini rakucode % pod init

//Podfileが作成されたことが確認できる。
Mac-mini rakucode % ls
Podfile			Rakucode		Rakucode.xcodeproj	RakucodeTests		RakucodeUITests
```

下記が正しいです。

```objc
//pod installを試してみたところ、下記の通りエラーとなりました。
Mac-mini rakucode % pod install
Analyzing dependencies

[!] Automatically assigning platform `iOS` with version `17.2` on target `Rakucode` because no platform was specified. Please specify a platform for this target in your Podfile. See `https://guides.cocoapods.org/syntax/podfile.html#platform`.
/System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require': cannot load such file -- ffi_c (LoadError)
	from /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require'
	from /Library/Ruby/Gems/2.6.0/gems/ffi-1.17.0-x86_64-darwin/lib/ffi.rb:5:in `rescue in <top (required)>'
	from /Library/Ruby/Gems/2.6.0/gems/ffi-1.17.0-x86_64-darwin/lib/ffi.rb:2:in `<top (required)>'
	from /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require'
	from /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require'
	from /Library/Ruby/Gems/2.6.0/gems/ethon-0.16.0/lib/ethon.rb:3:in `<top (required)>'
	from /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require'
	from /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require'
	from /Library/Ruby/Gems/2.6.0/gems/typhoeus-1.4.1/lib/typhoeus.rb:2:in `<top (required)>'
	from /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require'
	from /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require'
	from /Library/Ruby/Gems/2.6.0/gems/cocoapods-core-1.15.2/lib/cocoapods-core/cdn_source.rb:440:in `download_typhoeus_impl_async'
	from /Library/Ruby/Gems/2.6.0/gems/cocoapods-core-1.15.2/lib/cocoapods-core/cdn_source.rb:372:in `download_and_save_with_retries_async'
	from /Library/Ruby/Gems/2.6.0/gems/cocoapods-core-1.15.2/lib/cocoapods-core/cdn_source.rb:365:in `download_file_async'
	from /Library/Ruby/Gems/2.6.0/gems/cocoapods-core-1.15.2/lib/cocoapods-core/cdn_source.rb:338:in `download_file'
	from /Library/Ruby/Gems/2.6.0/gems/cocoapods-core-1.15.2/lib/cocoapods-core/cdn_source.rb:53:in `refresh_metadata'
	from /Library/Ruby/Gems/2.6.0/gems/cocoapods-core-1.15.2/lib/cocoapods-core/source.rb:31:in `initialize'
	from /Library/Ruby/Gems/2.6.0/gems/cocoapods-core-1.15.2/lib/cocoapods-core/cdn_source.rb:30:in `initialize'
	from /Library/Ruby/Gems/2.6.0/gems/cocoapods-core-1.15.2/lib/cocoapods-core/source/manager.rb:315:in `new'
	from /Library/Ruby/Gems/2.6.0/gems/cocoapods-core-1.15.2/lib/cocoapods-core/source/manager.rb:315:in `block in source_from_path'
	from /Library/Ruby/Gems/2.6.0/gems/cocoapods-core-1.15.2/lib/cocoapods-core/source/manager.rb:322:in `source_from_path'
	from /Library/Ruby/Gems/2.6.0/gems/cocoapods-core-1.15.2/lib/cocoapods-core/source/manager.rb:331:in `block in aggregate_with_repos'
	from /Library/Ruby/Gems/2.6.0/gems/cocoapods-core-1.15.2/lib/cocoapods-core/source/manager.rb:331:in `map'
	from /Library/Ruby/Gems/2.6.0/gems/cocoapods-core-1.15.2/lib/cocoapods-core/source/manager.rb:331:in `aggregate_with_repos'
	from /Library/Ruby/Gems/2.6.0/gems/cocoapods-core-1.15.2/lib/cocoapods-core/source/manager.rb:26:in `aggregate'
	from /Library/Ruby/Gems/2.6.0/gems/cocoapods-core-1.15.2/lib/cocoapods-core/source/manager.rb:60:in `all'
	from /Library/Ruby/Gems/2.6.0/gems/cocoapods-1.15.2/lib/cocoapods/user_interface/error_report.rb:173:in `repo_information'
	from /Library/Ruby/Gems/2.6.0/gems/cocoapods-1.15.2/lib/cocoapods/user_interface/error_report.rb:77:in `stack'
	from /Library/Ruby/Gems/2.6.0/gems/cocoapods-1.15.2/lib/cocoapods/user_interface/error_report.rb:24:in `report'
	from /Library/Ruby/Gems/2.6.0/gems/cocoapods-1.15.2/lib/cocoapods/command.rb:66:in `report_error'
	from /Library/Ruby/Gems/2.6.0/gems/claide-1.1.0/lib/claide/command.rb:396:in `handle_exception'
	from /Library/Ruby/Gems/2.6.0/gems/claide-1.1.0/lib/claide/command.rb:337:in `rescue in run'
	from /Library/Ruby/Gems/2.6.0/gems/claide-1.1.0/lib/claide/command.rb:324:in `run'
	from /Library/Ruby/Gems/2.6.0/gems/cocoapods-1.15.2/lib/cocoapods/command.rb:52:in `run'
	from /Library/Ruby/Gems/2.6.0/gems/cocoapods-1.15.2/bin/pod:55:in `<top (required)>'
	from /usr/local/bin/pod:23:in `load'
	from /usr/local/bin/pod:23:in `<main>'
/System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require': dlopen(/Library/Ruby/Gems/2.6.0/gems/ffi-1.17.0-x86_64-darwin/lib/2.6/ffi_c.bundle, 0x0009): tried: '/Library/Ruby/Gems/2.6.0/gems/ffi-1.17.0-x86_64-darwin/lib/2.6/ffi_c.bundle' (mach-o file, but is an incompatible architecture (have 'x86_64', need 'arm64e' or 'arm64')), '/System/Volumes/Preboot/Cryptexes/OS/Library/Ruby/Gems/2.6.0/gems/ffi-1.17.0-x86_64-darwin/lib/2.6/ffi_c.bundle' (no such file), '/Library/Ruby/Gems/2.6.0/gems/ffi-1.17.0-x86_64-darwin/lib/2.6/ffi_c.bundle' (mach-o file, but is an incompatible architecture (have 'x86_64', need 'arm64e' or 'arm64')) - /Library/Ruby/Gems/2.6.0/gems/ffi-1.17.0-x86_64-darwin/lib/2.6/ffi_c.bundle (LoadError)
	from /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require'
	from /Library/Ruby/Gems/2.6.0/gems/ffi-1.17.0-x86_64-darwin/lib/ffi.rb:3:in `<top (required)>'
//〜〜〜


//gem経由でのcocoapodsのインストール方法だと上手くいかないといった記事を見かけたので、
//homebrew経由でのcocoapodsのインストール方法に変更します。
//gem経由でcocoapodsを入れている場合は、cocoapodsを削除します。
Mac-mini rakucode % sudo gem uninstall cocoapods
Password:
Remove executables:
	pod, sandbox-pod

in addition to the gem? [Yn]  Y
Removing pod
Removing sandbox-pod
Successfully uninstalled cocoapods-1.15.2


//homebrewをインストールします。
Mac-mini rakucode % /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
==> Checking for `sudo` access (which may request your password)...
==> This script will install:
/opt/homebrew/bin/brew
/opt/homebrew/share/doc/homebrew
/opt/homebrew/share/man/man1/brew.1
/opt/homebrew/share/zsh/site-functions/_brew
/opt/homebrew/etc/bash_completion.d/brew
/opt/homebrew
//〜〜〜
==> Updating Homebrew...
==> Downloading https://ghcr.io/v2/homebrew/portable-ruby/portable-ruby/blobs/sha256:1e64d7393d6bed090ebd892514457a10a2066682693eaade7d4a25568fa35312
######################################################################################################################################################################################################################################################### 100.0%
==> Pouring portable-ruby-3.3.4.arm64_big_sur.bottle.tar.gz
Warning: /opt/homebrew/bin is not in your PATH.
  Instructions on how to configure your shell for Homebrew
  can be found in the 'Next steps' section below.
==> Installation successful!

==> Homebrew has enabled anonymous aggregate formulae and cask analytics.
Read the analytics documentation (and how to opt-out) here:
  https://docs.brew.sh/Analytics
No analytics data has been sent yet (nor will any be during this install run).

==> Homebrew is run entirely by unpaid volunteers. Please consider donating:
  https://github.com/Homebrew/brew#donations

==> Next steps:
- Run these two commands in your terminal to add Homebrew to your PATH:
    (echo; echo 'eval "$(/opt/homebrew/bin/brew shellenv)"') >> /Users/takayanagiryouta/.zprofile
    eval "$(/opt/homebrew/bin/brew shellenv)"
- Run brew help to get started
- Further documentation:
    https://docs.brew.sh


//上記の文（Next steps）に記載されている通り、二つコマンドを実行します。
Mac-mini rakucode % (echo; echo 'eval "$(/opt/homebrew/bin/brew shellenv)"') >> /Users/takayanagiryouta/.zprofile  
Mac-mini rakucode % eval "$(/opt/homebrew/bin/brew shellenv)"
Mac-mini rakucode % brew install cocoapods                                                                       
Warning: Treating cocoapods as a formula. For the cask, use homebrew/cask/cocoapods or specify the `--cask` flag.
==> Downloading https://ghcr.io/v2/homebrew/core/cocoapods/manifests/1.15.2
######################################################################################################################################################################################################################################################### 100.0%
==> Fetching dependencies for cocoapods: libyaml, ca-certificates, openssl@3 and ruby
==> Downloading https://ghcr.io/v2/homebrew/core/libyaml/manifests/0.2.5
//~~~
==> Installing cocoapods
==> Pouring cocoapods--1.15.2.arm64_sonoma.bottle.tar.gz
🍺  /opt/homebrew/Cellar/cocoapods/1.15.2: 13,736 files, 28.8MB
==> Running `brew cleanup cocoapods`...
Disable this behaviour by setting HOMEBREW_NO_INSTALL_CLEANUP.
Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).


//pod installでライブラリインストールします。
//※Podfileは既に作成及び編集済みであることを前提。
Mac-mini rakucode % pod install
Analyzing dependencies
Downloading dependencies
Installing Alamofire (5.9.1)
Installing Kingfisher (7.12.0)
Installing PKHUD (5.3.0)
Installing Realm (10.52.2)
Installing RealmSwift (10.52.2)
Generating Pods project
Integrating client project

[!] Please close any current Xcode sessions and use `Rakucode.xcworkspace` for this project from now on.
Pod installation complete! There are 4 dependencies from the Podfile and 5 total pods installed.

[!] Automatically assigning platform `iOS` with version `17.2` on target `Rakucode` because no platform was specified. Please specify a platform for this target in your Podfile. See `https://guides.cocoapods.org/syntax/podfile.html#platform`.

[!] The `RakucodeUITests [Debug]` target overrides the `ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES` build setting defined in `Pods/Target Support Files/Pods-Rakucode-RakucodeUITests/Pods-Rakucode-RakucodeUITests.debug.xcconfig'. This can lead to problems with the CocoaPods installation
    - Use the `$(inherited)` flag, or
    - Remove the build settings from the target.

[!] The `RakucodeUITests [Release]` target overrides the `ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES` build setting defined in `Pods/Target Support Files/Pods-Rakucode-RakucodeUITests/Pods-Rakucode-RakucodeUITests.release.xcconfig'. This can lead to problems with the CocoaPods installation
    - Use the `$(inherited)` flag, or
    - Remove the build settings from the target.
Mac-mini rakucode % 

```
以上。

## 参考文献
- [[error report] pod install /w m1 macbook · Issue #10518](https://github.com/CocoaPods/CocoaPods/issues/10518)
- [macOS（またはLinux）用パッケージマネージャー — Homebrew](https://brew.sh/ja/)