unit and acceptance testing, automation, productivity

How to install Xcode Command Line Tools without GUI or Xcode

If you are in the business of setting up new development machines or virtual machines you probably know that to do almost anything you need to have the Xcode Command Line Tools.

Being something that only devs need Apple doesn't ship them with OS X, as far as Yosemite at least, so you need to install them yourself.

The canonical way to do it is to install Xcode itself, but that means downloading a couple of GB of app from the store, which might take time.

Another option is to open a Terminal and type xcode-select --install.

Both the options above have a flaw: they require a GUI. That is not optimal when trying to setup a VM in an automated way.

In this AskDifferent answer a solution without GUI is proposed, and here you can find my spin on it:


# See

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/;
  PROD=$(softwareupdate -l |
    grep "\*.*Command Line" |
    head -n 1 | awk -F"*" '{print $2}' |
    sed -e 's/^ *//' |
    tr -d '\n')
  softwareupdate -i "$PROD" -v;
  echo "Xcode CLI tools OK"

Easy peasy ☺️ I also made a gist with this, which you can download here.

Happy coding, and leave the codebase better than you found it

Want more of these posts?

Subscribe to receive new posts in your inbox.