'How to make Brew show the size of the formula before installing it?

I'm looking for a way to make brew show the size of a formula without installing it. I've tried various options to brew info and brew install, but neither let me view the size of the formula.

However, when installing a formula, its size is shown as part of the output.

Any ideas?

nlykkei-mbp:Projects nlykkei$ brew info llvm
llvm: stable 8.0.1 (bottled), HEAD [keg-only]
Next-gen compiler infrastructure
https://llvm.org/
Not installed
From: https://github.com/Homebrew/homebrew-core/blob/master/Formula/llvm.rb
==> Dependencies
Build: cmake ✔
Required: libffi ✔, swig ✔
==> Requirements
Build: xcode ✔
==> Options
--HEAD
        Install HEAD version
==> Caveats
To use the bundled libc++ please add the following LDFLAGS:
  LDFLAGS="-L/usr/local/opt/llvm/lib -Wl,-rpath,/usr/local/opt/llvm/lib"

llvm is keg-only, which means it was not symlinked into /usr/local,
because macOS already provides this software and installing another version in
parallel can cause all kinds of trouble.

==> Analytics
install: 15,199 (30 days), 39,871 (90 days), 183,880 (365 days)
install_on_request: 11,125 (30 days), 30,095 (90 days), 140,885 (365 days)
build_error: 0 (30 days)


Solution 1:[1]

There are two issues here:

  1. If a formula doesn’t have a bottle (= pre-built archive) you can’t know its size without installing it.
  2. If a formula has a bottle, you need to download it to know its compressed size

I’m skipping the first issue because it has no solution for your case. Regarding the second one, we can get the URL of a bottle using Homebrew’s JSON API and jq:

$ brew info --json=v1 llvm | jq --raw-output '.[0].bottle.stable.files.big_sur.url'
https://ghcr.io/v2/homebrew/core/llvm/blobs/sha256:ff9a71b7b35ecb6c1dfcfe40152b00f4777a3f4a10dcf5cc41044458b02c99cd

Note: because this URL is very long, I’m truncating it in the commands below so they are easier to read.

We can curl this URL to know the compressed size. Note that since April 2021, ghcr.io require authentification, but using a dummy token works. We use -I to perform a HEAD request and get the response headers without its body (-L = follow redirects; -s don’t show progress).

$ curl -Ls -I -H 'Authorization: Bearer QQ==' https://ghcr.io/v2/homebrew/... | grep -i content-length
content-length: 474762229

The gzip-compressed size of this formula is 474,762,229 bytes, i.e. ~474MiB.

gzip’d archives don’t contain their final size; we must uncompress them to know it.

We can stream the archive through gunzip and get its size without storing anything on the disk:

$ curl -Ls -H 'Authorization: Bearer QQ==' https://ghcr.io/v2/homebrew/... | gunzip - | wc -c
1695293440

The uncompressed llvm formula weights 1,695,293,440 bytes, i.e. ~1.7GiB.

Unfortunately this can take some time depending on your connection and the bottle’s size.

One-liner:

brew info --json=v1 llvm | jq -r '.[0].bottle.stable.files.big_sur.url' | xargs curl -Ls -H 'Authorization: Bearer QQ==' | gunzip - | wc -c

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1