Apple - How can I install the Command Line Tools completely from the command line?

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

  1. Download the Command Line Tools package from the Apple Developer site.

  2. Mount the downloaded1 DMG:

    hdiutil attach "command_line_tools_os_x_mountain_lion_for_xcode__october_2013.dmg"
    
  3. Run the installer via the command line:

    cd "/Volumes/Command Line Tools (Mountain Lion)"
    installer -verbose -pkg "Command Line Tools (Mountain Lion).mkpg" -target /
    
  4. When finished, unmount the DMG and delete the download.

    1. Eject the DMG2.

      cd /
      hdiutil detach /dev/disk3s2
      
    2. Delete the DMG.

      rm "command_line_tools_os_x_mountain_lion_for_xcode__october_2013.dmg"
      

1 At the time of writing, the downloaded DMG and package name is accurate, however in the future, the name may change. Remember to alter the command (or use tab-completion) to fill in the relevant portion of the command.
2 If the command does not work, check the disk matches the name of the mount (as per mount).


An alternative is to use this applescript I wrote:

https://gist.github.com/brysgo/9007731

do shell script "xcode-select --install"
do shell script "sleep 1"

tell application "System Events"
    tell process "Install Command Line Developer Tools"
        keystroke return
        click button "Agree" of window "License Agreement"
    end tell
end tell

xcode-select --install
sleep 1
osascript <<EOD
  tell application "System Events"
    tell process "Install Command Line Developer Tools"
      keystroke return
      click button "Agree" of window "License Agreement"
    end tell
  end tell
EOD