Skip to content

Instantly share code, notes, and snippets.

@zencd
Created October 15, 2024 10:19
Show Gist options
  • Save zencd/c479b037ab8ecdd6616968dc5ff1bc38 to your computer and use it in GitHub Desktop.
Save zencd/c479b037ab8ecdd6616968dc5ff1bc38 to your computer and use it in GitHub Desktop.
install xcode command line tools from command line
function install_xcode_command_line_tools_if_not_yet() {
# install xcode cli tools via terminal
# try the gui installer if failed
# original hack: https://github.com/Homebrew/install/blob/master/install.sh
if xcode-select -p 1>/dev/null 2>&1; then
return
fi
installed=0
tmp_file=/tmp/.com.apple.dt.CommandLineTools.installondemand.in-progress
touch "$tmp_file"
label=$(softwareupdate -l | grep -B 1 -E 'Command Line Tools' | awk -F'*' '/^ *\\*/ {print $2}' | sed -e 's/^ *Label: //' -e 's/^ *//' | sort -V | tail -n1)
# $label is like 'Command Line Tools for Xcode-16.0'
if [ -n "$label" ]; then
(set -x; softwareupdate -i "$label")
installed=1
fi
rm -f "$tmp_file"
if [ "$installed" = "0" ]; then
(set -x; xcode-select --install)
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment