38

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.

  • 1
    Are you trying to do this for an internal script or for some kind of distribution? The reason I ask is because the developer tools require a (free) developer account to access and distributing them outside of that would violate Apple's licensing agreement. – Colin C. Oct 28 '13 at 17:40
  • 1
    @ColinC. xcode-select --install does not require an Apple Developer account. It may require the acceptance of the license agreement before the tools are used, but it downloads them without an account. – also Oct 28 '13 at 20:25
24

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 doesnt 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
    This script appears to be the original source for the code and has additional code that will work on versions of Mac OS X below 10.9: github.com/timsutton/osx-vm-templates/blob/… – finn Nov 1 '15 at 20:32
  • 1
    sadly it's pretty typical for this type of stuff to be broken every new release, so no real surprise..... – keen Dec 14 '16 at 19:43
  • 3
    For 10.12 (Sierra), all I had to change here was for "-v" to become "--verbose" – macetw Apr 3 '17 at 14:44
  • 3
    Great answer, works for me on Sierra! :-) However, you might want to rm /tmp/.com.apple.dt.CommandLineTools.installondemand.in-progress after running softwareupdate -i ... , otherwise subsequent calls to softwareupdate -l display Command Line Tools ... as an available update. This (supposed) update does not seem to be shown in the App Store, possibly because softwareupdate -l only lists "system" updates, not application updates. – ssc Dec 6 '17 at 10:39
  • 1
    @thaimin I dont have any repro cases that return multiple results - but I posted an update with a possible fix for you. one of the challenges is that apple has been very inconsistent with how they name these over the years, so there's really nothing super consistent to sort or filter on... – keen Jan 22 at 16:31
18
  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).

  • 1
    I would like this to be future proof, and I don't want to have to provide my own copy of the tools disk image. xcode-select --install downloads and installs the tools for the user, and it doesn't require an ADC . I want to do exactly this, but without the GUI. – also Oct 28 '13 at 20:32
  • Found points 1 and 2 very useful for installing the Hardware IO Tools. – rstackhouse Jan 15 '16 at 2:50
5

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
  • 1
    macos 10.11 I get the error "osascript is not allowed on assistive access" – ClintM Nov 10 '17 at 14:54
4

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
  • just a note: the sw_vers filter doesnt work on various releases before 10.10. (I can't speak to 10.11-10.13, only tested today on 10.9 10.10 and 10.14) (see my updated answer for some examples) – keen Jan 22 at 16:34
2

This is what I use, partly based on brysgo's answer.

sudo sqlite3 /Library/Application\ Support/com.apple.TCC/TCC.db \
    "INSERT or REPLACE INTO access(service,client,client_type,allowed,prompt_count) VALUES('kTCCServiceAccessibility','com.apple.Terminal',0,1,1);"

if [[ "$(xcode-select -p)" == "" ]]; then
  printf "\n### Installing Xcode command line tools\n"
  xcode-select --install
  sleep 2
  osascript <<EOD
    tell application "System Events"
      tell process "Install Command Line Developer Tools"
        click button "Install" of window ""
        click button "Agree" of window "License Agreement"
        repeat until exists button "Done" of window ""
          delay 1
        end repeat
        click button "Done" of window ""
      end tell
    end tell
EOD
fi

update: Apple keeps adding new columns to the access database, you only need to specify values for the first 5, updated the code to reflect this.

1

A couple more alternatives may be found at these answers:

Download XCode Command Line Tools

You can download the XCode 4.x CLTools packages from their official download site via direct links.

Here is a script that will automate the install process for you 1.

To find updated links for the tools, you can use this command:

curl -Ls https://devimages.apple.com.edgekey.net/downloads/xcode/simulators/index-3905972D-B609-49CE-8D06-51ADC78E07BC.dvtdownloadableindex | plutil -convert json -o - - | python -mjson.tool | less

[1] Please don't abuse/overload their servers

You may find that it is easier to first Login to the Apple Developer Download Site and download the latest Command Line Tools available there. You can then host the .dmg on your own package repo server and be able to reliably install the .dmg on many machines with hdiutil without having to login to developer.apple.com.

As of 06/10/2015, the link to the latest Command Line Tools dmg is:

http://adcdownload.apple.com/Developer_Tools/Command_Line_Tools_OS_X_10.10_for_Xcode_6.3.2/commandlinetoolsosx10.10forxcode6.3.2.dmg

  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – grg Jun 7 '15 at 9:21
  • 1
    @grgarside: Took some time to update this script with the latest Yosemite / XCode 6.3.2 CLT dmg, and copied essential parts of the answer locally to this answer. – TrinitronX Jun 10 '15 at 17:06
  • @grgarside: I realized the linking issue & that the script was probably out of date, but didn't have time to update it on the same day I initially posted. I just wanted to give those links as a good place to start and as a note to self to go back and update & test things again. – TrinitronX Jun 10 '15 at 17:10
1

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .