Balaji Vajjala's Blog

A DevOps Blog from Trenches

Install Xcode CLI Tools via Script

Well just got a mac from my new project FB Status… – couldn’t resist it :)

One of the tasks was to setup an environment, and learn what a mac is :), a few days later I’m in love but still I need a way to destroy an recover my environment. I have a few plans like bash_it / oh_my_zsh & others, but until then I put togter a few scripts [ more to come ], the first of them is this one installing Xcode CLI Tools on Mac OSX Mountain Lion.

Install or (un-install) with the following simple command:

1
2
 sudo bash < <(curl -L https://raw.github.com/hagzag/xcode-cli-install/master/install.sh)
  sudo bash < <(curl -L https://raw.github.com/hagzag/xcode-cli-install/master/uninstall.sh)

The essence … see the rest on Github

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
detect_osx_version() {
  result=`sw_vers -productVersion`
  ...
  osxversion="10.8"
  osxvername="Mountain Lion"
  cltools=xcode462_cltools_10_86938259a.dmg
  mountpath="/Volumes/Command Line Tools (Mountain Lion)"
  mpkg="Command Line Tools (Mountain Lion).mpkg"
  pkg_url="https://www.dropbox.com/s/hw45wvjxrkrl59x/xcode462_cltools_10_86938259a.dmg"
  pkgmd5="90c5db99a589c269efa542ff0272fc28"
  ...
}
download_tools () {
  if [ -f /tmp/$cltools ]; then
    if [ `md5 -q /tmp/$cltools` = "${pkgmd5}" ]; then
      echo -e "$info $cltools already downloaded to /tmp/$cltools."
    else
       rm -f /tmp/$cltools
    fi
  else
    cd /tmp && wget $pkg_url -O ./$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"

  gcc_bin=`which gcc`
  gcc --version &>/dev/null && echo -e "$info gcc found in $gcc_bin"
}

I took the liberty (not sure it’s mine to take … but still), locating Xcode CLI tools for both mountain lion (10.8) and Lion (10.7) on DropBox, ecause Apple (like Oracle and other Giants) won’t let you download without a userid / pass which is quite irretating …

As Always hope you guy’s enjoy this. HP