How to Use Swift Package Manager to Write a Command Line Program

*In this article, I assume you’re familiar with basic shell commands.

If you have Xcode 8, you already have Swift Package Manager (a.k.a. SwiftPM). Try this on your command line.

swift package

You will see some messages rather than an error if you have it.

To make a new package, you need to create a new directory and initialize basic file structures.

mkdir app1
cd app2
swift package init --type executable

Now you can see new files and directories that are created by SwiftPM. It should look like this.

total 8
-rw-r--r-- 1 Eonil staff 75 Oct 1 12:23 Package.swift
drwxr-xr-x 3 Eonil staff 102 Oct 1 12:23 Sources/
drwxr-xr-x 2 Eonil staff 68 Oct 1 12:23 Tests/
drwxr-xr-x 2 Eonil staff 68 Oct 1 12:23 app1/

Let’s build it.

swift build

Notice that the command is not `swift package build`. You have to remove “package” word from the command.

And run it.

.build/debug/app1

Now you will see classic “Hello World!” message. For now, I couldn’t find a command to execute the program using `swift` command.


For now, A swift package seem to be required to be a Git repository. SwiftPM also creates `.gitignore` file for you. So don’t forget to make your package into a Git repo.

git init
git add . -A
git commit -a -m "Ready."

This doesn’t matter when you work on single package executable, but as soon as you build a library, you will have trouble without git repo.