How to Run Swift from the Command Line on macOS


This blog post is an adaptation of the official Swift documentation.

Swift is a programming language developed by Apple for programming their suite of devices. It is a general purpose language and can even be used to develop user interfaces with the first party library SwiftUI. It is statically typed and compiled, similar to languages like C and Java, it is fast and memory safe like Rust, and it is also expressive and easy to read like Python.

This tutorial walks you through the basic steps of installing and running a simple Swift application.

Installing

Install swift, then check your installation with:

swift --version

Getting Started

The Swift Package Manager comes with Swift and will help us get our project ready. This is called “bootstrapping”. Create a directory for your project and then open a terminal inside it. You can do that with some commands like these:

mkdir MySwiftProject
cd MySwiftProject
swift package init --name MySwiftProject --type executable

This creates a directory called MySwiftProject and adds the following to it:

$ tree
.
├── Package.swift
└── Sources
    └── main.swift

1 directory, 2 files

Running Your Code

You can compile and run your project from the same terminal using this command:

swift run MySwiftProject

Adding Dependencies

Dependencies are very common in modern software projects. These let you add convenient functionality to your project which can speed up development and make your code more readable and reliable.

Let’s add the package example-package-figlet that the Swift docs use as a sample. Open Package.swift and add the dependency like this:

// swift-tools-version: 5.10
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
    name: "MySwiftProject",
    dependencies: [
        .package(url: "https://github.com/apple/example-package-figlet", branch: "main"),
    ]
    targets: [
        // Targets are the basic building blocks of a package, defining a module or a test suite.
        // Targets can depend on other targets in this package and products from dependencies.
        .executableTarget(
            name: "MySwiftProject",
            dependencies: [
                .product(name: "Figlet", package: "example-package-figlet"),
            ],
            path: "Sources"),
    ]
)

Once you do that, you can download dependencies and build your project with the command:

swift build

Creating Your App

You can delete the contents of main.swift and replace it with the following code:

import Figlet

@main
struct FigletTool {
    static func main() {
        Figlet.say("Love your code!")
    }
}

Run your app with swift run and you should see this output:

$ swift run
Building for debugging...
[7/7] Applying MySwiftProject
Build complete! (0.77s)
  _                                                                                    _          _
 | |       ___   __   __   ___     _   _    ___    _   _   _ __      ___    ___     __| |   ___  | |
 | |      / _ \  \ \ / /  / _ \   | | | |  / _ \  | | | | | '__|    / __|  / _ \   / _` |  / _ \ | |
 | |___  | (_) |  \ V /  |  __/   | |_| | | (_) | | |_| | | |      | (__  | (_) | | (_| | |  __/ |_|
 |_____|  \___/    \_/    \___|    \__, |  \___/   \__,_| |_|       \___|  \___/   \__,_|  \___| (_)
                                   |___/

Try it yourself: follow the instructions in the swift-argument-parser repo and have your app accept a custom message to print to the console!

Conclusion

Swift is easy to run from the command line and doesn’t require the massive Xcode IDE to get started. For coding CLI apps and learning syntax, this is a great place to start.

What’s Next