Installing Xcode via command line

I am a Linux user and have never used OSX before. Someone asked me for help with installing some software on an OSX "Lion" server. I asked him to give me an SSH shell, assuming that would be sufficient. However things are more tricky than I thought.

I wanted to install homebrew, but it didn't install because there was no cc on the system. After some google, I suppose I need to install Xcode. I just downloaded xcode 4.5 from the apple developer site, and mounted it on the system. It contains a dir Xcode.app with a bunch of stuff in it. How do I go from here?

Can I install Xcode without physical access to the machine? All I really want is get homebrew running at this point.


Solution 1:

You might be better off just installing the standalone Developer Command Line Tools (which includes gcc and other standard developer toolchain items). You can get it from Apple's Developer Downloads (which requires a free developer account). Unfortunately there's no direct link, but just search for command line tools and you'll find it (note that there are different downloads for Lion and Mountain Lion).

The download is a DMG. Mount that, and you'll find a .mpkg file, which you can install via the command line: sudo installer -pkg "Command Line Tools.mpkg" -target /.

Solution 2:

I recently had to install Xcode command line tools on Mountain Lion over SSH. Here's how I did it.

  1. If you don't have a free Apple developer account, register for one

  2. Login to https://developer.apple.com/downloads

  3. Download the "Command Line Tools for Xcode" appropriate for your version of OSX

    For me, that was "Command Line Tools (OS X Mountain Lion) for Xcode - April 2014"

  4. Copy the dmg file to your remote

    In the following command, I'm using scp to securely copy the file from my local computer to the remote named remote

    $ scp ~/Downloads/command_line_tools_for_osx_mountain_lion_april_2014.dmg remote:Downloads/
    
  5. ssh to your remote

    $ ssh remote
    
  6. mount the dmg file on the remote

    Here, I'm using hdiutil to mount the image

    $ hdiutil attach ~/Downloads/command_line_tools_for_osx_mountain_lion_april_2014.dmg
    
  7. install the package contained in the dmg

    Here, installer must be run with sudo because this package needs to be installed on the root file system

    $ cd /Volumes/Command\ Line\ Tools\ \(Mountain\ Lion\)
    $ sudo installer -pkg Command\ Line\ Tools\ \(Mountain\ Lion\).mpkg -target /
    
  8. unmount the dmg file

    $ hdiutil detach /Volumes/Command\ Line\ Tools\ \(Mountain\ Lion\)
    
  9. delete the dmg file from the remote; optional

    I see no purpose keeping it around, but you can if you want.

    $ rm ~/Downloads/command_line_tools_for_osx_mountain_lion_april_2014.dmg
    

Solution 3:

This is pretty old, but for anyone who comes by this, there is a simple, built-in, one-liner to install the Command Line Tools without the need to download any .dmg or .pkgs. This is available even on a fresh install of macOS.

The command is:

xcode-select --install

According to the man page:

Opens a user interface dialog to request automatic installation of the command line developer tools.

It seems to have been available since Xcode 3.0 was around (OS X 10.5).