Managing multiple versions of Swift locally

The most common way of installing a version of Swift is by downloading Xcode.

Some cases might require you to download a toolchain not associated with an Xcode release, such as a development snapshot for an upcoming release of Swift or a Swift toolchain for a different architecture such as WebAssembly.

This article explains how to install and use a Swift toolchain both manually and using a command line tool called swiftenv.

Installing a toolchain

On macOS, toolchains come in the form of .pkg files. Users can install these toolchains manually by downloading them from the vendor website directly and following the instructions in the package's install wizard.

The next sections show how to download, install, and setup a Swift toolchain, more specifically the trunk development snapshot for macOS provided by Apple.

Manually

Installing a toolchain manually requires just three steps:

  1. Download the toolchain from the vendor website. In this case, download the trunk development snapshot toolchain from Apple directly.

Swift's trunk development snapshot download page

  1. Click on the downloaded .pkg file.

Result of double-clicking on the toolchain's pkg file

  1. Follow the steps on the install wizard.

Result of finishing going through all steps on the install wizard

Once the wizard completes all install actions, you can find the new toolchain under ~/Library/Developer/Toolchains.

Swiftenv

Swiftenv allows you to install and manage different Swift versions from the terminal.

This article doesn't go into how to install swiftenv, but if you want to find out more about getting swiftenv setup, please refer to the official documentation.

Swiftenv can install the same trunk development snapshot toolchain from the previous section with a single command:

Terminal
swiftenv install DEVELOPMENT-SNAPSHOT-2022-11-19-a

You can find a list of available versions by running swiftenv install --list and a list of development snapshots available by running swiftenv install --list-snapshots.

You can then verify the previous command worked by checking the installed Swift versions in the system:

Terminal
swiftenv versions

Xcode

Xcode has out of the box support for switching between different Swift toolchains. The Xcode > Toolchains menu lists all installed toolchains:

Pick a toolchain from the list to start using it. Xcode shows an indicator next to the build status menu to denote the usage of a toolchain. To go back to Xcode's default Swift toolchain, tap on the icon and select the Xcode-named option from the list.

Command line

Swiftenv provides an easy way to switch between Swift versions directly from the command line. It can select a Swift toolchain either locally, which selects it just for the current directory, or globally, which selects it system-wide:

Terminal
# Just in the current directory
# Creates a .swift-version file
swiftenv local DEVELOPMENT-SNAPSHOT-2022-11-19-a

# Sets the version of Swift system-wide
# Creates a ~/.swiftenv/version file
swiftenv global DEVELOPMENT-SNAPSHOT-2022-11-19-a

Any swift commands which run after selecting a Swift version with swiftenv use the correct toolchain.

Email icon representing an email newsletter

iOS CI Newsletter

A fortnightly independent newsletter gathering all updates and news about Continuous Integration and Continuous Delivery from the iOS community.