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

I'd like to script the installation of 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.


Solution 1:

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

Solution 2:

  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).

Solution 3:

An attempt to clean up the accepted answer's style and logic, making it as version-independent as possible, using softwareupdate to its full capacity and introducing a re-usable os variable:

#!/bin/bash
# Requires root
os=$(sw_vers -productVersion | awk -F. '{print $1 "." $2}')
if softwareupdate --history | grep --silent "Command Line Tools.*${os}"; then
    echo 'Command-line tools already installed.' 
else
    echo 'Installing Command-line tools...'
    in_progress=/tmp/.com.apple.dt.CommandLineTools.installondemand.in-progress
    touch ${in_progress}
    product=$(softwareupdate --list | awk "/\* Command Line.*${os}/ { sub(/^   \* /, \"\"); print }")
    softwareupdate --verbose --install "${product}" || echo 'Installation failed.' 1>&2 && rm ${in_progress} && exit 1
    rm ${in_progress}
    echo 'Installation succeeded.'
fi

Solution 4:

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

Solution 5:

I found that if you install Home Brew, it will automatically install the command line tools.

I tested this using this vagrant image.

Run the following command to install Homebrew, and in doing so install the Command Line Tools.

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
  • I know the original question only asked how to install the Command Line Tools, but I don't know any mac these days that doesn't also have Home Brew installed on it.