Skip to content

Getting Started ​

In this chapter, you’ll get started with Swift and write your first Swift program.

Setting up a development environment ​

Before you can write Swift code, you need to set up a development environment. At a minimum, this consists of the Swift toolchain and an editor or IDE that supports Swift.

Our Swift Setup repository contains installation instructions for the various platforms and editors that support Swift. For this course, we recommend that you use one of the following combinations:

  • macOS with Xcode or Visual Studio Code
  • Ubuntu or Fedora Linux with Visual Studio Code
  • Windows with Visual Studio Code

Follow the instructions in the repository and verify that you have Swift 5.9 or later installed, as that’s the minimum version you’ll need for this course.

Using a command line interface ​

Throughout this chapter, you’ll use a terminal to compile and run your code. A terminal is an application that provides a command line interface (CLI) through which you can control your computer by typing commands.

Using a terminal can be daunting at first, but once you know the basic commands, it’s not too hard. Command line skills are essential for any programmer, which is why we don’t shy away from using a terminal in this course. Of course, you don’t need any prior experience with using a command line to complete this course. The instructions in this chapter will show you the commands you need.

On macOS and Linux, you use the Terminal application to access the command line. Windows has two different command lines: Command Prompt and PowerShell. Both are available as stand-alone applications, or through the new Windows Terminal.

Your first program ​

Open your editor, create a new file, and enter the following:

swift
print("Hello, world!")

Save this file as hello.swift in a directory named Projects in your home directory. Next, you’ll use a terminal to navigate to this source file, compile it, and execute the resulting binary.

Open your terminal. The terminal should start in your home directory. Verify this:

sh
pwd

If you’re not in your home directory, navigate there as follows:

sh
cd

Now navigate to the Projects directory:

sh
cd Projects

List the contents of this directory:

sh
ls

Verify that hello.swift is in this directory before continuing.

Next, invoke the Swift compiler (swiftc) to compile your source file and produce an executable binary:

sh
swiftc hello.swift -o hello

This command produces an executable binary named hello (on macOS and Linux) or hello.exe (on Windows). You run this binary as follows:

sh
./hello

As you can expect, this prints the text Hello, world! in the terminal.

Note

The leading ./ or .\ is required here because the executable is in the current directory β€” represented by a single dot β€” and not in the usual directories where the command line searches for its commands.

Compiling and executing multiple files ​

Most programs consist of multiple source files that are combined into a single executable. To illustrate how this works, use your editor to create two new files: square.swift and main.swift.

In square.swift, put the following code:

swift
func square(_ n: Int) -> Int {
  n * n
}

In main.swift, put this:

swift
for n in 1...20 {
  print(square(n))
}

Together, this code prints the squares of the numbers 1 through 20.

To compile both files into a single executable, you specify them both as arguments to swiftc:

sh
swiftc main.swift square.swift -o squares

You run this executable as before:

sh
./squares

When your program starts, it executes the code in main.swift from top to bottom. This code can use the code from square.swift because it’s part of the same executable.

Using Swift packages ​

Programs can quickly grow to tens or even hundreds of source files. When that happens, you want to rely on tools to manage your files and invoke the compiler for you.

Swift includes such a tool: the Swift Package Manager (SwiftPM). This section describes how you can use the Swift Package Manager to create a Swift package, compile it, and run your program.

To get started, create an empty directory named SquaresApp in the Projects directory, and navigate into it:

sh
mkdir SquaresApp
cd SquaresApp

Invoke the swift package init command to create an executable package:

swift package init --type executable

This command generates the following files and directories:

  • Package.swift

    A package consists of one or more components known as targets, each with its own source files. Package.swift configures your targets and how they depend on each other. A package can even depend on other packages.

    In this case, Package.swift configures a single executable target named SquaresApp that has no dependencies.

  • Sources

    This directory contains the source files for the SquaresApp target. In this case, it contains a main.swift file that prints the text Hello, world!.

    Every executable target must have a main.swift or equivalent file. This is the starting point for your program. A target that doesn’t have a starting point is a library. You can’t execute a library; you can only include it as a dependency in other targets.

  • .gitignore

    This hidden file is part of the Git version control system. Git is a tool that manages your files, tracks the changes you make, and allows multiple people to work on the same source files.

    Git is an essential tool for software developers; however, it’s beyond the scope of this course, so you can ignore this file for now.

Now that you understand the structure of your Swift package, you can start adding your files to it.

Move the main.swift and square.swift files you created in the previous section from the Projects directory into the Sources directory, overwriting the existing main.swift:

sh
mv ../main.swift ../square.swift Sources

Now compile your package with the swift build command:

swift build

This command compiles your code and generates an executable in the hidden .build directory. You can run this executable as follows:

sh
.build/debug/SquaresApp

Alternatively, you can use the swift run command to compile and execute your code with a single command:

swift run

Once you’ve created a Swift package, you can use your favorite editor or IDE to edit the source files. Our Swift Setup repository explains how to do this.

Up next ​

Now that you know how to create and run a Swift program, you’re ready to start your programming journey. In the next chapter, you’ll learn how you can use operators and functions to perform basic calculations.