How to set a custom path for Homebrew

Updated: January 28, 2024 By: Guest Contributor Post a comment

Introduction

Homebrew is a popular package manager for macOS and Linux that simplifies the process of installing software. By default, Homebrew installs itself and its packages in /usr/local on macOS and /home/linuxbrew/.linuxbrew on Linux. However, there might be occasions where you need to customize this path. This tutorial will guide you through the process of setting a custom path for Homebrew installations, with step-by-step instructions and code examples.

Prerequisites

  • A computer running macOS or Linux
  • Basic knowledge of command-line operations
  • Administrative or superuser access on your system

Installing Homebrew

Before setting a custom path, ensure that you have Homebrew installed. If it’s not installed, you can install it using the following script:

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

This installs Homebrew to its default location. Follow all the on-screen instructions to complete the installation.

Reasons for Changing the Homebrew Path

Some reasons you might want to change the default Homebrew installation path include:

  • Permission issues with the default directory.
  • Separation of environments for development purposes.
  • Limited storage on the default partition.
  • Organization preferences.

Setting a Custom Path for Homebrew

Step 1: Uninstall the Current Homebrew Installation (Optional)

If Homebrew is already installed and you want to move it to a new custom location, you’ll first need to uninstall it.

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

Step 2: Selecting a New Homebrew Path

Decide on the new directory where you’ll install Homebrew. For this example, we’ll use /opt/homebrew as the custom path.

Step 3: Installing Homebrew to the Custom Path

We need to set the directory manually and adjust permissions accordingly:

sudo mkdir /opt/homebrew
sudo chown -R $(whoami) /opt/homebrew

This creates the new directory and gives your user ownership.

Step 4: Modify the Homebrew Install Script

You must modify the Homebrew installation script to recognize the new location. Download the installation script and open it in a text editor:

curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh -o install.sh
nano install.sh

Locate and modify the HOMEBREW_PREFIX to point to your new path:

HOMEBREW_PREFIX="/opt/homebrew"

Save and close the file.

Step 5: Run the Modified Install Script

Run the modified install script to install Homebrew into the custom location:

/bin/bash install.sh

The script will install Homebrew into the specified directory.

Step 6: Updating Your Profile

Now, you must update your shell profile to include the new Homebrew bin directory in your PATH. For bash users:

echo 'export PATH="/opt/homebrew/bin:$PATH"' >> ~/.bash_profile
source ~/.bash_profile

If you use a different shell, adjust the above command to update the appropriate profile file (e.g., .zshrc for Zsh users).

Advanced Configuration

Advanced users can configure Homebrew further. For instance, Homebrew Cask can be configured to install applications in a specific location beyond the default /Applications folder:

export HOMEBREW_CASK_OPTS="--appdir=~/apps"

This setting tells Homebrew to install GUI applications in the ~/apps directory.

Conclusion

This tutorial walked you through the steps of changing the default Homebrew path to a custom one and provided advanced configuration options for more granular control. Customizing your Homebrew path can be particularly useful for managing storage or permissions and can be successfully done with a bit of command-line knowledge.