Skip to content

Instantly share code, notes, and snippets.

@mokagio
Created September 9, 2015 09:28
Show Gist options
  • Save mokagio/b974620ee8dcf5c0671f to your computer and use it in GitHub Desktop.
Save mokagio/b974620ee8dcf5c0671f to your computer and use it in GitHub Desktop.
Install Xcode CLI Tools without GUI
#!/bin/bash
# See http://apple.stackexchange.com/questions/107307/how-can-i-install-the-command-line-tools-completely-from-the-command-line
echo "Checking Xcode CLI tools"
# Only run if the tools are not installed yet
# To check that try to print the SDK path
xcode-select -p &> /dev/null
if [ $? -ne 0 ]; then
echo "Xcode CLI tools not found. Installing them..."
touch /tmp/.com.apple.dt.CommandLineTools.installondemand.in-progress;
PROD=$(softwareupdate -l |
grep "\*.*Command Line" |
head -n 1 | awk -F"*" '{print $2}' |
sed -e 's/^ *//' |
tr -d '\n')
softwareupdate -i "$PROD" -v;
else
echo "Xcode CLI tools OK"
fi
@damacguy
Copy link

First - Thanks for this script!

Second, weirdly on my M1 I'm getting both 12.4 and 12.5 of the command line tools showing up. LOL. But softwareupdate won't let me run the 12.4 (obviously). Another Big Sur (intel) machine only shows 12.5. Very weird. I may see if I can figure out how to choose the 'last' option if multiple are given, assuming that's the latest. I might even try to figure out how to grep the versions and test that to choose. But that all makes my head spin. :-p

Copy link

ghost commented Sep 6, 2021

FWIW I was using this to install on an old Mojave Mac mini over ssh. softwareupdate didn't recognize the -v option so I had to remove it for it to run.

Still, thanks for the help :-)

@That-Dude
Copy link

Thank you for the code. I had to modify this slightly for it to run on Monterey 12.0.1. I have not regression tested this but it would be simple to check the OS version and use your original code for versions prior to Monterey (if this code doesn't work for them).

#!/bin/bash

# See http://apple.stackexchange.com/questions/107307/how-can-i-install-the-command-line-tools-completely-from-the-command-line

echo "Checking Xcode CLI tools"
# Only run if the tools are not installed yet
# To check that try to print the SDK path
xcode-select -p &> /dev/null
if [ $? -ne 0 ]; then
  echo "Xcode CLI tools not found. Installing them..."
  touch /tmp/.com.apple.dt.CommandLineTools.installondemand.in-progress;
  PROD=$(softwareupdate -l |
    grep "\*.*Command Line" |
    tail -n 1 | sed 's/^[^C]* //')
    echo "Prod: ${PROD}"
  softwareupdate -i "$PROD" --verbose;
else
  echo "Xcode CLI tools OK"
fi

@jaygooby
Copy link

jaygooby commented Apr 11, 2022

Thanks for this! Had same issue of needing to install over ssh over vpn, so no chance to get at the GUI (also had to remove the -v)

@kalaklanar
Copy link

I tested the version from @That-Dude and it works under my VM of Big Sur 11.5.2. Catalina 10.15.3 fails.

@Antom91
Copy link

Antom91 commented Sep 24, 2023

Thank you for the code. I had to modify this slightly for it to run on Monterey 12.0.1. I have not regression tested this but it would be simple to check the OS version and use your original code for versions prior to Monterey (if this code doesn't work for them).

Work on macOS Catalina

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment