Getting Started

Here, you’ll find information about the how to use the Swift programming language.

If you’re new to Swift, check out A Swift Tour in The Swift Programming Language, for a quick introduction to the most important concepts and features of the language.

Installing 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

  1. Install required dependencies:
Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04 CentOS 7 CentOS 8 Amazon Linux 2
$ apt-get install \
          binutils \
          git \
          libc6-dev \
          libcurl3 \
          libedit2 \
          libgcc-5-dev \
          libpython2.7 \
          libsqlite3-0 \
          libstdc++-5-dev \
          libxml2 \
          pkg-config \
          tzdata \
          zlib1g-dev
$ apt-get install \
          binutils \
          git \
          libc6-dev \
          libcurl4 \
          libedit2 \
          libgcc-5-dev \
          libpython2.7 \
          libsqlite3-0 \
          libstdc++-5-dev \
          libxml2 \
          pkg-config \
          tzdata \
          zlib1g-dev
$ apt-get install \
          binutils \
          git \
          gnupg2 \
          libc6-dev \
          libcurl4 \
          libedit2 \
          libgcc-9-dev \
          libpython2.7 \
          libsqlite3-0 \
          libstdc++-9-dev \
          libxml2 \
          libz3-dev \
          pkg-config \
          tzdata \
          zlib1g-dev
$ yum install \
      binutils \
      gcc \
      git \
      glibc-static \
      libbsd-devel \
      libedit \
      libedit-devel \
      libicu-devel \
      libstdc++-static \
      pkg-config \
      python2 \
      sqlite

      # __block conflicts with clang's __block qualifier
      sed -i -e 's/\*__block/\*__libc_block/g' /usr/include/unistd.h
$ yum install \
      binutils \
      gcc \
      git \
      glibc-static \
      libbsd-devel \
      libedit \
      libedit-devel \
      libicu-devel \
      libstdc++-static \
      pkg-config \
      python2 \
      sqlite
$ yum install \
      binutils \
      gcc \
      git \
      glibc-static \
      gzip \
      libbsd \
      libcurl \
      libedit \
      libicu \
      libsqlite \
      libstdc++-static \
      libuuid \
      libxml2 \
      tar \
      tzdata

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}"

On Windows

Visual Studio and Swift

You will need to install both the toolchain installer from the Download page and Visual Studio 2019.

The following Visual Studio components are required:

Component Visual Studio ID
MSVC v142 - VS 2019 C++ x64/x86 build tools (v14.25)1 Microsoft.VisualStudio.Component.VC.Tools.x86.x64
Windows Universal C Runtime Microsoft.VisualStudio.Component.Windows10SDK
Windows 10 SDK (10.0.17763.0)2 Microsoft.VisualStudio.Component.Windows10SDK.17763

1 You may install a newer build toolset.
2 You may install a newer SDK instead.

The following additional Visual Studio components are recommended:

Component Visual Studio ID
C++ CMake tools for Windows Microsoft.VisualStudio.Component.VC.CMake.Project
Git for Windows Microsoft.VisualStudio.Component.Git
Python 3 64-bit (3.7.8) Component.CPython.x64

The default installation location for the toolchain on Windows is %SystemDrive%\Library\Developer\Toolchains.

Support Files

Note that you must use the x64 Native Tools for VS2019 Command Prompt to run the toolchain. The x64 Native Tools for VS2019 Command Prompt runs the DevEnv script from Visual Studio that sets up the necessary environment variables to find the system headers.

In order to make the Windows SDK accessible to Swift, it is necessary to deploy a few files into the Windows SDK. The following will modify your Visual Studio Installation, and as such will require to be run from an (elevated) “Administrator” x86 Native Tools for VS2019 Command Prompt.

copy %SDKROOT%\usr\share\ucrt.modulemap "%UniversalCRTSdkDir%\Include\%UCRTVersion%\ucrt\module.modulemap"
copy %SDKROOT%\usr\share\visualc.modulemap "%VCToolsInstallDir%\include\module.modulemap"
copy %SDKROOT%\usr\share\visualc.apinotes "%VCToolsInstallDir%\include\visualc.apinotes"
copy %SDKROOT%\usr\share\winsdk.modulemap "%UniversalCRTSdkDir%\Include\%UCRTVersion%\um\module.modulemap"

Because it is installing the files into the Visual Studio image, the files will need to be copied each time Visual Studio is updated.


Swift Version

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.

Using the REPL

If you run the swift command without any other arguments, you’ll launch the REPL, an interactive shell that will read, evaluate, and print the results of any Swift code you enter.

$ swift
Welcome to Apple Swift version 2.2. Type :help for assistance.
  1>

Interacting with the REPL is a great way to experiment with Swift. For example, if you enter the expression 1 + 2, the result of the expression, 3, is printed on the next line:

  1> 1 + 2
$R0: Int = 3

You can assign values to constants and variables, and use them in subsequent lines. For instance, the String value Hello, world! can be assigned to the constant greeting, and then passed as an argument to the print(_:) function:

  2> let greeting = "Hello!"
greeting: String =