77

Homebrew nicely allows package installations without sudo privileges, but it seems that I need admin privileges to install Homebrew itself.

I'd like to install Homebrew in a Mac environment where I don't have sudo or admin privileges. Is this possible?

BenjaminGolder
  • 967
  • 2
  • 8
  • 13

7 Answers7

95

In case somebody is still looking for this in 2020 and beyond, yes, this is possible without root privileges:

mkdir homebrew && curl -L https://github.com/Homebrew/brew/tarball/master | tar xz --strip 1 -C homebrew

The above allows you to install homebrew anywhere. Make sure that you add homebrew/bin to your $PATH.

More information about this alternative installation method is available in Brew's installation instructions.

judepereira
  • 1,066
  • 8
  • 5
25

Yes.

I modified the install script to not use sudo and to use a directory of your choice. https://gist.github.com/skyl/36563a5be809e54dc139

Download that, set YOUR_HOME in the script to the absolute path. chmod +x the script. Create the YOUR_HOME/usr/local directory. Then, execute the script.

./install.rb

In .bash_profile, I set (I'm not positive this is important, pretty sure):

export HOMEBREW_PREFIX=/The/path/to/YOUR_HOME/usr/local

Now, I can:

brew install wget

Make sure the bin directory, YOUR_HOME + /usr/local/bin is on your $PATH.

which wget
Skylar Saveland
  • 401
  • 1
  • 5
  • 5
  • 2
    In the install script I had to add `#{YOUR_HOME}` prefix to the `HOMEBREW_CACHE` variable as well. That, and because the system admin already had homebrew installed, I had to add `export PATH=/path/to/home/usr/local/bin:$PATH` to `.bash_profile` as well. But with all that, it seems to work great. – golmschenk Jun 26 '15 at 19:31
  • 3
    Just went through the process again, and I just wanted to mention you also have to make a `usr` directory in your home directory before the script will run. Additionally, when you add `#{YOUR_HOME}` to the `HOMEBREW_CACHE` variable, it seems you need to change the single quotes to double quotes for it to work. So there are a few steps, but it's definitely worth it to get the power of Homebrew. – golmschenk Jul 20 '15 at 17:01
  • @golmschenk double quotes are required for ruby's string formatting to kick in, I believe. – Skylar Saveland Jul 24 '15 at 20:14
  • 1
    That's a nice adjustment. You really ought to submit a pull request to the home-brew project. – David Hoelzer Dec 05 '15 at 19:48
  • 1
    I tried this from a non-admin account on macOS Sierra (10.12.3) and it didn't work because it needed to do a `chgrp admin /usr/local`, and a non-admin account isn't a member of the admin group, so it can't change anything to that group. It would be great if this could be made to work for a normal non-admin account (not just for admin accounts that have for some strange reason been locked out of sudo). – Spiff Feb 06 '17 at 18:13
  • I left the chgrp commands in and had no issues. In Mojave, and totally not an administrator. Here's a fork I recommend most people use: https://gist.github.com/devinrhode2/a37f9a05656fef604332779ec6e5f0b4 Disclaimer: I barely know ruby, but just plucked a few seemingly good bits from various people's forks. Worked fine for me. – Devin Rhode Sep 28 '20 at 02:37
  • Only thing I can't do is change my default shell to the brew-installed one. Otherwise, everything works!! (Instead I just uninstall zsh and re-ran chsh -s $(which zsh), which is the default-install /bin/zsh on mojave) – Devin Rhode Sep 28 '20 at 14:06
  • For homebrew cask users, you'll want to add this to your ~/.profile: `export HOMEBREW_CASK_OPTS="--appdir=~/Users/devinrhode3/Applications --fontdir=/Users/devinrhode3/Library/Fonts"` – Devin Rhode Sep 30 '20 at 19:21
  • I used the linked `install.rb` again successfully on catalina :) – Devin Rhode Dec 15 '20 at 17:15
17

No.. Unless you do significant surgery.

The reason is that Homebrew strongly insists on installing packages into /usr/local. In fact, even if you forced it to install somewhere else, you are likely to break dependencies when you use brew install to install packages. Most if not all of these packages are pre-compiled and linked expecting to live in /usr/local.

The reason for this insistence is that /usr/local is precisely where POSIX recommends that stuff like this gets installed. In order to create /usr/local Homebrew needs temporary admin credentials to create the directory and assign ownership.

This, in turn, is what allows you to install anything else without elevating credentials.

David Hoelzer
  • 476
  • 4
  • 10
  • Thanks, but I think you missed my question. I know how & where it installs packages, but I want to install _Homebrew_ without admin or sudo. – BenjaminGolder Jul 14 '13 at 01:17
  • I didn't miss your question. The answer was "No".. I just said "Yes" accidentally. You cannot easily do what you are asking and even if you forced it by editing the Homebrew installer to put things into a branch off of your home directory, more than likely lots of brews would fail after installation. – David Hoelzer Jul 14 '13 at 01:18
  • okay, thanks. I was confused by the previous "Yes". – BenjaminGolder Jul 14 '13 at 01:25
  • 7
    This is just wrong, or at least you definition of "significant surgery" is very different then mine. I've been running `brew` with a prefix set to my home directory for 5 plus years and I've encounter 1 or 2 broken packages in that time. – James McMahon Sep 12 '17 at 02:22
  • 1
    However, also there is also this, which is hard to get around: Error: You have not agreed to the Xcode license. Please resolve this by running: sudo xcodebuild -license accept – Albert Jan 16 '19 at 17:47
  • 1
    @Albert I was recently checking the git version installed by default on mojave, I literally just typed "git" and hit enter, and I was able to get a prompt to install xcode cli tools, seems to have worked fine without admin privileges. (10.14.6) – Devin Rhode Sep 26 '20 at 01:30
2

To install homebrew without sudo.

git clone https://github.com/Homebrew/brew.git
echo 'export PATH="/path/to/cloned_folder/homebrew/bin:$PATH"' >> ~/.bash_profile

Restart terminal and run

brew --version
Devin Rhode
  • 128
  • 1
  • 1
  • 9
Astik Anand
  • 121
  • 3
1

Yes.

The brew system appears bootstrappable and can be installed in one own's home directory. According to a comment by @Adrian, this will take much longer because each package will be built from its source code.

#!/bin/bash
set -ex

export HOMEBREW_PREFIX=~/homebrew
# export HOMEBREW_NO_ANALYTICS=1
mkdir -p "${HOMEBREW_PREFIX}"
curl -fsSLk https://github.com/Homebrew/brew/tarball/master | tar xz --strip 1 -C "${HOMEBREW_PREFIX}"

ls -laR "${HOMEBREW_PREFIX}"

export PATH="${HOMEBREW_PREFIX}/bin:${PATH}"
type -a brew

type -a openssl || :
openssl version -a || :

type -a curl || :
curl -V || :

# Fails to lock a .git/config file.
##brew analytics off

# No "brew update" until installing the proper openssl and a curl that uses it.
# brew update

# brew remove openssl || :
brew install openssl
brew link --force openssl

# brew remove curl || :
brew install --with-openssl curl
brew link --force curl || :
curl -V

ls -la "${HOMEBREW_PREFIX}/opt"
ls -la "${HOMEBREW_PREFIX}/bin"
ls -laLR "${HOMEBREW_PREFIX}/opt/curl/"
eel ghEEz
  • 308
  • 2
  • 7
  • like 10x less code than the ruby version... but partly ruby has to deal with a lot of ceremony by not being a bash script.. @eel have you run into any issues with this installation? – Devin Rhode Sep 26 '20 at 02:56
  • I used this script only to get my homebrew going. (A brew upgrade needs a trick with `brew update-reset`. https://gist.github.com/ilatypov/f197cd65941063e2df828122c7de0c52 ). – eel ghEEz Sep 26 '20 at 04:08
  • What do you mean "only get my homebrew going"? Are you able to get a successful homebrew setup going with this script? Install command line things like git, zsh, etc? I ended up using a tweaked version of Skyl's ruby script above, but, I am curious if this would have worked too. – Devin Rhode Sep 28 '20 at 02:42
  • Wow `brew install openssl` is painfully slow on my MBP :( – Devin Rhode Sep 28 '20 at 02:56
  • Updated openssl and now curl, and getting "Error: invalid option: --with-openssl" https://gist.github.com/devinrhode2/832288a0ef15d6e4e4dfc3e4a341bcbe – Devin Rhode Sep 28 '20 at 04:20
  • 2
    @DevinGRhode All core Homebrew formulae no longer support options like `--with-openssl`. Also, if you install Homebrew outside the official root, all your installs are likely to build from source, rather than using prebuilt binaries, so yeah, it'll be painfully slow. – Adrian Sep 28 '20 at 13:50
  • I have no idea how you ensure curl is using the latest openssl.. Would you mind updating your answer? – Devin Rhode Sep 28 '20 at 14:13
  • Answer still has "brew install --with-openssl curl" - @eelghEEz @Adrian how do I ensure `curl` is using the brew-installed openssl? – Devin Rhode Dec 14 '20 at 02:12
  • @DevinGRhode As of this writing, the `curl-openssl` formula no longer exists, and `brew install curl` installs the OpenSSL-enabled curl. – Adrian Dec 15 '20 at 03:28
1

Brew moved their git repo

git clone git@github.com:Homebrew/brew.git
echo 'export PATH="/path/to/cloned_folder/homebrew/bin:$PATH"' >> ~/.bash_profile
pwaterz
  • 111
  • 2
0

The following script shows a way to install brew without root. Please do export environment variables HOMEBREW_PREFIX & PATH in your ~/.zshrc after installation to ensure the package will placed at a expected directory

download

cd ~
git clone https://github.com/Homebrew/brew homebrew
mkdir ~/usr/local
# installed packaged directory
echo "export HOMEBREW_PREFIX=~/usr/local" >> ~/.zshrc
echo "export PATH=$PATH:~/homebrew/bin:HOMEBREW_PREFIX/bin" >> ~/.zshrc

update brew

brew update
# if error
brew update-reset
Alpha
  • 101
  • 2
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 26 '21 at 15:42