[Mac] Homebrewで”Permission denied @ apply2files”と出た時の対処法


Macのpythonをアップグレードしたいなぁと思い、Homebrewでpyenvを入れようとした時に出たエラーの対処ログです。

## 状況

[こちらのサイト](https://jinsei9.com/2018/10/25/mac-python2-python3/)

を参考にpythonの環境を３.７にしようとしたわけです。

```
$brew install pyenv
```
で、pyenvを入れようとしましたが、、、

```
Error: Permission denied @ apply2files - /usr/local/share/ghostscript/9.16/Resource/CIDFSubst/Hiragino Sans GB W3.ttc
```
というエラーが出ました。

どうも、アクセス権限がないらしいです。

## やってみたこと①

Homebrewには、`brew doctor`という、今何がやばいかを教えてくれる便利なコマンドがあるので、それを実行します。

```
Keigo-Enomoto:~ KeigoEnomoto$ brew doctor
Please note that these warnings are just used to help the Homebrew maintainers
with debugging if you file an issue. If everything you use Homebrew for is
working fine: please don't worry or file an issue; just ignore this. Thanks!

Warning: You have unlinked kegs in your Cellar.
Leaving kegs unlinked can lead to build-trouble and cause brews that depend on
those kegs to fail to run properly once built. Run `brew link` on these:
  ghostscript

Warning: Your Xcode (10.2.1) is outdated.
Please update to Xcode 11.3.1 (or delete it).
Xcode can be updated from the App Store.


Warning: Broken symlinks were found. Remove them with `brew cleanup`:
  /usr/local/share/ghostscript/9.16/Resource/CIDFSubst/Hiragino Sans GB W3.ttc
  /usr/local/share/ghostscript/9.16/Resource/CIDFSubst/Hiragino Sans GB W6.ttc
  /usr/local/share/ghostscript/9.16/Resource/CIDFSubst/HiraginoSansR-W4.ttc
  /usr/local/share/ghostscript/9.16/Resource/CIDFSubst/HiraginoSerif-W3.ttc
  /usr/local/share/ghostscript/9.16/Resource/CIDFSubst/HiraginoSerif-W6.ttc
  /usr/local/share/ghostscript/9.16/Resource/CIDFSubst/Kaiti.ttc
  /usr/local/share/ghostscript/9.16/Resource/CIDFSubst/Klee.ttc
  /usr/local/share/ghostscript/9.16/Resource/CIDFSubst/LiHeiPro.ttf
  /usr/local/share/ghostscript/9.16/Resource/CIDFSubst/LiSongPro.ttf
  /usr/local/share/ghostscript/9.16/Resource/CIDFSubst/NanumGothic.ttc
  /usr/local/share/ghostscript/9.16/Resource/CIDFSubst/NanumMyeongjo.ttc
  /usr/local/share/ghostscript/9.16/Resource/CIDFSubst/NanumScript.ttc
  /usr/local/share/ghostscript/9.16/Resource/CIDFSubst/STFangsong.ttf
  /usr/local/share/ghostscript/9.16/Resource/CIDFSubst/STHeiti.ttf
  /usr/local/share/ghostscript/9.16/Resource/CIDFSubst/STXihei.ttf
  /usr/local/share/ghostscript/9.16/Resource/CIDFSubst/TsukushiAMaruGothic.ttc
  /usr/local/share/ghostscript/9.16/Resource/CIDFSubst/TsukushiBMaruGothic.ttc
  /usr/local/share/ghostscript/9.16/Resource/CIDFSubst/YuMincho.ttc
  /usr/local/share/ghostscript/9.16/Resource/CIDFont/HiraKakuPro-W3
  /usr/local/share/ghostscript/9.16/Resource/CIDFont/HiraKakuPro-W6
  /usr/local/share/ghostscript/9.16/Resource/CIDFont/HiraKakuProN-W3
  /usr/local/share/ghostscript/9.16/Resource/CIDFont/HiraKakuProN-W6
  /usr/local/share/ghostscript/9.16/Resource/CIDFont/HiraKakuStd-W8
  /usr/local/share/ghostscript/9.16/Resource/CIDFont/HiraKakuStdN-W8
  /usr/local/share/ghostscript/9.16/Resource/CIDFont/HiraMaruPro-W4
  /usr/local/share/ghostscript/9.16/Resource/CIDFont/HiraMaruProN-W4
  /usr/local/share/ghostscript/9.16/Resource/CIDFont/HiraMinPro-W3
  /usr/local/share/ghostscript/9.16/Resource/CIDFont/HiraMinPro-W6
  /usr/local/share/ghostscript/9.16/Resource/CIDFont/HiraMinProN-W3
  /usr/local/share/ghostscript/9.16/Resource/CIDFont/HiraMinProN-W6
  /usr/local/share/ghostscript/9.16/Resource/CIDFont/HiraginoSansGB-W3
  /usr/local/share/ghostscript/9.16/Resource/CIDFont/HiraginoSansGB-W6
  /usr/local/share/ghostscript/9.16/Resource/CIDFont/YuGo-Bold
  /usr/local/share/ghostscript/9.16/Resource/CIDFont/YuGo-Medium
  /usr/local/share/ghostscript/9.16/Resource/CIDFont/YuMin-Demibold
  /usr/local/share/ghostscript/9.16/Resource/CIDFont/YuMin-Medium
```

明らかにこの３つ目のWarningが原因っぽいですね
言われた通りに`brew cleanup`を実行します

```
Error: Permission denied @ apply2files - /usr/local/share/ghostscript/9.16/Resource/CIDFSubst/Hiragino Sans GB W3.ttc
```

###さっきと同じエラー！！！

嫌になっちゃいますね

## やってみたこと②

`ghostscript`というやつが悪いのかな、、、と思いましてアンインストールしてみました

```
$brew uninstall ghostscript
```
アンインストールは成功したっぽいですが、さっきと同じエラーが出てしまいました。

解決ならず

## ３度目の正直

色々探していると、

[Homebrewのアップデート時に出た5つのWarningと解決方法MacOSX - [Qiita]](https://qiita.com/unsoluble_sugar/items/acaffa6d0e28c3c24934)

が出てきました。

４つ目のエラーと状況が同じですね。

`$ sudo chown -R [ユーザー名]/usr/local/share/ghostscript/9.16/Resource/CIDFSubst`
と
`$ sudo chown -R [ユーザー名]/usr/local/share/ghostscript/9.16/Resource/CIDFont`

で権限を与えてみましょう

そして、再挑戦っ！！！

```
Keigo-Enomoto:~ KeigoEnomoto$ brew cleanup
```

その結果は、、、、、

```
Pruned 36 symbolic links and 2 directories from /usr/local
```

#### うまくpruneできました！！

(刈り込むとか、剪定っていう意味らしいです、というか初めて知った)


もう一度、お医者さんに診てもらいましょう

```
Keigo-Enomoto:~ KeigoEnomoto$ brew doctor
Please note that these warnings are just used to help the Homebrew maintainers
with debugging if you file an issue. If everything you use Homebrew for is
working fine: please don't worry or file an issue; just ignore this. Thanks!

Warning: Your Xcode (10.2.1) is outdated.
Please update to Xcode 11.3.1 (or delete it).
Xcode can be updated from the App Store.

```

ちゃんと、消えてるっぽいですね！！

そのあと、うまくインストールできましたとさ

めでたし、めでたし。