December 23, 2021

Managing Xcode on the Command Line

xcode-install is a nifty tool to install and manage Xcode over the command line. Furthermore, it supports installing multiple Xcode versions side-by-side. It can even install all kinds of simulators and works great with automation tools like Ansible.

Are you more of a GUI person? You might find XcodesApp helpful.

xcode-install uses fastlane under the covers. It downloads everything from the Apple Developer Portal. Consequently, you need a free ADP login to use xcode-install.

Authenticating to the Apple Developer Portal

Unfortunately, automatically logging into the Apple Developer Portal is a bit of a pain since Apple introduced 2FA. If you still have an old account without 2FA enabled, you can get away with a few environment variables:

[email protected] ~ % export XCODE_INSTALL_USER=yourappleid
[email protected] ~ % export XCODE_INSTALL_PASSWORD=yourpassword
[email protected] ~ % export SPACESHIP_SKIP_2FA_UPGRADE=1

If you do not want that fastlane stores your credentials, set FASTLANE_DONT_STORE_PASSWORD=1, too.

If your account has 2FA enabled, you have to create a login cookie first:

[email protected] ~ % fastlane spaceauth -u yourappleid
[email protected] ~ % export XCODE_INSTALL_USER=yourappleid
e[email protected] ~ % export XCODE_INSTALL_PASSWORD=yourpassword

The login cookie expires and needs to be renewed from time to time.

If you cannot create the login cookie on the remote machine, you have still options. fastlane spaceauth prints the login cookie, including the necessary export command to the terminal. This allows you to “transfer” the cookie to the remote shell. If this is not possible, either, you can take advantage of fastlane storing the cookie in ~/.fastlane/spaceship. Copy ~/.fastlane/spaceship with all its contents to the remote machine, and you should have the login cookie available.

A Short Tour of xcversion

Before installing Xcode, ensure that you have at least 30 GB of free disk space available.

Installing Xcode is a matter of running

[email protected] ~ % xcversion install 13.2.1

and some patience. Unpacking and installing Xcode takes roughly half an hour. To see all available Xcode versions, run

[email protected] ~ % xcversion list
4.3 for Lion
(...)
13.2
13.2.1 (installed)

Installing simulators is easy, too. xcversion simulators gives you a list of all available simulators including their installation status. To install a specific simulator, for example, tvOS 15, run:

[email protected] ~ % xcversion simulators --install="tvOS 15.0"