問題描述
當執行 brew 命令時,產生如下錯誤:
# brew doctor ==> Downloading https://homebrew.bintray.com/bottles-portable-ruby/portable-ruby-2.6.3_2.yosemite.bottle.tar.gz Already downloaded: /Users/pengfeiwang/Library/Caches/Homebrew/portable-ruby-2.6.3_2.yosemite.bottle.tar.gz ==> Pouring portable-ruby-2.6.3_2.yosemite.bottle.tar.gz portable-ruby/2.6.3_2/: Can't create 'portable-ruby/2.6.3_2' portable-ruby/2.6.3_2/.brew/: Failed to create dir 'portable-ruby/2.6.3_2' .... portable-ruby/2.6.3_2/bin/rdoc: Failed to create dir 'portable-ruby/2.6.3_2' portable-ruby/2.6.3_2/bin/ri: Failed to create dir 'portable-ruby/2.6.3_2' portable-ruby/2.6.3_2/bin/ruby: Failed to create dir 'portable-ruby/2.6.3_2' portable-ruby/2.6.3_2/.brew/portable-ruby.rb: Failed to create dir 'portable-ruby/2.6.3_2' tar: Error exit delayed from previous errors. Error: Failed to install ruby 2.6.3_2! Error: Failed to upgrade Homebrew Portable Ruby!
該筆記將記錄:如何解決該問題,以及注意事項。
問題原因
在 Google 后,我們發現導致該問題的原因有很多,但是究其根本,正如錯誤信息的提示:Failed to create dir 'portable-ruby/2.6.3_2'。因此,我們嘗試查找 portable-ruby/2.6.3_2 無法寫入的原因。
Homebrew 的安裝目錄為 /usr/local/Homebrew ,而 portable-ruby/ 在 /usr/local/Homebrew/Library/Homebrew/vendor/ 中,我們發現 vendor 目錄屬於其他用戶。在我們的場景中,該主機的其他用戶以前已經安裝過 Homebrew 工具。當我們進行安裝時,由於權限問題導致安裝失敗。
解決方案
針對我們的問題,修改 /user/local/Homebrew 權限即可。但是我們發現 /usr/local 的很多文件都屬於其他用戶,所以我們進行全部修改:
# sudo chown -R -v $(whoami): /usr/local/*
將 /usr/local 下的全部文件都修改為當前用戶和當前用戶所屬的組。
參考文獻
Failed to install Homebrew Portable Ruby (and your system version is too old) - Stack Overflow