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

1

1

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

brodybits

Posted 2018-07-02T21:21:47.883

Reputation: 151

1Why is using sudo a problem (might be worth mentioning in the question)? – fixer1234 – 2018-07-02T22:12:57.630

@fixer1234 it is clarified in the question. – brodybits – 2018-07-02T23:22:22.543

1

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/install)". I have always done this and have utterly never had to use sudo brew for any reason.

– JakeGould – 2018-07-02T23:47:19.010

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. – dirkt – 2018-07-03T06:33:49.823

Answers

1

NOTE: This answer assumes that the macOS user is setup as an administrator.

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.

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.

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

brew analytics off

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

Additional reading and other resources:

brodybits

Posted 2018-07-02T21:21:47.883

Reputation: 151