By Mohamad Wael

Posted :

Brew tutorial

Brew is used to install applications for macOS. The kind of applications that it can be used to install, are Unix, and GUI.app applications. An example of a Unix application is nginx, or any other Linux, or BSD application. An example of a GUI.app macOS application, is google chrome, or any application, in the Applications folder.

Having defined what brew is, the question to ask is, where can it be installed. Basically brew officially supports, only the latest three versions of macOS, so the macOS versions, which are officially supported by apple.

For macOS versions starting 10.10, which is macOS Yosemite, you can also use brew, but this is only supported on best efforts. For PowerPC Macs, or macs with tiger, which is 10.4, or leopard, which is 10.5, you can use Tigerbrew. Additionally, brew can be used on Linux.

Brew prides itself, on not using sudo , in other words, it does not need to gain administrator privileges, to install application. Using sudo is actually still needed, the first time you install brew, because brew creates certain directories, and this action does require having administrator privileges. So what is so special about not using sudo ? Well it is promoted that using sudo, can be a security hazard.

To install brew on macOS, first you need to make sure, that you have xcode, and the Xcode command line tools, installed. Xcode can be downloaded from the apple store, and the Xcode command line tools, can be installed by running from the command Line: xcode-select --install.

Once this is done, you just have to run this script, which is found on the brew installation page:

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

Before jumping on how to use brew, there are certain terms that you need to be aware of, mainly these are brewery terms, which are applied to brew. Brew is also called homebrew.

Brewery terms a Cellar a Cellar is where your Unix applications are going to be installed: /usr/local/Cellar. a Rack a Rack, is your Unix application folder: /usr/local/Cellar/app . a Keg a Keg, is a Unix application version: /usr/local/Cellar/app/0.1. an Opt Prefix an Opt Prefix, is the folder, where the Unix applications that you have installed, will have a symlink, a shortcut if you want, so that they are part of your path, as such they can be accessed from the command line. /usr/local/bin/app a Keg Only a Keg Only, is a Unix applications which will not be added to your path, so it will not have a symlink in the opt prefix folder. a Tap a Tap, as in tapping into something, so as in adding sources of formulas, or casks. a Formula a Formula is a recipe for installing a Unix application. a Bottle a Bottle is, instead of having to compile a formula from source, it is precompiled with certain default options, for you, and you just install the formula, without having to wait compilation time. a cask a Cask, is a recipe, for installing GUI.app, macOS applications. a Caskroom a Caskroom, is going to contain the casks that you have installed, so some metadata, and their installation recipe, and not the actual GUI application. The actual GUI application is installed into your Applications folder. a Brew Bundle a Brew Bundle, is just a Brewfile, to automate applications installation. It contains brew commands that you want to be performed.

Having finished with defining some key terms, brew actually does collect some anonymous analytics, so if you want to check the state of the analytics, or you want to turn them on or off, you can use:

$ brew analytics
# Tells if analytics is On
# or Off, while displaying
# your analytics UUID.
Analytics are enabled.
UUID: F70BD8F7-9892-4265-B3BB-2674624D8A22

$ brew analytics on #off
# Turn on or off analytics.
# brew analytics on, will
# turn it on, and brew analytics
# off, will turn it off.

brew commands, can be used to get a list of brew commands, that can be executed.

$ brew commands
# List available brew
# built-in and external
# commands.

==> Built-in commands
--cache         commands        home            pin             untap
--caskroom      completions     info            postinstall     update-report
--cellar        config          install         readall         update-reset
--env           deps            leaves          reinstall       update
--prefix        desc            link            search          upgrade
--repository    developer       list            shellenv        uses
--version       doctor          log             tap-info        vendor-install
analytics       fetch           migrate         tap
autoremove      formulae        missing         uninstall
casks           gist-logs       options         unlink
cleanup         help            outdated        unpin

==> Built-in developer commands
audit                      generate-man-completions   sponsors
bottle                     install-bundler-gems       style
bump-cask-pr               irb                        tap-new
bump-formula-pr            linkage                    test
bump-revision              livecheck                  tests
bump-unversioned-casks     pr-automerge               typecheck
bump                       pr-publish                 unbottled
cat                        pr-pull                    unpack
command                    pr-upload                  update-license-data
create                     prof                       update-maintainers
dispatch-build-bottle      release                    update-python-resources
edit                       rubocop                    update-test
extract                    ruby                       vendor-gems
formula                    sh

==> External commands
aspell-dictionaries                      postgresql-upgrade-database
determine-rebottle-runners

You just need to be aware at this stage, that in brew, you have what is called built-in, and external commands . So built-in commands, are commands which are built into brew, and external commands, are just scripts that you can execute. Some of the external commands, are considered official brew commands.

brew help command_name, can be used to get help, about a specific command.

$ brew help analytics
# Get help, about the
# analytics command.

Usage: brew analytics [subcommand]

Control Homebrew s anonymous aggregate user behaviour analytics. Read more at
https://docs.brew.sh/Analytics.

brew analytics [state]:
    Display the current state of Homebrew s analytics.

brew analytics (on|off):
    Turn Homebrew s analytics on or off respectively.

brew analytics regenerate-uuid:
    Regenerate the UUID used for Homebrew s analytics.

  -d, --debug                      Display any debugging information.
  -q, --quiet                      Make some output more quiet.
  -v, --verbose                    Make some output more verbose.
  -h, --help                       Show this message.

brew doctor, can be used to check for possible problems, with your brew installation.

$ brew doctor
# Check for possible problems
# with brew installation.

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 (11.3.1) is outdated.
Please update to Xcode 12.4 (or delete it).
Xcode can be updated from the App Store.

brew --config, can be used to view, how brew is configured, on your system.

$ brew --config
# View brew configuration

HOMEBREW_VERSION: 3.3.11
ORIGIN: https://github.com/Homebrew/brew
HEAD: 537036ab3627b65d71dfa920b98d43774f861dec
Last commit: 3 days ago
Core tap ORIGIN: https://github.com/Homebrew/homebrew-core
Core tap HEAD: d4f782b5784689460407517b227857b3c8fdac20
Core tap last commit: 14 hours ago
Core tap branch: master
HOMEBREW_PREFIX: /usr/local
HOMEBREW_CASK_OPTS: []
HOMEBREW_CORE_GIT_REMOTE: https://github.com/Homebrew/homebrew-core
HOMEBREW_MAKE_JOBS: 2
Homebrew Ruby: 2.6.8 => /usr/local/Homebrew/Library/Homebrew/vendor/portable-ruby/2.6.8/bin/ruby
CPU: dual-core 64-bit haswell
Clang: 11.0.0 build 1100
Git: 2.21.1 => /Applications/Xcode.app/Contents/Developer/usr/bin/git
Curl: 7.64.1 => /usr/bin/curl
macOS: 10.15.7-x86_64
CLT: 12.4.0.0.1.1610135815
Xcode: 11.3.1

brew shellenv , can be used, to output a list of exports, which defines environment variables, which can be added to any of, ~/.profile, ~/.bash_profile, or ~/.zprofile , by inserting into any of them: eval "$(brew shellenv)".

$ brew shellenv
# List environment variables,
# related to brew, and which
# can be added, to your profile.
export HOMEBREW_PREFIX="/usr/local";
export HOMEBREW_CELLAR="/usr/local/Cellar";
export HOMEBREW_REPOSITORY="/usr/local/Homebrew";
export PATH="/usr/local/bin:/usr/local/sbin${PATH+:$PATH}";
export MANPATH="/usr/local/share/man${MANPATH+:$MANPATH}:";
export INFOPATH="/usr/local/share/info:${INFOPATH:-}";

As explained earlier, brew has certain directories that it uses, the previous command does reveal some, additionally, to get the cash, caskroom, and cellar directories, the following commands can be used:

$ brew --cache
# Path to where things
# get downloaded.
/Users/username/Library/Caches/Homebrew

$ brew --caskroom
# Path to caskroom, the
# directory containing
# metadata about your
# installed casks.
/usr/local/Caskroom

$ brew --cellar
# Tells where your cellar is.
# A cellar is where Unix
# applications are installed.
/usr/local/Cellar

brew --env, can be used to get a summary, of your build environment.

$ brew --env
# Get a summary of your
# build environment
HOMEBREW_CC: clang
HOMEBREW_CXX: clang++
MAKEFLAGS: -j2
CMAKE_PREFIX_PATH: /usr/local
CMAKE_INCLUDE_PATH: /Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/System/Library/Frameworks/OpenGL.framework/Versions/Current/Headers
CMAKE_LIBRARY_PATH: /Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/System/Library/Frameworks/OpenGL.framework/Versions/Current/Libraries
PKG_CONFIG_LIBDIR: /usr/lib/pkgconfig:/usr/local/Homebrew/Library/Homebrew/os/mac/pkgconfig/10.15
HOMEBREW_GIT: git
HOMEBREW_SDKROOT: /Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk
PATH: /usr/local/Homebrew/Library/Homebrew/shims/mac/super:/usr/bin:/bin:/usr/sbin:/sbin

You have tapped into certain repositories, in order to be able to install formulas, or casks. A repository is basically just a Git repo, which is being synced locally into your home machine. To get the path, of where the repositories that you have tapped into, are syncing, you can use the command:

$ brew --repository user/repo
# user, is a git username, as
# in homebrew.
# repo, is a brew repository by
# this user, as in
# homebrew-core.
# https://github.com/Homebrew/homebrew-core

$ brew --repository homebrew/homebrew-core
# Output the path, where the
# homebrew-core repo, is being
# synced.
/usr/local/Homebrew/Library/Taps/homebrew/homebrew-core

$ brew --repository homebrew/homebrew-cask
# Output the path, where the
# homebrew-cask repo, is being
# synced.
/usr/local/Homebrew/Library/Taps/homebrew/homebrew-cask

For where brew itself, is being synced with its git repo, you can use:

$  brew --repository
/usr/local/Homebrew

brew update, can be used to update, brew itself, and all the available formulas, and casks.

$  brew update
# Update homebrew, and the
# available formulas, and
# casks.

Updated 2 taps (homebrew/core and homebrew/cask).

==> New Formulae
elixir-ls                                juliaup

==> Updated Formulae
aerc                       keptn
aws-sso-util               libwebsockets

==> New Casks
surge-xt                                 the-watcher

==> Updated Casks
aerial                                   photoninja
atext                                    projector

==> Deleted Casks
doomrl                                   residualvm
meshmixer                                sql-power-architect-jdbc

brew tap, can be used to list the repositories, that you have tapped into.

$ brew tap
# List the repositories of
# formulas, and casks, that
# you have tapped into.
homebrew/cask
homebrew/core
name/myrepo

brew tap, can also be used, to add repositories, of formulas, or casks.

$ brew tap homebrew/homebrew-cask
# Add a Git repository as
# a tap or a source  of casks.
# The username of the Git repo
# is homebrew, and the repo is
# called homebrew-cask.
# https://github.com/Homebrew/homebrew-core

$ brew tap homebrew/cask
# You can leave out the prefix
# homebrew-, from the repository
# name, it is automatically
# appended.
# This command is equivalent to:
# brew tap homebrew/homebrew-cask

$ brew tap homebrew/cask-versions
Running `brew update --preinstall`...
==> Tapping homebrew/cask-versions
Cloning into '/usr/local/Homebrew/Library/Taps/homebrew/homebrew-cask-versions'...
remote: Enumerating objects: 242831, done.
remote: Counting objects: 100% (25/25), done.
remote: Compressing objects: 100% (17/17), done.
remote: Total 242831 (delta 12), reused 19 (delta 8), pack-reused 242806
Receiving objects: 100% (242831/242831), 62.08 MiB | 288.00 KiB/s, done.
Resolving deltas: 100% (167801/167801), done.
Tapped 211 casks (241 files, 68.9MB).

$ brew tap user/repo URL
# The tap source is a Git
# repository, present at
# the provided URL, with the
# given user and repo name.

brew untap, can be used, to remove a repository, containing formulas or casks.

$ brew untap homebrew/cask-versions
# Untap from the cask-versions
# repository, having a
# username of homebrew.
Untapping homebrew/cask-versions...
Untapped 211 casks (241 files, 68.9MB).

brew list, can be used to view, the installed formulas, and casks.

$ brew list
# List the installed
# formulas, and casks.
==> Formulae
ca-certificates	nginx		openssl@1.1	pcre

==> Casks
google-chrome

At this stage, if you want to list all locally available, to install formulas, you can use: brew formulae, and if you wish, to list all the locally available to install casks, you can use: brew casks, and to search for a formula, or a cask, you can use: brew search (name_any|/regex/).

brew fetch, can be used to download, or cache, ready to install binaries for formulas, if available, or their source packages, so that formulas can be compiled from source. Additionally it can be used, to download, cask binaries.

$ brew fetch nginx
# Download nginx, ready to
# install binaries if available,
# otherwise, fetch the source
# packages, so that nginx can be
# compiled.

==> Downloading https://ghcr.io/v2/homebrew/core/nginx/manifests/1.21.5
######################################################################## 100.0%
==> Downloading https://ghcr.io/v2/homebrew/core/nginx/blobs/sha256:2f90927e81f6
==> Downloading from https://pkg-containers.githubusercontent.com/ghcr1/blobs/sh
######################################################################## 100.0%
Downloaded to: /Users/df/Library/Caches/Homebrew/downloads/3a88717048ab239ff584251e7699ee2d17d2e5add763c16005b8f07f9ce86f83--nginx--1.21.5.catalina.bottle.tar.gz
SHA256: 2f90927e81f6beeb764f997afd48eb60af07aa53d5d58e93a5b17fb1b174e270

brew install, can be used to install, or upgrade a formula, or a cask. It will try to install a precompiled binary package, if available, otherwise, it will compile the formula from source. Additionally, for third party formulas, options, that a formula is to be compiled with, can be passed.

$ brew install nginx
# Install or upgrade nginx,
# from the
# homebrew/core repo

$ brew install username/repo/nginx
# Install or upgrade nginx,
# from the
# username/repo repo

$ brew install username/repo/nginx --with-modsecurity
# Install or upgrade nginx,
# from a third party repo,
# passing in, compilation
# options.

$ brew install tree rmate
# Install or upgrade,
# tree, and rmate.

brew options, can be used, to view installation options, for a formula.

$ brew options python
# View the installation
# options, for the python
# formula.

To get a description of a formula, or to go to the homepage, of a formula, or a cask, you can use:

$ brew desc nginx
# Get a description, of a
# formula.
nginx: HTTP(S) server and reverse proxy, and IMAP/POP3 proxy server

$ brew homepage google-chrome
# Open the homepage, of
# a formula, or a cask.

brew deps , can be used to show the requirements, for a formula, or a cask.

$ brew deps nginx
# Show the dependencies of
# the nginx formula.
ca-certificates
openssl@1.1
pcre

$ brew deps nginx --tree
# Show the requirements of
# the nginx formula in a
# tree format.
nginx
├── openssl@1.1
│   └── ca-certificates
└── pcre

Having installed a daemon application, for example nginx, you can use the brew service command, to manage this background app, as in for example, to run, start, stop, and restart it.

# [] means optional.

$ [sudo] brew services run formula
# Run a daemon application
# in the background.
# For example, to run nginx,
# as the current user:
# brew services run nginx
# And to run nginx, as the root
# user:
# sudo brew services run nginx

$ [sudo] brew services start formula
# Run a daemon application,
# and register it to start,
# at login.

$ [sudo] brew services stop formula
# Stop a daemon application,
# additionally, stop it,
# from running at login.

$ [sudo] brew services restart formula
# Stop a daemon app if
# running, next, start it,
# and register it to run,
# at login.

$ [sudo] brew services
# Show the managed services,
# for the current user, or
# root.

$ [sudo] brew services cleanup
# Remove, all of the unused
# services.

brew outdated, can be used to view the formulas, or casks, which have newer versions.

$ brew outdated
# List the outdated,
# formulas and casks.

brew upgrade, can be used to upgrade all outdated casks, and formulas, or specific ones.

$ brew upgrade
# Upgrade all outdated
# casks, and formulas.

$ brew upgrade google-chrome
# upgrade google chrome.

brew pin, and brew unpin, can be used, to forbid, or to allow, a formula to be upgraded, to a newer version, by the brew upgrade command.

$ brew pin nginx
# Forbid nginx from
# being upgraded, to
# newer versions.

$ brew unpin nginx
# Allow nginx to be
# upgraded, to newer
# versions.

brew uninstall, can be used, to uninstall a formula, or a cask.

$ brew uninstall sketch
# Uninstall the sketch
# application.

brew uses, can be used to show the formulas and casks, which require a given formula.

$ brew uses pcre
# Show the formulas, and casks,
# which require, pcre to be
# installed.

brew leaves, can be used to view installed formulas, which are not dependencies, of other installed formulas.

$ brew leaves
# Show the formulas, and
# casks, which are not
# dependencies, of other
# installed formulas.

$ brew leaves --installed-on-request
# Show installed on
# request formulas,
# which are not dependencies,
# of other installed formulas.

$ brew leaves --installed-as-dependency
# Show installed, as being
# part of a dependency,
# formulas, which now are
# not dependencies, of other
# installed formulas.

brew autoremove, will remove the formulas, which were installed as being required by other formulas, and are no longer needed.

$ brew autoremove
# Remove unneeded installed
# dependencies.

==> Uninstalling 6 unneeded formulae:
icu4c
libevent
lz4
protobuf
six
zstd
Uninstalling /usr/local/Cellar/icu4c/69.1... (259 files, 72.8MB)
Uninstalling /usr/local/Cellar/lz4/1.9.3... (22 files, 657.9KB)
Uninstalling /usr/local/Cellar/zstd/1.5.1... (31 files, 2.7MB)
Uninstalling /usr/local/Cellar/libevent/2.1.12... (57 files, 1.9MB)
Uninstalling /usr/local/Cellar/protobuf/3.19.3... (270 files, 19.7MB)
Uninstalling /usr/local/Cellar/six/1.16.0_2... (20 files, 122.3KB)

brew cleanup, can be used to remove stale lock, and downloaded files, for a formula or a cask, and to remove old versions, of a formula.

$ brew cleanup
# Remove stale lock, and
# downloaded files, for a
# formula or a cask, and
# remove old versions, of a
# formula.

Finally, to uninstall homebrew, you can use this script, found on the installation page.

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/uninstall.sh)"