MacOS – How to install the Command Line Tools completely from the command line

command lineinstallmacosterminalxcode

I'd like to script the installation of the the Xcode Command Line Tools.

On Mavericks,

xcode-select --install

will open a dialog prompting the user to install, but I'd like to trigger the install without the dialog, like using the softwareupdate command.

Is there a way to do this?

Edit:

Specifically, xcode-select --install launches an application that downloads and installs the tools without requiring the user to manually download them or have an Apple Developer account. It appears to use /System/Library/CoreServices/Install Command Line Developer Tools.app to do this.

I would like to use the same mechanism that Apple is, but without the GUI. I do not want to have to manually download the .dmg containing the tools as this seems fragile. Apple provides several other command-line tools, like softwareupdate and install that download or install software directly from Apple, and I'm looking to find the same for this type of distribution.

Best Answer

Wish I could claim credit for this one, but I found it buried in https://github.com/chcokr/osx-init/blob/master/install.sh

This worked on my 10.10 headless VM without a logged in UI. Updates applied for compatibility with at least 10.9-10.14

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" --verbose
rm /tmp/.com.apple.dt.CommandLineTools.installondemand.in-progress

This presumes you only have 1 result to softwareupdate -l | grep "\*.*Command Line" - if this returns multiple versions, you might need more specific logic. (I dont have a repro case)

one variation that seems to work (limited testing) on 10.10-10.14 (10.9 doesn't return an osx version number in the cli tools name..so this doesn't work there):

PROD=$(softwareupdate -l |
 grep "\*.*Command Line.*$(sw_vers -productVersion|awk -F. '{print $1"."$2}')" |
 head -n 1 | awk -F"*" '{print $2}' |
 sed -e 's/^ *//' |
 tr -d '\n')

a few example results:

* Command Line Tools (OS X Mavericks)-6.2
* Command Line Tools (OS X 10.10) for Xcode-7.2
* Command Line Tools (macOS El Capitan version 10.11) for Xcode-8.2
* Command Line Tools (macOS High Sierra version 10.13) for Xcode-10.1
* Command Line Tools (macOS Mojave version 10.14) for Xcode-10.1