Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Script to download & install XCode Command Line tools on OSX 10.7 or 10.8. Lifted from jedi4ever/veewee template.
OSX_VERS=$(sw_vers -productVersion | awk -F "." '{print $2}')
# Get Xcode CLI tools
if [ ! -f "$TOOLS" ]; then
if [ "$OSX_VERS" -eq 7 ]; then
elif [ "$OSX_VERS" -eq 8 ]; then
elif [ "$OSX_VERS" -eq 9 ]; then
elif [ "$OSX_VERS" -eq 10 ]; then
elif [ "$OSX_VERS" -eq 11 ]; then
elif [ "$OSX_VERS" -eq 12 ]; then
elif [ "$OSX_VERS" -eq 14 ]; then
curl "$DMGURL" -o "$TOOLS"
TMPMOUNT=`/usr/bin/mktemp -d /tmp/clitools.XXXX`
hdiutil attach "$TOOLS" -mountpoint "$TMPMOUNT"
installer -pkg "$(find $TMPMOUNT -name '*.mpkg' -o -name '*.pkg')" -target /
hdiutil detach "$TMPMOUNT"
rm -rf "$TMPMOUNT"
rm "$TOOLS"

This comment has been minimized.

Copy link

@rwilhelm rwilhelm commented Oct 6, 2013

curl -Ls | plutil -convert json -o - - | python -mjson.tool | less

(thank you)


This comment has been minimized.

Copy link

@xbeta xbeta commented Jan 7, 2014

Thanks for this! But it's only for CLI , is there something for upgrading xcode from command-line too?


This comment has been minimized.

Copy link
Owner Author

@trinitronx trinitronx commented Aug 5, 2014

@xbeta: Something like this should do what you want. If you're upgrading, you'll have to make sure to delete the old /Applications/ application bundle first.

As I've since left my last company, those XCode URLs may not work forever, in which case you might want to download them from the App Store and mirror them yourself.

The XCode dmg files were found via an excellent trick from Brian @cunnie at Pivotal Labs. Since XCode is a very large download, you should have enough time to grab it before the App Store app deletes the dmg. Just make sure that you're getting the complete file though! You can use a bash while loop with sleep to poll for the existence of the final download file and then copy it somewhere.

You also might be interested in this great list of official versioned xcode URLs from Ben Chatelain's xcode-installer gem.


This comment has been minimized.

Copy link

@TheTechRobo TheTechRobo commented May 28, 2020

@trinitronx, @xbeta: Broken link, use this permalink

And @trinitronx: THANK YOU SO MUCH, Xcode's dmg is WAY TOO MUCH FILESIZE...

Oh and one more thing: you may want to add 10.15 and 10.13 so it's future-proof 😉

(EDIT: It doesn't work on 10.7.5:

# computer @ Anfunfuns-MacBook-Air in ~ [19:53:13]
$ zsh InstallXcodeCommandlineTools.zsh
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  139M  100  139M    0     0   374k      0  0:06:21  0:06:21 --:--:--  590k
Checksumming Driver Descriptor Map (DDM : 0)…
     Driver Descriptor Map (DDM : 0): verified   CRC32 $3FD89CD1
Checksumming  (Apple_Free : 1)…
                    (Apple_Free : 1): verified   CRC32 $00000000
Checksumming Apple (Apple_partition_map : 2)…
     Apple (Apple_partition_map : 2): verified   CRC32 $C83292EA
Checksumming disk image (Apple_HFS : 3)…
          disk image (Apple_HFS : 3): verified   CRC32 $A65E85CD
Checksumming  (Apple_Free : 4)…
                    (Apple_Free : 4): verified   CRC32 $00000000
verified   CRC32 $A35EE7B6
/dev/disk2          	Apple_partition_scheme
/dev/disk2s1        	Apple_partition_map
/dev/disk2s2        	Apple_HFS                      	/private/tmp/clitools.9QjD
installer: Error the package path specified was invalid: '/tmp/clitools.9QjD/Command Line Tools (Lion).mpkg
"disk2" unmounted.
"disk2" ejected.

(fixed it by copy-pasting the os x 10.7 url from the file into curl -O, but could you fix this?)


This comment has been minimized.

Copy link
Owner Author

@trinitronx trinitronx commented May 29, 2020

@TheTechRobo: Yeah, this hasn't been updated in a while... I haven't had to bootstrap from scratch since 10.14, and my company is stubbornly refusing to let us upgrade to 10.15 Catalina.

I've fixed the link rot for the Pivotal post for now. Maybe one day I can get a change to upgrade & test this.

I do have a sprout-wrap fork where this Gist script is used as a sub-component to bootstrap Xcode CLT before git is available. The reason it's done that way is to avoid a dependency on git, but I should probably pull this script into the repo and just use the raw link for that.

I don't have time right now to work on this, but I do accept Pull Requests :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment