Skip to content

Instantly share code, notes, and snippets.

@rfink
Created March 13, 2015 17:16
Show Gist options
  • Save rfink/fb742902028aa76ce09f to your computer and use it in GitHub Desktop.
Save rfink/fb742902028aa76ce09f to your computer and use it in GitHub Desktop.
Mac OS/X install command line tools
#!/bin/bash
# install_cltools.sh
# This script will install Apple Command Line Tools in
# an automated fashion after downloading the install
# dmg from an accessible web server.
# This is a webserver from which the installers can be
# downloaded. Unfortunately, Apple has a lame habit
# of only making these utilities available for download
# by logging into their website, so you'll need to
# download them there and then move them onto a web
# server that this script can access. PROTIP: You can
# even use localhost by starting a quick python
# webserver from the ~/Downloads directory on your Mac.
# For example:
# cd ~/Downloads; python -m SimpleHTTPServer 8080
webserver="http://localhost:8080/"
info="[info]"
warning="[warning]"
error="[error]"
check_root() {
if [[ $EUID -ne 0 ]]; then
echo "$error This script must be run as root" 1>&2
exit 1
fi
}
detect_osx_version() {
result=`sw_vers -productVersion`
if [[ $result =~ "10.7" ]]; then
osxversion="10.7"
osxvername="Lion"
cltools=xcode46cltools_10_76938132a.dmg
mountpath="/Volumes/Command Line Tools (Lion)"
mpkg="Command Line Tools (Lion).mpkg"
elif [[ $result =~ "10.8" ]]; then
osxversion="10.8"
osxvername="Mountain Lion"
cltools=xcode46cltools_10_86938131a.dmg
mountpath="/Volumes/Command Line Tools (Mountain Lion)"
mpkg="Command Line Tools (Mountain Lion).mpkg"
else
echo "$error This machine is running an unsupported version of OS X" 1>&2
exit 1
fi
echo -e "$info Detected OS X $osxversion $osxvername"
}
check_tools() {
RECEIPT_FILE=/var/db/receipts/com.apple.pkg.DeveloperToolsCLI.bom
[ -f "$RECEIPT_FILE" ] && echo "$info Command Line Tools are already installed. Exiting..." && exit 0
}
download_tools () {
# Use curl to download the appropriate installer to tmp
if [ ! -f /tmp/$cltools ]; then
echo -e "$info Downloading Command Line Tools for Mac OS X $osxversion"
cd /tmp && curl -O $webserver/$cltools
else
echo -e "$info $cltools already downloaded to /tmp/$cltools."
fi
}
install_tools() {
# Mount the Command Line Tools dmg
echo -e "$info Mounting Command Line Tools..."
hdiutil mount -nobrowse /tmp/$cltools
# Run the Command Line Tools Installer
echo -e "$info Installing Command Line Tools..."
installer -pkg "$mountpath/$mpkg" -target "/Volumes/Macintosh HD"
# Unmount the Command Line Tools dmg
echo -e "$info Unmounting Command Line Tools..."
hdiutil unmount "$mountpath"
}
cleanup () {
rm /tmp/$cltools
echo "$info Cleanup complete."
exit 0
}
# Make sure only root can run our script
check_root
# Detect and set the version of OS X for the rest of the script
detect_osx_version
# Check for if tools are already installed by looking for a receipt file
check_tools
# Check for and if necessary download the required dmg
download_tools
# Start the appropriate installer for the correct version of OSX
install_tools
# Cleanup files used during script
cleanup
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment