How should I install homebrew into /usr/local subdirectory manually?

10,033

NOTE: This answer assumes that the macOS user is setup as an administrator. Please see IMPORTANT note:

IMPORTANT: do not use sudo unless absolutely necessary as described below. The end result should be a Homebrew installation that does not need the use of sudo ever again.

Recommended method as of June 2021:

Use sudo to make a subdirectory such as opt that Homebrew can be installed under, and use your own login password if needed:

sudo mkdir /usr/local/opt

and sudo sudo only once more to take ownership of the subdirectory under the staff group, for example:

sudo chown brodybits:staff /usr/local/opt

At this point sudo should no longer be needed for installing or using Homebrew.

ls -al /usr/local should now show the new subdirectory, for example:

total 0
drwxr-xr-x   5 root       wheel  160 May 13 17:13 .
drwxr-xr-x@ 11 root       wheel  352 Jan  1  2020 ..
-rw-r--r--   1 root       wheel    0 Oct 16  2018 .com.apple.installer.keep
drwxr-xr-x   4 root       wheel  128 May 13 17:13 bak
drwxr-xr-x   2 brodybits  staff   64 May 13 17:03 opt

Now use Git to clone Homebrew/brew from GitHub into the new subdirectory, for example:

cd /usr/local/opt
git clone https://github.com/Homebrew/brew
ls -al /usr/local/opt # recommended

Optional: It is possible to rename the newly installed brew subdirectory, if so desired. It is also optional to move the new brew subdirectory up to the /usr/local level (sudo may be needed just once more for this).

Update the desired profile file ($HOME/.zprofile or $HOME/.zshenv in case of zsh, for example) to add the new Homebrew bin subdirectory to the PATH environment variable. Here is an example:

export PATH=/usr/local/opt/brew/bin:$PATH

(See also this answer.)

Restart the shell, start a new shell, or source the profile file. It is recommended to check that which brew shows it in the right place, /usr/local/opt/brew/bin/brew.

At this point, brew --version should show some information (with no homebrew-core installed at this point), for example:

Homebrew 3.1.7-36-g7c68b17
Homebrew/homebrew-core N/A

Then run brew update to include homebrew-core and run brew doctor to verify proper installation. It will likely output some warning messages about the non-standard Homebrew prefix but this should be an issue for most packages.

brew --version after brew doctor should show homebrew-core included, for example:

Homebrew 3.1.7-36-g7c68b17
Homebrew/homebrew-core (git revision 61cc59c956; last commit 2021-05-13)

Try installing a simple formula such as autoconf:

brew install autoconf

autoconf --version should now show that it is installed.

See further below for some optional followup steps.


Outdated solution from 2018 no longer tested or updated:

First make a temporary directory within /usr/local using sudo (enter password if requested):

sudo mkdir /usr/local/brewtmp1

Check the results of ls -l /usr/local:

total 0
drwxr-xr-x   2 root       wheel   64 Jul  2 16:16 brewtmp1

Fix the user and group ownership of the temporary directory:

sudo chown $USER:admin brewtmp1

Check the results of ls -l /usr/local again:

total 0
drwxr-xr-x   2 brodybits  admin   64 Jul  2 16:16 brewtmp1

Then within the temporary directory download and extract the contents of master branch of Homebrew/brew on GitHub without using sudo:

cd /usr/local/brewtmp1
curl -L https://github.com/Homebrew/brew/tarball/master | tar xz

Check the results of ls -l within /usr/local/brewtmp1:

total 0
drwxr-xr-x  16 brodybits  admin  512 Jul  2 14:46 Homebrew-brew-cbc3184

Check the results of ls -l Homebrew-brew-*:

total 48
-rw-r--r--   1 brodybits  admin    98 Jul  2 14:46 CHANGELOG.md
-rw-r--r--   1 brodybits  admin  3161 Jul  2 14:46 CODE_OF_CONDUCT.md
-rw-r--r--   1 brodybits  admin   720 Jul  2 14:46 CONTRIBUTING.md
-rw-r--r--   1 brodybits  admin  1334 Jul  2 14:46 LICENSE.txt
drwxr-xr-x   7 brodybits  admin   224 Jul  2 14:46 Library
-rw-r--r--   1 brodybits  admin  7374 Jul  2 14:46 README.md
drwxr-xr-x   3 brodybits  admin    96 Jul  2 14:46 bin
drwxr-xr-x   5 brodybits  admin   160 Jul  2 14:46 completions
drwxr-xr-x  48 brodybits  admin  1536 Jul  2 14:46 docs
drwxr-xr-x   5 brodybits  admin   160 Jul  2 14:46 manpages

This should show the contents of Homebrew with correct user and group.

Rename Homebrew-brew-??????? to homebrew (without using sudo) (it should be OK to use another name, with adaptations to other steps below):

mv Homebrew-brew-??????? homebrew

and double-check the results of ls -l:

total 0
drwxr-xr-x  16 brodybits  admin  512 Jul  2 14:46 homebrew

Move the new homebrew directory to the right place using sudo:

sudo mv /usr/local/brewtmp1/homebrew /usr/local

Double-check the results of ls -l /usr/local:

total 0
drwxr-xr-x   2 brodybits  admin   64 Jul  2 16:20 brewtmp1
drwxr-xr-x  16 brodybits  admin  512 Jul  2 14:46 homebrew

Add the following line to profile file (.profile or .bash_profile as appropriate) (as directed by this answer to another question):

export PATH=/usr/local/homebrew/bin:$PATH

Restart shell, start new shell, or source the profile file. Check that which brew shows /usr/local/homebrew/bin/brew. Check that brew --version works (though with no homebrew-core installed) for example:

Homebrew >=1.4.0 (shallow or no git repository)
Homebrew/homebrew-core N/A

It is recommended to do brew doctor at this point, which will include homebrew-core and verify proper installation. It may output a warning about non-standard Homebrew prefix but this should be an issue for most packages. Then brew --version should show homebrew-core included:

Homebrew >=1.4.0 (shallow or no git repository)
Homebrew/homebrew-core (git revision 2f937; last commit 2018-07-02)

Try installing a simple formula such as autoconf:

brew install autoconf

autoconf --version should now show that it is installed.

ls -l /usr/local/homebrew should now look like this:

total 48
-rw-r--r--   1 brodybits  admin    98 Jul  2 16:43 CHANGELOG.md
-rw-r--r--   1 brodybits  admin  3161 Jul  2 16:43 CODE_OF_CONDUCT.md
-rw-r--r--   1 brodybits  admin   720 Jul  2 16:43 CONTRIBUTING.md
drwxr-xr-x   3 brodybits  admin    96 Jul  2 16:43 Cellar
-rw-r--r--   1 brodybits  admin  1334 Jul  2 16:43 LICENSE.txt
drwxr-xr-x   8 brodybits  admin   256 Jul  2 16:43 Library
-rw-r--r--   1 brodybits  admin  7374 Jul  2 16:43 README.md
drwxr-xr-x  10 brodybits  admin   320 Jul  2 16:43 bin
drwxr-xr-x   5 brodybits  admin   160 Jul  2 16:43 completions
drwxr-xr-x  48 brodybits  admin  1536 Jul  2 16:43 docs
drwxr-xr-x   3 brodybits  admin    96 Jul  2 16:43 etc
drwxr-xr-x   5 brodybits  admin   160 Jul  2 16:43 manpages
drwxr-xr-x   4 brodybits  admin   128 Jul  2 16:43 opt
drwxr-xr-x   8 brodybits  admin   256 Jul  2 16:43 share
drwxr-xr-x   3 brodybits  admin    96 Jul  2 16:42 var

It is now recommended to add some possibly missing directories and fix some directory permissions to match the results of the Homebrew install script (I wish brew doctor would check and offer to take care of this part):

cd /usr/local/homebrew
mkdir -p include Caskroom Cellar lib opt share var
chmod g+w include Caskroom Cellar lib opt share var

And finally cleanup /usr/local/brewtmp1 (using sudo):

sudo rm -rf /usr/local/brewtmp1

Optional followup steps:

To disable analytics (thanks to this article, must be done after installing a package):

brew analytics off

You can Homebrew/homebrew-cask-versions to get access to alternate versions such as daily and beta versions, as they have documented:

brew tap homebrew/cask-versions

Additional reading and other resources from 2018:

Share:
10,033

Related videos on Youtube

brodybits
Author by

brodybits

Updated on September 18, 2022

Comments

  • brodybits
    brodybits over 1 year

    Manual installation directions in docs.brew.sh are very brief and unsatisfactory, resulting in some errors if I try it in a subdirectory of /usr/local without using sudo. Directions in this question along with its accepted answer are a little better but also do not work without using sudo.

    I understand that I may need to use sudo to install Homebrew itself but hope I do not need to continue using sudo to install new packages, update, etc. (Standard Homebrew install scripts seem to use sudo but only when absolutely necessary, while the standard result is that there is no need to use sudo to install new packages, update, etc.)

    How should I manually install Homebrew into a /usr/local subdirectory?

    Motivation for manual installation info /usr/local subdirectory:

    • Manual installation gives me the best control over the process of installation and management of Homebrew.
    • Installation into subdirectory of my home directory is not ideal since Homebrew manages a combination of locally built packages and system-wide packages.

    Motivation for avoiding the need for the user to use sudo after installation:

    • consistency with standard installation
    • easier to manage existing Homebrew packages
    • lower security risk if Homebrew itself or Homebrew packages do not compel the user to run some things as root
    • fixer1234
      fixer1234 almost 6 years
      Why is using sudo a problem (might be worth mentioning in the question)?
    • brodybits
      brodybits almost 6 years
      @fixer1234 it is clarified in the question.
    • Giacomo1968
      Giacomo1968 almost 6 years
      Call me nuts, but why not use the official method of install as shown here on the official Homebrew site: /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/in‌​stall)". I have always done this and have utterly never had to use sudo brew for any reason.
    • dirkt
      dirkt almost 6 years
      The standard installation of homebrew avoids having to use sudo after installation, and while it's true you don't have control over what's going on with the "official" method, all it does is create subdirectories in /usr/local with the user as the owner (to avoid sudo later). So I don't really understand the motivation to do that manually, having to get all the details right yourself.