Installing Swift
In this page:
Installing on macOS
On macOS, installing Xcode from the App Store automatically installs the Swift compiler and standard library. Alternatively, running xcode-select --install installs just the Command Line Tools, which include swift and swiftc without the full Xcode IDE.
Example: Installing on macOS
# Install Xcode from App Store, or install command-line tools only:
xcode-select --install
⚠️ Run this command in your terminal.
Installing on Linux
On Linux, Swift is installed by downloading a toolchain tarball from swift.org or by using a package manager for supported distributions, then extracting it and adding its bin directory to your PATH. Docker images such as swift:5.9 are a fast way to get a working environment without a manual install.
Example: Installing on Linux
# Ubuntu — install via Swift.org toolchain
wget https://download.swift.org/swift-5.10-release/ubuntu2204/swift-5.10-RELEASE/swift-5.10-RELEASE-ubuntu22.04.tar.gz
tar xzf swift-5.10-RELEASE-ubuntu22.04.tar.gz
export PATH=$PWD/swift-5.10-RELEASE-ubuntu22.04/usr/bin:$PATH
⚠️ Run this command in your terminal.
Verifying the Installation
After installing, running swift --version in a terminal prints the installed Swift compiler version. If the command is not found, the toolchain's bin folder still needs to be added to your shell's PATH.
Example: Verifying the Installation
swift --version
⚠️ Run this command in your terminal.
- Trying to write Swift code before verifying the installation with
swift --version, then debugging phantom errors caused by a missing toolchain. - Assuming Swift is only installable via Xcode on macOS and not realizing official Linux toolchains and Docker images exist.
- Forgetting to add the Swift toolchain to the system PATH after a manual Linux install, so the
swiftcommand is not found.
- On macOS, Swift comes bundled with Xcode or the standalone Command Line Tools.
- On Linux, official Swift toolchains are distributed by swift.org, or via Docker images like
swift:5.9. - Running
swift --versionconfirms the installation and shows the compiler version. - Once installed, the
swiftcommand can compile and run.swiftfiles directly from the terminal.
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: