Skip to main contentEric N. Garcia

Homebrew

Helpful commands while working with brew.

When trying to install using homebrew

User does not have permission to write to /usr/local/Cellar and throws following error.

Error: Cannot write to /usr/local/Cellar

Attempting to

sudo
it refuses with following error.

Cowardly refusing to `sudo brew install'

You somehow have limited permissions to /usr/local/Cellar. Brew doesn’t like to install with sudo which is why it refuses. Correct the issue with the following commands:

  1. Check the permissions:

    ls -ld /usr/local/Cellar
  2. Open them up for writing:

    sudo chmod a+w /usr/local/Cellar

Bundle

Bundle is a very useful command. This command will allow you to keep a backup of all installed packages from brew into a file. That can be backed up and reinstalled on different machines, or to clean up and reset your environments.

Creating Brewfile

To create the the Brewfile run the following command:

# To write to specific file
brew bundle dump --file=~/.eng/Brewfile
# To write to current working directory
brew bundle dump

Installing from Brewfile

If you want to ensure you have all packages listed in your Brewfile installed, you can reinstall or do a fresh install (on a new machine) with the following command:

# Install from specific file
brew bundle --file=~/.eng/Brewfile
# Install with Brewfile in current working directory
brew bundle

TIP: if you want to overwrite an existing brew file add the

--force
flag to the above command.

Resetting local environment

If your machine has gotten out of hand and you want to forcefully go back to only having packages listed in your Brewfile, you can run the following:

# Pointing to specific Brewfile
brew bundle --force cleanup --file=~/.eng/Brewfile
# Pointing to Brewfile in current working directory
brew bundle --force cleanup

Housekeeping

  • autoremove
    : Removes all unused dependencies.

    brew autoremove
  • cleanup
    : cleans up cached packages and old versions of installed packages. You can use the
    --dry-run
    flag here to see what would be removed, before clearing is desired.

    # This safely removes aged cache and packages.
    brew cleanup
    # This cleans up all regardless of age.
    brew cleanup --prune=all

Useful taps and packages