← Back to Swift Course | Chapter 1: Setup & Basics | Lesson 2 of 7

Installing Swift

Installing Swift means downloading the toolchain onto your computer so it understands and can run Swift code.

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

bash
# 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

bash
# 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

bash
swift --version

⚠️ Run this command in your terminal.

Common Mistakes
  1. Trying to write Swift code before verifying the installation with swift --version, then debugging phantom errors caused by a missing toolchain.
  2. Assuming Swift is only installable via Xcode on macOS and not realizing official Linux toolchains and Docker images exist.
  3. Forgetting to add the Swift toolchain to the system PATH after a manual Linux install, so the swift command is not found.
Chapter Summary
  • 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 --version confirms the installation and shows the compiler version.
  • Once installed, the swift command can compile and run .swift files directly from the terminal.
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.