Home > Mac administration, Mac OS X, Scripting, Xcode > Installing the Xcode command line tools on 10.7.x and later

Installing the Xcode command line tools on 10.7.x and later

A number of Mac admins need to provide the Xcode Command Line Tools for the Macs in their environments, either as part of building machines or afterwards. To help with this task, I’ve developed a script that will download and install the Xcode Command Line Tools on Macs running 10.7.x and higher. See below the jump for more details.

The script will perform different tasks, depending on which version of OS X it’s being run on.

On OS X 10.9.x and 10.10.x:

  1. Creates a placeholder file in /tmp. This file’s existence is checked by the softwareupdate tool before allowing the installation of the Xcode command line tools.
  2. Runs the softwareupdate tool and checks for the latest version of the Xcode command line tools for the OS in question.
  3. Uses the softwareupdate tool to install the latest version of the Xcode command line tools for the OS in question.
  4. Removes the placeholder file stored in /tmp.

On Mac OS X 10.7.x and 10.8.x:

  1. Uses curl to download a disk image containing the specified Xcode Command Line Tools installer from Apple’s web site.
  2. Renames the downloaded disk image to cltools.dmg.
  3. Mounts the disk image silently in /tmp. Disk image will not be visible to any logged-in user.
  4. Installs the Xcode Command Line Tools using the installer package stored on the disk image
  5. After installation, unmounts the disk image and removes it from the Mac in question.

Note: This script should not be used as part of a payload-free installer package. On 10.9.x and 10.10.x, the softwareupdate tool will not work properly when called from within an installer package.

The script is available below. It’s also available from my Github repo from the following link:

https://github.com/rtrouton/rtrouton_scripts/tree/master/rtrouton_scripts/install_xcode_command_line_tools


#!/bin/bash
# Installing the Xcode command line tools on 10.7.x or higher
osx_vers=$(sw_vers -productVersion | awk -F "." '{print $2}')
cmd_line_tools_temp_file="/tmp/.com.apple.dt.CommandLineTools.installondemand.in-progress"
# Installing the latest Xcode command line tools on 10.9.x or higher
if [[ "$osx_vers" -ge 9 ]]; then
# Create the placeholder file which is checked by the softwareupdate tool
# before allowing the installation of the Xcode command line tools.
touch "$cmd_line_tools_temp_file"
# Find the last listed update in the Software Update feed with "Command Line Tools" in the name
cmd_line_tools=$(softwareupdate -l | awk '/\*\ Command Line Tools/ { $1=$1;print }' | tail -1 | sed 's/^[[ \t]]*//;s/[[ \t]]*$//;s/*//' | cut -c 2-)
#Install the command line tools
softwareupdate -i "$cmd_line_tools" -v
# Remove the temp file
if [[ -f "$cmd_line_tools_temp_file" ]]; then
rm "$cmd_line_tools_temp_file"
fi
fi
# Installing the latest Xcode command line tools on 10.7.x and 10.8.x
# on 10.7/10.8, instead of using the software update feed, the command line tools are downloaded
# instead from public download URLs, which can be found in the dvtdownloadableindex:
# https://devimages.apple.com.edgekey.net/downloads/xcode/simulators/index-3905972D-B609-49CE-8D06-51ADC78E07BC.dvtdownloadableindex
if [[ "$osx_vers" -eq 7 ]] || [[ "$osx_vers" -eq 8 ]]; then
if [[ "$osx_vers" -eq 7 ]]; then
DMGURL=http://devimages.apple.com/downloads/xcode/command_line_tools_for_xcode_os_x_lion_april_2013.dmg
fi
if [[ "$osx_vers" -eq 8 ]]; then
DMGURL=http://devimages.apple.com/downloads/xcode/command_line_tools_for_osx_mountain_lion_april_2014.dmg
fi
TOOLS=cltools.dmg
curl "$DMGURL" -o "$TOOLS"
TMPMOUNT=`/usr/bin/mktemp -d /tmp/clitools.XXXX`
hdiutil attach "$TOOLS" -mountpoint "$TMPMOUNT" -nobrowse
# The "-allowUntrusted" flag has been added to the installer
# command to accomodate for now-expired certificates used
# to sign the downloaded command line tools.
installer -allowUntrusted -pkg "$(find $TMPMOUNT -name '*.mpkg')" -target /
hdiutil detach "$TMPMOUNT"
rm -rf "$TMPMOUNT"
rm "$TOOLS"
fi
exit 0

view raw

gistfile1.sh

hosted with ❤ by GitHub

  1. February 3, 2015 at 3:46 pm

    Reblogged this on SutoCom Solutions.

  2. February 4, 2015 at 2:27 pm

    Reblogged this on /home/kOoLiNuS and commented:
    As always a great post on Der Flounder. Check it out!

  3. Brad Vrooman
    February 4, 2015 at 2:45 pm

    Great script! Thanks for writing this.

    I did notice a small error on line 27 – the cmd_line_tools_temp_file variable is missing its $, so the temp file doesn’t get deleted. It took me a few minutes to figure out why my test machines kept reporting software updates over and over during recon.

  4. February 24, 2015 at 4:30 am

    There’s no need to download the disk image, everything hosted at http://devimages.apple.com is seekable.

    “`
    hdiutil attach $DMGURL -mountpoint “$TMPMOUNT” -nobrowse
    “`

  5. niko
    July 19, 2016 at 11:04 pm

    I am newbie and sorry but lost in translation >.. i mean where and how i have access the to this command line tools now
    i spent all night on this
    if you pleeeeaaaase can help me it will be great .
    Some proms also when terminal ask me root accesss this was solved it seems
    again ur help would be greatly appreciated
    thanks a lot

  1. No trackbacks yet.

Leave a comment