Installing and Using Homebrew

https://en.wikipedia.org/wiki/Homebrew_(package_manager)

Installing

Run the following to install Homebrew:

sh -c "$(curl -fsSL https://raw.githubusercontent.com/Linuxbrew/install/master/install.sh)"

Next, add Homebrew to your PATH by running the following commands.

These commands work on all major flavors of Linux by adding either `/.profile` on Debian/Ubuntu or `/.bash_profile` on CentOS/Fedora/RedHat:

test -d ~/.linuxbrew && eval $(~/.linuxbrew/bin/brew shellenv)
test -d /home/linuxbrew/.linuxbrew && eval $(/home/linuxbrew/.linuxbrew/bin/brew shellenv)
test -r ~/.bash_profile && echo "eval \$($(brew --prefix)/bin/brew shellenv)" >>~/.bash_profile
echo "eval \$($(brew --prefix)/bin/brew shellenv)" >>~/.profile

Verify that Homebrew is installed

brew --version

List installed packages

brew list

Where does Homebrew install packages?

The easiest way (regardless of whether it is Linux or MacOS) is to list the existing packages and then use that information to see where they are being installed.

Pick a package from the installed list and run the info command

e.g.

[explorer436@explorer436-p50-20eqs27p03 programming-notes]$ brew info localstack-cli
==> localstack/tap/localstack-cli: stable 64
Localstack cli packaged using pyinstaller
https://github.com/localstack/localstack-cli
/home/linuxbrew/.linuxbrew/Cellar/localstack-cli/64 (94 files, 68.4MB) *
  Built from source on 2023-09-25 at 19:04:40
From: https://github.com/localstack/homebrew-tap/blob/HEAD/Formula/localstack-cli.rb
License: NOASSERTION

In Linux

Notice: It is not /home/<username>/linuxbrew

/home/linuxbrew/.linuxbrew

https://docs.brew.sh/Homebrew-on-Linux

In Mac

https://mkyong.com/mac/where-does-homebrew-install-packages-on-mac/

By default, Homebrew will install all packages in the directory /usr/local/Cellar/, and also creates symbolic links at /usr/local/opt/ and /usr/local/bin/ (for executable files).

To find out the details about where a specific package is installed, use this:

brew info [package]

We can use brew info [package] to show detail about a specified installed package.

brew info eclipse-java

Uninstalling packages

Find the command that installed the package and change the word install to uninstall

e.g.

brew install --cask diffmerge

becomes

brew uninstall --cask diffmerge

How to use Homebrew to install local archive

https://apple.stackexchange.com/questions/84403/how-to-use-homebrew-to-install-local-archive

Sometimes, due to access issues or VPN or proxy issues, it may not be possible to download the tar files using Homebrew. In that case, we can ask someone on the team to send the tar file to us. If getting the tar file, either from a browser, or from someone on the team is possible, we can try to install the tar file.

If Homebrew already has a formula for it, and you have the exact archive that the formula expects (i.e. same file name and same contents, as determined by SHA1 or whatever hash type the formula uses), you can put the archive in ~/Library/Caches/Homebrew and then try brew install formula_name (substituting the correct name for formula_name, of course).

How to determine if Homebrew already has a formula for it?

Search for it. And get the exact formula name.

[explorer436@explorer436-p50-20eqs27p03 05mydatastructures]$ brew search --formula localstack
==> Formulae
localstack                                                         localstack/tap/localstack-cli ✔

To see the formula

[explorer436@explorer436-p50-20eqs27p03 05mydatastructures]$ brew edit localstack/tap/localstack-cli
Warning: edit is a developer command, so Homebrew's
developer mode has been automatically turned on.
To turn developer mode off, run:
  brew developer off

Editing /home/linuxbrew/.linuxbrew/Homebrew/Library/Taps/localstack/homebrew-tap/Formula/localstack-cli.rb

If you want to open the file in some other editor (not in the default editor), you need to know where the file is. Looking at the output of this command will tell us where the file is.

Name and location of the tar file that the formula uses for installation

If the formula already exists in Homebrew, we can find out the name of the file that it expects:

brew --cache -s <formula>

e.g.

[explorer436@explorer436-p50-20eqs27p03 05mydatastructures]$ brew --cache -s localstack/tap/localstack-cli
/home/explorer436/.cache/Homebrew/downloads/fb577b72f14489649c3df1c1953d94995edc60844c2a2d160dde31b0a35caf7d--localstack-cli-2.2.0-linux-amd64.tar.gz

Grab this name and rename the manually downloaded tar file. Copy the manually downloaded tar file to this location.

Installation failed because of SHA1 mismatch

If you see an error like this when installing a package:

Error: SHA1 mismatch
Expected: 5ef03ca7aee134fe7dfecb6c9d048799f0810278
Actual: 5900fdfc7894c52ce7a3ab7ea6ebd29af22f0b70
Archive: /Library/Caches/Homebrew/libmpc08-0.8.1.tar.gz

Edit the formula (brew edit <formula-name>) and insert the Actual SHA in the formula.

No available formula with the name

https://superuser.com/questions/1762011/no-available-formula-with-the-name-testdisk-when-using-homebrew-in-macos

Check brew doctor for updates and errors.

When you run it, the output from the command should help us figure out what is wrong. If something is wrong with a package, deleting it altogether and then trying to install it again helps.

Updating formulae

  1. Update the local formulas.

    https://docs.brew.sh/Manpage#update-options

    brew update
    
  2. Upgrade all outdated formulae.

    brew upgrade
    

    or

    brew install `brew outdated`
    
  3. To upgrade only specific mongodb formula (e.g. mongodb) , use install.

    brew install <formula-name> && brew cleanup <formula-name>
    

    or

    brew install mongodb
    

Links to this note