
Inspired by @jvns' [post][1], I realized that I've had this problem on multiple teams, where someone missed the step at the end and didn't know how to recover. Typically, I've provided a version like what I've added to the Tips 'n' Tricks page so that I didn't have to think about what OS-arch pair my users are using. I've tested the loader added to tips and tricks with both bash and zsh and it passes both shellcheck and shfmt in posix mode. [1]: https://mastodon.social/@b0rk@jvns.ca/113997565198024027
182 lines
7.0 KiB
Markdown
182 lines
7.0 KiB
Markdown
---
|
||
last_review_date: "1970-01-01"
|
||
---
|
||
|
||
# Tips and Tricks
|
||
|
||
## Install previous versions of formulae
|
||
|
||
Some formulae in `homebrew/core` are made available as [versioned formulae](Versions.md) using a special naming format, e.g. `gcc@9`. If the version you're looking for isn't available, consider using `brew extract`.
|
||
|
||
## Quickly remove something from Homebrew's prefix
|
||
|
||
```sh
|
||
brew unlink <formula>
|
||
```
|
||
|
||
This can be useful if a package can't build against the version of something you have linked into Homebrew's prefix.
|
||
|
||
And of course, you can simply `brew link <formula>` again afterwards!
|
||
|
||
## Pre-download a file for a formula
|
||
|
||
Sometimes it's faster to download a file via means other than the strategies that are available as part of Homebrew. For example, Erlang provides a torrent that'll let you download at 4–5× compared to the normal HTTP method.
|
||
|
||
Downloads are saved in the `downloads` subdirectory of Homebrew's cache directory (as specified by `brew --cache`, e.g. `~/Library/Caches/Homebrew`) and renamed as `<url-hash>--<formula>-<version>`. The command `brew --cache --build-from-source <formula>` will print the expected path of the cached download, so after downloading the file, you can run `mv the_tarball "$(brew --cache --build-from-source <formula>)"` to relocate it to the cache.
|
||
|
||
You can also pre-cache the download by using the command `brew fetch <formula>` which also displays the SHA-256 hash. This can be useful for updating formulae to new versions.
|
||
|
||
## Install stuff without the Xcode CLT
|
||
|
||
```sh
|
||
brew sh # or: eval "$(brew --env)"
|
||
gem install ronn # or c-programs
|
||
```
|
||
|
||
This imports the `brew` environment into your existing shell; `gem` will pick up the environment variables and be able to build. As a bonus, `brew`'s automatically determined optimization flags are set.
|
||
|
||
## Install only a formula's dependencies (not the formula)
|
||
|
||
```sh
|
||
brew install --only-dependencies <formula>
|
||
```
|
||
|
||
## Use the interactive Homebrew shell
|
||
|
||
```console
|
||
$ brew irb
|
||
==> Interactive Homebrew Shell
|
||
Example commands available with: `brew irb --examples`
|
||
irb(main):001:0> Formulary.factory("ace").methods - Object.methods
|
||
=> [:install, :test, :test_defined?, :sbin, :pkgshare, :elisp,
|
||
:frameworks, :kext_prefix, :any_version_installed?, :etc, :pkgetc,
|
||
...
|
||
:on_macos, :on_linux, :debug?, :quiet?, :verbose?, :with_context]
|
||
irb(main):002:0>
|
||
```
|
||
|
||
## Hide the beer mug emoji when finishing a build
|
||
|
||
```sh
|
||
export HOMEBREW_NO_EMOJI=1
|
||
```
|
||
|
||
This sets the `HOMEBREW_NO_EMOJI` environment variable, causing Homebrew to hide all emoji.
|
||
|
||
The beer emoji can also be replaced with other character(s):
|
||
|
||
```sh
|
||
export HOMEBREW_INSTALL_BADGE="☕️ 🐸"
|
||
```
|
||
|
||
## Migrate a Homebrew installation to a new location
|
||
|
||
Running `brew bundle dump` will record an installation to a `Brewfile` and `brew bundle install` will install from a `Brewfile`. See `brew bundle --help` for more details.
|
||
|
||
## Appoint Homebrew Cask to manage a manually-installed app
|
||
|
||
Run `brew install --cask` with the `--adopt` switch:
|
||
|
||
```console
|
||
$ brew install --cask --adopt textmate
|
||
==> Downloading https://github.com/textmate/textmate/releases/download/v2.0.23/TextMate_2.0.23.tbz
|
||
...
|
||
==> Installing Cask textmate
|
||
==> Adopting existing App at '/Applications/TextMate.app'
|
||
==> Linking Binary 'mate' to '/opt/homebrew/bin/mate'
|
||
🍺 textmate was successfully installed!
|
||
```
|
||
|
||
## Editor plugins
|
||
|
||
### Visual Studio Code
|
||
|
||
- [Brewfile](https://marketplace.visualstudio.com/items?itemName=sharat.vscode-brewfile) adds Ruby syntax highlighting for [Homebrew Bundle](https://github.com/Homebrew/homebrew-bundle) `Brewfile`s.
|
||
|
||
- [Brew Services](https://marketplace.visualstudio.com/items?itemName=beauallison.brew-services) is an extension for starting and stopping Homebrew services.
|
||
|
||
### Sublime Text
|
||
|
||
- [Homebrew-formula-syntax](https://github.com/samueljohn/Homebrew-formula-syntax) can be installed with Package Control in Sublime Text 2/3, which adds highlighting for inline patches.
|
||
|
||
### Vim
|
||
|
||
- [brew.vim](https://github.com/xu-cheng/brew.vim) adds highlighting to inline patches in Vim.
|
||
|
||
### Emacs
|
||
|
||
- [homebrew-mode](https://github.com/dunn/homebrew-mode) provides syntax highlighting for inline patches as well as a number of helper functions for editing formula files.
|
||
|
||
- [pcmpl-homebrew](https://github.com/hiddenlotus/pcmpl-homebrew) provides completion for emacs shell-mode and eshell-mode.
|
||
|
||
## macOS Terminal.app: Enable the "Open man Page" contextual menu item
|
||
|
||
In the macOS Terminal, you can right-click on a command name (like `ls` or `tar`) and pop open its manpage in a new window by selecting "Open man Page".
|
||
|
||
Terminal needs an extra hint on where to find manpages installed by Homebrew because it doesn't load normal dotfiles like `~/.bash_profile` or `~/.zshrc`.
|
||
|
||
```sh
|
||
sudo mkdir -p /usr/local/etc/man.d
|
||
echo "MANPATH /opt/homebrew/share/man" | sudo tee -a /usr/local/etc/man.d/homebrew.man.conf
|
||
```
|
||
|
||
If you're using Homebrew on macOS Intel, you should also fix permissions afterwards with:
|
||
|
||
```sh
|
||
sudo chown -R "${USER}" /usr/local/etc
|
||
```
|
||
|
||
## Use a caching proxy or mirror for Homebrew bottles
|
||
|
||
You can configure Homebrew to retrieve bottles from a caching proxy or mirror.
|
||
|
||
For example, in JFrog's Artifactory, accessible at `https://artifacts.example.com`,
|
||
configure a new "remote" repository with `homebrew` as the "repository key" and `https://ghcr.io` as the URL.
|
||
|
||
Then, set these environment variables for Homebrew to retrieve from the caching proxy.
|
||
|
||
```sh
|
||
export HOMEBREW_ARTIFACT_DOMAIN=https://artifacts.example.com/artifactory/homebrew/
|
||
export HOMEBREW_ARTIFACT_DOMAIN_NO_FALLBACK=1
|
||
export HOMEBREW_DOCKER_REGISTRY_BASIC_AUTH_TOKEN="$(printf 'anonymous:' | base64)"
|
||
```
|
||
|
||
## Loading Homebrew from the same dotfiles on different operating systems
|
||
|
||
Some users may want to use the same shell initialization files on macOS and Linux.
|
||
Use this to detect the likely Homebrew installation directory and load Homebrew when it's found.
|
||
You may need to adapt this to your particular shell or other particulars of your environment.
|
||
|
||
```sh
|
||
# Execute only if brew isn't already available.
|
||
if ! [ -x "$(command -v brew)" ]; then
|
||
OS="$(uname)"
|
||
UNAME_MACHINE="$(uname -m)"
|
||
if [ "${OS}" = "Linux" ]; then
|
||
# Linux
|
||
HOMEBREW_PREFIX="/home/linuxbrew/.linuxbrew"
|
||
elif [ "${OS}" = "Darwin" ]; then
|
||
if [ "${UNAME_MACHINE}" = "arm64" ]; then
|
||
# M-series ARM64 macOS
|
||
HOMEBREW_PREFIX="/opt/homebrew"
|
||
else
|
||
# Intel macOS
|
||
HOMEBREW_PREFIX="/usr/local"
|
||
fi
|
||
fi
|
||
|
||
if [ -d "${HOMEBREW_PREFIX}" ]; then
|
||
BREW_BIN="${HOMEBREW_PREFIX}/bin/brew"
|
||
if [ -x "${BREW_BIN}" ]; then
|
||
eval "\$(${BREW_BIN} shellenv)"
|
||
else
|
||
>&2 printf "Homebrew possibly found at %s but %s is not executable. Check the permissions.\n" "${HOMEBREW_PREFIX}" "${BREW_BIN}"
|
||
fi
|
||
else
|
||
>&2 printf "Homebrew not found where expected in %s on %s %s\n" "${HOMEBREW_PREFIX}" "${OS}" "${UNAME_MACHINE}"
|
||
>&2 printf "Double-check that it's installed or run the following command to install it\n\n\t%s\n" \
|
||
'/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"'
|
||
fi
|
||
fi
|
||
```
|