How to Install and Use Homebrew on Linux in CircleCI
Overview
If you need to use brew in a CircleCI job and it isn't available in your convenience image, you can install it as a step in your job config.
Note: Linuxbrew has been merged into Homebrew. The
Linuxbrew/installrepository is deprecated — use the official Homebrew install script instead.
Installing Homebrew in a CircleCI job
Add the following run step before any step that uses brew. It installs Homebrew and makes it available to all subsequent steps via $BASH_ENV:
- run:
name: Install and configure Homebrew
command: |
sudo apt-get install -y build-essential curl file git
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
echo 'eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"' >> $BASH_ENV
source $BASH_ENV
CircleCI sources $BASH_ENV at the start of each subsequent run step, so Homebrew will remain available throughout the rest of the job without any additional setup.
Verifying the installation
To confirm Homebrew is installed and accessible, add a verification step:
- run:
name: Verify Homebrew
command: brew --version
Installing packages with brew
Once installed, use brew install as normal:
- run:
name: Install packages with Homebrew
command: |
brew install wget
brew install jq
Caching the Homebrew installation
Installing Homebrew on every run adds build time. You can cache the installation directory to speed up subsequent jobs:
- restore_cache:
keys:
- homebrew-v1-{{ arch }}
- run:
name: Install and configure Homebrew
command: |
if ! command -v brew &>/dev/null; then
sudo apt-get install -y build-essential curl file git
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
fi
echo 'eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"' >> $BASH_ENV
source $BASH_ENV
- save_cache:
key: homebrew-v1-{{ arch }}
paths:
- /home/linuxbrew/.linuxbrew
Notes
- Homebrew on Linux installs to
/home/linuxbrew/.linuxbrewby default. - The
brewcommand should not be run withsudoafter installation. - For full details on Homebrew on Linux, see the official Homebrew Linux documentation.
Comments
Article is closed for comments.