William Payne
William Payne

Reputation: 3325

How do I automate the installation of Xcode?

I am trying to write a shell script that will install all of our development tools & dependencies onto a clean OSX machine.

Does anybody know the best approach to automate the installation of Xcode?

I am doing this to:

  1. Document the development environment.
  2. Speed up the on-boarding process for new developers.
  3. Follow the automate-everything principle.

Upvotes: 7

Views: 8148

Answers (5)

davidcondrey
davidcondrey

Reputation: 35983

# Install Command-line tools as dependency for Homebrew
xcode-select --install # Sets the development directory path to /Library/Developer/CommandLineTools

# Install Homebrew
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
# Install Mas (command-line interface for Mac App Store)
brew install mas

# Search for Xcode showing only the first 5 results
mas search xcode | head -5
# Install Xcode using App ID
mas install 497799835 # The appid for Xcode shown when doing search

sudo xcode-select -r  # Reset the development directory path to put to Xcode /Applications/Xcode.app/Contents/Developer

#sudo xcodebuild -license

# Updaate all Apple software and auto agree to any licenses and restart if necessary
sudo softwareupdate --install --agree-to-license -aR

Upvotes: 0

River2202
River2202

Reputation: 1407

Well, just for who reach this page, there is a gem build by one of author of Fastlane to automatic this process.

https://github.com/KrauseFx/xcode-install

Upvotes: 4

Rockholla
Rockholla

Reputation: 61

A lot of things of things out there for automating both downloading and installing are outdated. I've been at work on something for another project and think I have a pretty good solution working:

https://github.com/rockholla/macosa/blob/master/bin/macosa_xcode

And here's the download part:

https://github.com/rockholla/macosa/blob/master/bin/macosa_xcode_download

Upvotes: 0

TrinitronX
TrinitronX

Reputation: 5213

Fully Automated XCode Installation

First, login to the Apple Developer Downloads Site and get the version of XCode.dmg that works for your version of OSX. You might want to grab a couple versions to support the different OSX platforms (e.g.: 10.10 Yosemite, 10.9 Mavericks, etc...). Upload the dmg to a file host you can access (e.g.: Amazon S3, Dropbox, your shared hosting provider of choice, etc...)

Change the DOWNLOAD_BASE_URL in the following script, and rename the dmgs appropriately with the version & build number or add it to the script below:

#!/bin/bash
DOWNLOAD_BASE_URL=http://example.com/path/to/xcode/dmgs/

## Figure out OSX version (source: https://www.opscode.com/chef/install.sh)
function detect_platform_version() {
  # Matching the tab-space with sed is error-prone
  platform_version=$(sw_vers | awk '/^ProductVersion:/ { print $2 }')

  major_version=$(echo $platform_version | cut -d. -f1,2)

  # x86_64 Apple hardware often runs 32-bit kernels (see OHAI-63)
  x86_64=$(sysctl -n hw.optional.x86_64)
  if [ $x86_64 -eq 1 ]; then
    machine="x86_64"
  fi
}

detect_platform_version

# Determine which XCode version to use based on platform version
case $platform_version in
  "10.10") XCODE_DMG='XCode-6.1.1-6A2008a.dmg' ;;
  "10.9")  XCODE_DMG='XCode-5.0.2-5A3005.dmg'  ;;
  *)       XCODE_DMG='XCode-5.0.1-5A2053.dmg'  ;;
esac

# Bootstrap XCode from dmg
if [ ! -d "/Applications/Xcode.app" ]; then
  echo "INFO: XCode.app not found. Installing XCode..."
  if [ ! -e "$XCODE_DMG" ]; then
    curl -L -O "${DOWNLOAD_BASE_URL}/${XCODE_DMG}"
  fi

  hdiutil attach "$XCODE_DMG"
  export __CFPREFERENCES_AVOID_DAEMON=1
  sudo installer -pkg '/Volumes/XCode/XCode.pkg' -target /
  hdiutil detach '/Volumes/XCode'
fi

You might be interested in installing the XCode Command Line Tools, and bypassing the XCode License Agreement also:

curl -Ls https://gist.github.com/trinitronx/6217746/raw/58456d6675e437cebbf771c60b6005b4491a0980/xcode-cli-tools.sh | sudo bash

# We need to accept the xcodebuild license agreement before building anything works
# Silly Apple...
if [ -x "$(which expect)" ]; then
  echo "INFO: GNU expect found! By using this script, you automatically accept the XCode License agreement found here: http://www.apple.com/legal/sla/docs/xcode.pdf"
  expect ./bootstrap-scripts/accept-xcodebuild-license.exp
else
  echo -e "\x1b[31;1mERROR:\x1b[0m Could not find expect utility (is '$(which expect)' executable?)"
  echo -e "\x1b[31;1mWarning:\x1b[0m You have not agreed to the Xcode license.\nBuilds will fail! Agree to the license by opening Xcode.app or running:\n
    xcodebuild -license\n\nOR for system-wide acceptance\n
    sudo xcodebuild -license"
  exit 1
fi

Alternative Method

An alternative is to use this applescript I created.

To use:

git clone https://gist.github.com/6237049.git
cd 6237049/
# Edit the script with AppleScript Editor to replace your APPLE ID and PASSWORD
osascript Install_XCode.applescript

You might also be interested in my answer here: How download and Install Command Line Tools for Xcode.

I would recommend the other method over the applescript method. The applescript depends on a UI element hierarchy that may not always stay the same for future versions of the App Store app on OSX. It just seems fragile to me. Installing XCode via the command line via a dmg is probably the most reliable way.

Upvotes: 6

Jason Coco
Jason Coco

Reputation: 78363

Starting with Xcode 4.3, it's delivered as an app bundle. The first time you launch the app, it will continue the installation. Since it's a beta, it's still not fixed, but currently the post-launch installation packages are in the app bundle inside a directory called Contents/Resources/Packages.

So, depending on your environment, your install script can just copy the Xcode.app bundle into the Applications directory and your users can complete the installation the first time they launch it. Or, you can copy the bundle and then manually install the additional packages using the installer(8) utility. For more information on using installer, see the man pages.

Upvotes: 2

Related Questions