brew: brew upgrade error

Verification

Description of issue

brew upgrade error

Command that failed

brew upgrade

Output of command with --verbose --debug

--remote-time --output /Users/jeremywang/Library/Caches/Homebrew/api/formula.json --compressed --silent https://formulae.brew.sh/api/formula.json
Warning: formula.json: update failed, falling back to cached version.
/opt/homebrew/Library/Homebrew/shims/shared/curl --disable --cookie /dev/null --globoff --user-agent Homebrew/3.6.20-261-gb3a71ba\ \(Macintosh\;\ arm64\ Mac\ OS\ X\ 13.2\)\ curl/7.86.0 --header Accept-Language:\ en --fail --max-time 10 --location --remote-time --output /Users/jeremywang/Library/Caches/Homebrew/api/formula.json --compressed --silent https://formulae.brew.sh/api/formula.json
Warning: formula.json: update failed, falling back to cached version.
/opt/homebrew/Library/Homebrew/shims/shared/curl --disable --cookie /dev/null --globoff --user-agent Homebrew/3.6.20-261-gb3a71ba\ \(Macintosh\;\ arm64\ Mac\ OS\ X\ 13.2\)\ curl/7.86.0 --header Accept-Language:\ en --fail --max-time 10 --location --remote-time --output /Users/jeremywang/Library/Caches/Homebrew/api/formula.json --compressed --silent https://formulae.brew.sh/api/formula.json
Warning: formula.json: update failed, falling back to cached version.
/opt/homebrew/Library/Homebrew/shims/shared/curl --disable --cookie /dev/null --globoff --user-agent Homebrew/3.6.20-261-gb3a71ba\ \(Macintosh\;\ arm64\ Mac\ OS\ X\ 13.2\)\ curl/7.86.0 --header Accept-Language:\ en --fail --max-time 10 --location --remote-time --output /Users/jeremywang/Library/Caches/Homebrew/api/formula.json --compressed --silent https://formulae.brew.sh/api/formula.json
Warning: formula.json: update failed, falling back to cached version.
Error: Cannot download non-corrupt https://formulae.brew.sh/api/formula.json!
Error: Kernel.exit
/opt/homebrew/Library/Homebrew/utils.rb:165:in `exit'
/opt/homebrew/Library/Homebrew/utils.rb:165:in `odie'
/opt/homebrew/Library/Homebrew/api.rb:77:in `rescue in fetch_json_api_file'
/opt/homebrew/Library/Homebrew/api.rb:51:in `fetch_json_api_file'
/opt/homebrew/Library/Homebrew/api/formula.rb:21:in `all_formulae'
/opt/homebrew/Library/Homebrew/formulary.rb:667:in `loader_for'
/opt/homebrew/Library/Homebrew/formulary.rb:561:in `factory'
/opt/homebrew/Library/Homebrew/formulary.rb:609:in `from_keg'
/opt/homebrew/Library/Homebrew/formulary.rb:582:in `from_rack'
/opt/homebrew/Library/Homebrew/formula.rb:1870:in `block in installed'
/opt/homebrew/Library/Homebrew/formula.rb:1869:in `each'
/opt/homebrew/Library/Homebrew/formula.rb:1869:in `flat_map'
/opt/homebrew/Library/Homebrew/formula.rb:1869:in `installed'
/opt/homebrew/Library/Homebrew/cmd/upgrade.rb:135:in `upgrade_outdated_formulae'
/opt/homebrew/Library/Homebrew/cmd/upgrade.rb:116:in `upgrade'
/opt/homebrew/Library/Homebrew/brew.rb:105:in `<main>'

Output of brew doctor and brew config

Warning: formula.json: update failed, falling back to cached version.
Warning: formula.json: update failed, falling back to cached version.
Warning: formula.json: update failed, falling back to cached version.
Warning: formula.json: update failed, falling back to cached version.
Error: Cannot download non-corrupt https://formulae.brew.sh/api/formula.json!

Output of brew tap

buo/cask-upgrade
homebrew/bundle
homebrew/cask
homebrew/cask-drivers
homebrew/cask-fonts
homebrew/cask-versions
homebrew/command-not-found
homebrew/core
majd/repo

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Comments: 32 (14 by maintainers)

Commits related to this issue

Most upvoted comments

@Kotodian, try doing

export HOMEBREW_NO_INSTALL_FROM_API=1

and see if that helps.

@Kotodian, try doing

export HOMEBREW_NO_INSTALL_FROM_API=1

and see if that helps.

Same problem here. I followed each steps stated in this issue and finally it works. Thank you very much.

@Kotodian, please try

rm -r "$(brew --cache)/api"
brew update

and see if it helps.

I have the same error

$ brew config
HOMEBREW_VERSION: 3.6.20-261-gb3a71ba
ORIGIN: https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git
HEAD: b3a71ba7cdb7e005c26c86bcc627f0549dfe19d0
Last commit: 12 hours ago
Core tap origin: https://github.com/Homebrew/homebrew-core
Core tap HEAD: 15cc58f490ef218e460352ccd00ebf79f35b2491
Core tap last commit: 4 hours ago
Core tap branch: master
HOMEBREW_PREFIX: /opt/homebrew
HOMEBREW_BOTTLE_DOMAIN: https://mirrors.ustc.edu.cn/homebrew-bottles
HOMEBREW_CASK_OPTS: []
HOMEBREW_MAKE_JOBS: 8
Homebrew Ruby: 2.6.10 => /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/bin/ruby
CPU: octa-core 64-bit arm_firestorm_icestorm
Clang: 14.0.0 build 1400
Git: 2.37.1 => /Library/Developer/CommandLineTools/usr/bin/git
Curl: 7.85.0 => /usr/bin/curl
macOS: 13.1-arm64
CLT: 14.2.0.0.1.1668646533
Xcode: N/A
Rosetta 2: false

$ 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: Suspicious https://github.com/Homebrew/brew git origin remote found.
The current git origin is:
  https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git

With a non-standard origin, Homebrew won't update properly.
You can solve this by setting the origin remote:
  git -C "/opt/homebrew" remote set-url origin https://github.com/Homebrew/brew

Warning: Suspicious https://github.com/Homebrew/homebrew-core git origin remote found.
The current git origin is:
  https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git

With a non-standard origin, Homebrew won't update properly.
You can solve this by setting the origin remote:
  git -C "/opt/homebrew/Library/Taps/homebrew/homebrew-core" remote set-url origin https://github.com/Homebrew/homebrew-core
Warning: formula.json: update failed, falling back to cached version.

 $ curl -LI https://formulae.brew.sh/api/formula.json
HTTP/1.1 200 Connection established

HTTP/2 200
server: GitHub.com
content-type: application/json; charset=utf-8
x-origin-cache: HIT
last-modified: Mon, 06 Feb 2023 05:21:13 GMT
access-control-allow-origin: *
etag: "63e08e49-129b66d"
expires: Mon, 06 Feb 2023 05:34:18 GMT
cache-control: max-age=600
x-proxy-cache: MISS
x-github-request-id: EDB0:2952:110FC2:1233C7:63E08F02
accept-ranges: bytes
age: 1
date: Mon, 06 Feb 2023 05:24:19 GMT
via: 1.1 varnish
x-served-by: cache-tyo11942-TYO
x-cache: MISS
x-cache-hits: 0
x-timer: S1675661058.074362,VS0,VE973
vary: Accept-Encoding
x-fastly-request-id: cb9d0a76f8e4c0e4e27e70e40b4d6184b109f63c
content-length: 19510893

While export HOMEBREW_NO_INSTALL_FROM_API=1 does temporarily fix the issue, it only seems to mask the real problem. Or at least that’s what was happening to me. Here is what I found:


The Error

brew update fails to download formula.json due to a Permission denied error:

$ brew update
Error: Failed to download https://formulae.brew.sh/api/formula.json!
==> Downloading https://formulae.brew.sh/api/formula.json
Warning: Failed to open the file
Warning: /Users/scott/Library/Caches/Homebrew/api/formula.json: Permission
Warning: denied
curl: (23) Failure writing output to destination

Warning: formula.json: update failed, falling back to cached version.
Error: Permission denied @ apply2files - /Users/scott/Library/Caches/Homebrew/api/formula.json

The Cause

Three of the files in ~/Library/Caches/Homebrew/api/ (including formula.json, specifically) somehow became owned by root:

$ ls -lah ~/Library/Caches/Homebrew/api/
Permissions Size User  Date Modified Name
.rw-r--r--@ 4.5M scott 22 Feb 21:06  cask.json
.rw-r--r--@  20M root  21 Feb 10:31  formula.json
.rw-r--r--@ 5.3k root  21 Feb 10:31  formula_aliases.txt
.rw-r--r--@  57k root  21 Feb 10:31  formula_names.txt

The Fix

Change the ownership of the files back to your user:

sudo chown -R $(whoami) ~/Library/Caches/Homebrew/api/

Verify ownership is fixed:

$ ls -lah ~/Library/Caches/Homebrew/api/
Permissions Size User  Date Modified Name
.rw-r--r--@ 4.5M scott 22 Feb 21:06  cask.json
.rw-r--r--@  20M scott 21 Feb 10:31  formula.json
.rw-r--r--@ 5.3k scott 21 Feb 10:31  formula_aliases.txt
.rw-r--r--@  57k scott 21 Feb 10:31  formula_names.txt

And now brew update should work great:

$ brew update
Updated 2 taps (homebrew/core and homebrew/cask).
...

rm -r "$(brew --cache)/api" && brew update does nothing

export HOMEBREW_NO_INSTALL_FROM_API=1 fixes the issue, on my computer at least

Edit: I’m seriously bad at markdown formatting

Did you run sudo brew services... at some point?

That’s a fair shout actually. Will look into that.

HOMEBREW_NO_INSTALL_FROM_API is the default for now. There is no need to set it once you’ve done brew update.

What’s the output of

brew config
brew doctor
curl -LI https://formulae.brew.sh/api/formula.json

?

这是来自QQ邮箱的假期自动回复邮件。   您好,我最近正在休假中,无法亲自回复您的邮件。我将在假期结束后,尽快给您回复。

@carlocab this is related to a different issue (it was opened before the GitHub issue started), so I’m not sure it’s been fixed yet.