Install Swift
The first step to using Swift is to download and install the compiler and other required components. Go to the Download page and follow the instructions for your target platform.
In order to follow along with the examples below, make sure to add Swift to your $PATH
.
On MacOS
The default location for the downloadable toolchain on macOS is /Library/Developer/Toolchains
. You can make the latest installed toolchain available for use from the terminal with the following command:
$ export TOOLCHAINS=swift
To select any other installed toolchain, use its identifier in the TOOLCHAINS
variable. The identifier can be found in toolchain’s Info.plist
file.
$ /usr/libexec/PlistBuddy -c "Print CFBundleIdentifier:" /Library/Developer/Toolchains/swift-4.0-RELEASE.xctoolchain/Info.plist
org.swift.4020170919
$ export TOOLCHAINS=org.swift.4020170919
On Linux
First, install clang:
$ sudo apt-get install clang
If you installed the Swift toolchain on Linux to a directory other than the system root, you will need to run the following command, using the actual path of your Swift installation:
$ export PATH=/path/to/Swift/usr/bin:"${PATH}"
You can verify that you are running the expected version of Swift by entering the swift command and passing the --version
flag:
$ swift --version
Apple Swift version 2.2-dev (LLVM ..., Clang ..., Swift ...)
The -dev
suffix on the version number is used to indicate that it’s a development build, not a released version.