Swift Package Manager for Swift: Complete Guide with Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Swift Package Manager (SPM): Complete Guide for Swift Developers

\n\n

Swift Package Manager (SPM) is the official tool for managing dependencies, building, and distributing Swift code. Built directly into the Swift ecosystem, SPM provides a unified and standardized way to work with packages, freeing developers from the need to use third-party solutions such as CocoaPods or Carthage. SPM is tightly integrated with the Swift compiler and Xcode, making it an ideal choice for modern projects.

\n\n

Why do you need Swift Package Manager? First and foremost, for dependency management. Any non-trivial project uses third-party libraries: for networking (Alamofire, URLSession), for JSON parsing (SwiftyJSON), for working with databases (GRDB, SQLite.swift), etc. SPM automatically downloads, builds, and links these libraries with your project. Secondly, SPM allows you to create and publish your own packages, making your code reusable. Thirdly, it provides a powerful build tool, including support for tests, resources, and plugins.

\n\n

Unlike CocoaPods, SPM does not require installing a separate Ruby gem or configuring a complex Podfile. Unlike Carthage, SPM does not require manually linking frameworks. SPM is a "native" package manager that understands the structure of Swift projects out of the box. It uses the Package.swift file to describe the project and its dependencies, making the configuration process transparent and predictable.

\n\n

Installation

\n\n

Swift Package Manager comes bundled with the official Swift distribution. No separate installation is required. If you have Swift installed (version 5.0 or later), SPM is already available on your system.

\n\n

Check Installation:

\n
swift --version
\n

If you see the Swift version, SPM is ready to use. You can also check for the tool itself:

\n
swift package --version
\n\n

Installing Swift (if not installed):

\n
    \n
  • macOS: Install Xcode from the Mac App Store. Swift and SPM are included.
  • \n
  • Linux (Ubuntu, CentOS, Amazon Linux): Download and install the distribution from the official website swift.org.
  • \n
  • Windows: Download and install the distribution from swift.org.
  • \n
\n\n

Quick Start

\n\n

Let's create a new executable project using SPM. This is a minimal working example.

\n\n

1. Create the project:

\n
mkdir MySwiftProject\ncd MySwiftProject\nswift package init --type executable
\n

This command will create the project structure:

\n
    \n
  • Package.swift — the manifest file.
  • \n
  • Sources/MySwiftProject/main.swift — the entry point.
  • \n
  • Tests/ — the directory for tests.
  • \n
\n\n

2. Contents of Package.swift:

\n
// swift-tools-version:5.9\nimport PackageDescription\n\nlet package = Package(\n    name: "MySwiftProject",\n    targets: [\n        .executableTarget(\n            name: "MySwiftProject"\n        )\n    ]\n)
\n\n

3. Contents of main.swift:

\n
print("Hello, Swift Package Manager!")
\n\n

4. Build and run:

\n
swift build\nswift run
\n

You should see in the console: Hello, Swift Package Manager!

\n\n

Main Methods, Functions, and Classes

\n\n

Let's look at the key elements of SPM that you will encounter daily. These are mainly the API provided by the PackageDescription module.

Recommendations