Skip to content

Commit de18eb8

Browse files
authored
Adding sudoless mode incorporating suggestions
This is a larger change but it: 1. Has a check to make sure all files in the Homebrew prefix are writable 2. Specifically handles CLT installation 3. Cleanly separates it out into its own function Uses running `chmod` as a check. If it fails then something isn't writable. Bonus is it also is a permissions repair. For the CLT install with macOS sudoless mode, the first method will fail, so within the fallback we check for macOS_sudoless_mode and if we're using it then `abort` with the message about manual installation, instead of running the GUI pop up manual installation. This still retains the original goal of installing in the official prefixes without sudo, so it doesn't result in an "Alternative (unsupported) installation".
1 parent 98e98cc commit de18eb8

File tree

1 file changed

+32
-6
lines changed

1 file changed

+32
-6
lines changed

install.sh

+32-6
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,18 @@ REQUIRED_GIT_VERSION=2.7.0 # HOMEBREW_MINIMUM_GIT_VERSION in brew.sh in Homebr
217217
export HOMEBREW_NO_ANALYTICS_THIS_RUN=1
218218
export HOMEBREW_NO_ANALYTICS_MESSAGE_OUTPUT=1
219219

220+
macos_sudoless_mode() {
221+
if [[ -n "${MACOS_SUDOLESS_SATISFIED-}" ]]
222+
then
223+
return "${MACOS_SUDOLESS_SATISFIED}"
224+
fi
225+
226+
[[ -d "${HOMEBREW_PREFIX}" ]] && "${CHMOD[@]}" -R "ug+w" "${HOMEBREW_PREFIX}"
227+
MACOS_SUDOLESS_SATISFIED=$?
228+
229+
return "${MACOS_SUDOLESS_SATISFIED}"
230+
}
231+
220232
unset HAVE_SUDO_ACCESS # unset this from the environment
221233

222234
have_sudo_access() {
@@ -225,7 +237,7 @@ have_sudo_access() {
225237
return 1
226238
fi
227239

228-
if [[ -n "${HOMEBREW_ON_MACOS-}" ]] && [[ -w "${HOMEBREW_PREFIX}" ]]
240+
if [[ -n "${HOMEBREW_ON_MACOS-}" ]] && macos_sudoless_mode
229241
then
230242
return 1
231243
fi
@@ -260,6 +272,7 @@ Or for a standard (non-admin) user, run:
260272
261273
sudo mkdir -p "${HOMEBREW_PREFIX}"
262274
sudo chown -R "${USER}:${GROUP}" "${HOMEBREW_PREFIX}"
275+
sudo chmod -R "ug+w" "${HOMEBREW_PREFIX}"
263276
264277
Then run this installer script again.
265278
EOABORT
@@ -877,11 +890,24 @@ fi
877890
# Headless install may have failed, so fallback to original 'xcode-select' method
878891
if should_install_command_line_tools && test -t 0
879892
then
880-
ohai "Installing the Command Line Tools (expect a GUI popup):"
881-
execute "/usr/bin/xcode-select" "--install"
882-
echo "Press any key when the installation has completed."
883-
getc
884-
execute_sudo "/usr/bin/xcode-select" "--switch" "/Library/Developer/CommandLineTools"
893+
if macos_sudoless_mode
894+
then
895+
abort "$(
896+
cat <<EOABORT
897+
Installing without sudo on macOS requires manual Command Line Tools installation. Run:
898+
899+
sudo xcode-select --install
900+
901+
Then run this installer script again.
902+
EOABORT
903+
)"
904+
else
905+
ohai "Installing the Command Line Tools (expect a GUI popup):"
906+
execute "/usr/bin/xcode-select" "--install"
907+
echo "Press any key when the installation has completed."
908+
getc
909+
execute_sudo "/usr/bin/xcode-select" "--switch" "/Library/Developer/CommandLineTools"
910+
fi
885911
fi
886912

887913
if [[ -n "${HOMEBREW_ON_MACOS-}" ]] && ! output="$(/usr/bin/xcrun clang 2>&1)" && [[ "${output}" == *"license"* ]]

0 commit comments

Comments
 (0)