Skip to content

Instantly share code, notes, and snippets.

@trinitronx
Last active January 18, 2024 05:20
  • Star 50 You must be signed in to star a gist
  • Fork 16 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save trinitronx/6217746 to your computer and use it in GitHub Desktop.
Script to download & install XCode Command Line tools on OSX 10.7 or 10.8. Lifted from jedi4ever/veewee template.
#!/bin/sh
# 2021-12-09:
# This script is no longer supported!
# Apple broke all direct downloads without logging with an Apple ID first.
# The number of hoops that a script would need to jump through to login,
# store cookies, and download is prohibitive.
# Now we all must manually download and mirror the files for this to work at all :'-(
OSX_VERS=$(sw_vers -productVersion | awk -F "." '{print $2}')
# Get Xcode CLI tools
# https://devimages.apple.com.edgekey.net/downloads/xcode/simulators/index-3905972D-B609-49CE-8D06-51ADC78E07BC.dvtdownloadableindex
# https://developer.apple.com/downloads/index.action
TOOLS=clitools.dmg
if [ ! -f "$TOOLS" ]; 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
elif [ "$OSX_VERS" -eq 8 ]; then
DMGURL=http://devimages.apple.com/downloads/xcode/command_line_tools_for_xcode_os_x_mountain_lion_april_2013.dmg
elif [ "$OSX_VERS" -eq 9 ]; then
DMGURL=http://adcdownload.apple.com/Developer_Tools/command_line_tools_os_x_mavericks_for_xcode__late_october_2013/command_line_tools_os_x_mavericks_for_xcode__late_october_2013.dmg
elif [ "$OSX_VERS" -eq 10 ]; then
DMGURL=http://adcdownload.apple.com/Developer_Tools/Command_Line_Tools_OS_X_10.10_for_Xcode_6.3.2/commandlinetoolsosx10.10forxcode6.3.2.dmg
elif [ "$OSX_VERS" -eq 11 ]; then
DMGURL=http://adcdownload.apple.com/Developer_Tools/Command_Line_Tools_OS_X_10.11_for_Xcode_7.3.1/Command_Line_Tools_OS_X_10.11_for_Xcode_7.3.1.dmg
elif [ "$OSX_VERS" -eq 12 ]; then
DMGURL=http://adcdownload.apple.com/Developer_Tools/Command_Line_Tools_macOS_10.12_for_Xcode_8.1/Command_Line_Tools_macOS_10.12_for_Xcode_8.1.dmg
elif [ "$OSX_VERS" -eq 14 ]; then
DMGURL=https://download.developer.apple.com/Developer_Tools/Command_Line_Tools_for_Xcode_11_GM_Seed/Command_Line_Tools_for_Xcode_11_GM_Seed.dmg
fi
curl "$DMGURL" -o "$TOOLS"
fi
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"
exit
@rxw1
Copy link

rxw1 commented Oct 6, 2013

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

(thank you)

@xbeta
Copy link

xbeta commented Jan 7, 2014

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

@trinitronx
Copy link
Author

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/Xcode.app 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.

@TheTechRobo
Copy link

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
/tmp/clitools.9QjD/Packages/BluetoothSDK.pkg
/tmp/clitools.9QjD/Packages/CoreAudioSDK.pkg
/tmp/clitools.9QjD/Packages/DeveloperToolsCLI.pkg
/tmp/clitools.9QjD/Packages/DevSDK.pkg
/tmp/clitools.9QjD/Packages/FireWireSDK.pkg
/tmp/clitools.9QjD/Packages/JavaSDK.pkg
/tmp/clitools.9QjD/Packages/OpenGLSDK.pkg
/tmp/clitools.9QjD/Packages/QuickTimeSDK.pkg
/tmp/clitools.9QjD/Packages/WebKitSDK.pkg
/tmp/clitools.9QjD/Packages/X11Documentation.pkg
/tmp/clitools.9QjD/Packages/X11SDK.pkg'.
"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?)

@trinitronx
Copy link
Author

@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 :-)

@trinitronx
Copy link
Author

@xbeta @TheTechRobo

Apple broke script access to all adcdownload.apple.com and download.developer.apple.com URLs. They now respond with an HTML page and force sign in with an Apple ID which is rather prohibitive to do in an unattended way.

<title>Unauthorized - Apple Developer</title>
        <meta name="omni_page" content="Unauthorized" />
        <meta name="Description" content="The page you requested does not exist.">
[...SNIP...]
<main class="main" data-page-type="overview" id="main" role="main">
                <h1 class="section-headline typography-headline">Sorry, you cannot view this page.</h1>
                <p class="intro">The Apple ID you signed in with does not have permission to view this page.</p>
                <p>If you’re currently a member of the Apple Developer Program, you or your <span class="nowrap">Account Holder</span> may need to update your account by agreeing to the latest license agreement in order to access this page. To view your current membership status and benefits, <a href="/account/?view=membership#/welcome">visit your account</a>.</p>
        </main>

I'm looking for alternatives, but have yet to find any that don't already depend on Xcode and CLT packages... which defeats the purpose of an automated script. Anyone who manages Apple CI / CD build nodes is probably stuck in the same chicken-egg problem with installing Xcode + Command Line Tools now. 😞
Are we stuck with doing this manually now? 🤷

@TheTechRobo
Copy link

Might work if you pass in a cookies.txt file to curl ... the -b cookies.txt option should do it.

Just make sure you export it in the old Netscape format. Lots of FF and Chrome extensions to do this.

You can just login with an apple id and export the cookies.txt then pass it in.

Not pretty but it would probably work.

Unforutnately I can't test - I moved away from mac completely recently.

@trinitronx
Copy link
Author

@TheTechRobo yeah that would probably work. However, it defeats the original purpose of this script: unattended automated Xcode installations.

This type of broken download URL problem is why Homebrew doesn't accept certain apps. It's too much of a nightmare to keep things working when URLs break, move behind paywall or login, redirect to unverified third-party domain, etc...

@TheTechRobo
Copy link

Would be better than nothing tho, especially sinc eyou can use the same cookies file for many machines

@trinitronx
Copy link
Author

So, I figured out another way around having to maintain URLs for the CLT packages... I ended up deprecating this script and using an AppleScript instead

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