Swift Package Manager (Package Management)
Swift Package Manager (SwiftPM or SPM for short) is Swift's official dependency management and build tool.
It handles three things: describing what code a package consists of, fetching dependencies from remote sources, and compiling the code into a library or executable.
It's integrated into the Swift toolchain and Xcode, needs no separate installation, and is the preferred way to manage dependencies in Swift projects today.
Core concepts of SwiftPM
Before you start, let's map a few terms to actual directories and files.
| Concept | Corresponding entity | Description |
|---|---|---|
| Package | A directory containing Package.swift | The basic unit of distribution and dependency management |
| Manifest | Package.swift | Describes the package structure using Swift code |
| Target | A subdirectory under Sources | A compilation module, the unit of code organization |
| Product | An entry in the products array | What the package offers externally, a library or executable |
| Dependency | An entry in the dependencies array | Other packages used by this package |
The most confusing pair is "target" and "product": a target is how code is organized internally, while a product is what's available externally.
A target may not appear in any product (pure internal implementation), and a product can consist of multiple targets.
Creating a package
SwiftPM comes with a scaffolding command that directly generates the standard directory structure.
$ swift package init --type executable --name RunoobCLI
Creating executable package: RunoobCLI
Creating Package.swift
Creating .gitignore
Creating Sources
Creating Sources/RunoobCLI/RunoobCLI.swift
Creating Tests/
Creating Tests/RunoobCLITests/
Creating Tests/RunoobCLITests/RunoobCLITests.swift
The generated structure is very fixed: source files go under Sources, tests under Tests.
RunoobCLI/
├── Package.swift
├── Sources/
│ └── RunoobCLI/
│ └── RunoobCLI.swift
└── Tests/
└── RunoobCLITests/
└── RunoobCLITests.swift
--type supports several templates; the most common are library, executable, and empty, corresponding to a library, an executable, and an empty package.
There are also templates like tool (an executable with command-line argument parsing), macro (a macro package), and build-tool-plugin.
When no name is given, SwiftPM uses the current directory name as the package name.
Structure of Package.swift
The manifest file is itself a Swift source file, using the types provided by PackageDescription to describe the package.
The first line, swift-tools-version, is required; it determines which APIs the manifest can use.
Example
// File path: Package.swift
// swift-tools-version: 6.0
import PackageDescription
let package
=
Package
(
// Package name, also the project name shown in Xcode
name
:
"RunoobKit"
,
// Declare the minimum supported platform version
platforms
:
[
.
macOS
(
.
v14
)
]
,
products
:
[
// Library product: for other packages or apps to depend on
.
library
(
name
:
"RunoobCore"
, targets
:
[
"RunoobCore"
]
)
,
// Executable product: the target run by swift run
.
executable
(
name
:
"RunoobCLI"
, targets
:
[
"RunoobCLI"
]
)
,
]
,
targets
:
[
// Library target: contains only reusable code, no main entry point
.
target
(
name
:
"RunoobCore"
)
,
// Executable target: must have a main entry point
.
executableTarget
(
name
:
"RunoobCLI"
,
dependencies
:
[
"RunoobCore"
]
)
,
// Test target: depends on the module under test
.
testTarget
(
name
:
"RunoobCoreTests"
,
dependencies
:
[
"RunoobCore"
]
)
,
]
,
// Use Swift 6 language mode
swiftLanguageModes
:
[
.
v6
]
)
The commonly used parameters are explained below.
| Parameters | Required | Purpose |
|---|---|---|
| name | Required | Package name |
| platforms | Optional | Declare the minimum supported OS version |
| products | Optional | The library or executable offered externally |
| dependencies | Optional | Other packages this package depends on |
| targets | Required | All targets within the package |
| swiftLanguageModes | Optional | Specify the Swift language mode to use |
Note:When platforms is not declared, SwiftPM compiles against the minimum OS version supported by the toolchain. If your code uses newer APIs (such as the Synchronization module), you must declare platforms explicitly, or you'll get an "API is unavailable on older OS versions" error.
Library targets and executable targets
A library target is declared with .target; it holds reusable code and cannot have top-level executable statements.
An executable target is declared with .executableTarget and must provide a main entry point, either via @main or by naming the file main.swift.
Dependencies between targets are declared via the dependencies parameter, using the target name.
Example
// File path: Sources/RunoobCore/RunoobCore.swift
import Foundation
/// Site information model, for reuse by other modules
public
struct
RunoobSite
:
Sendable
{
public
let name
:
String
public
let url
:
String
public
init
(
name
:
String, url
:
String
)
{
self.
name
=
name
self.
url
=
url
}
/// Returns a description in the form "name (address)"
public
func describe
(
)
String
{
"(name) ((url))"
}
}
Example
// File path: Sources/RunoobCLI/RunoobCLI.swift
import Foundation
import RunoobCore
@main
struct
RunoobCLI
{
static
func main
(
)
{
let site
=
RunoobSite
(
name
:
"RUNOOB"
, url
:
"www.runoob.com"
)
(
site.
describe
(
)
)
}
}
Once the executable target imports the library target, it can use RunoobSite directly.
Note that types and members in the library target are all marked public; otherwise they're invisible across modules—this is the most common source of errors in SwiftPM multi-target projects.
Dependency management and version constraints
In the dependencies array, declare dependencies with .package; there are several ways to write version constraints.
| Syntax | Meaning | Applicable scenarios |
|---|---|---|
| .package(url: "…", from: "1.3.0") | From 1.3.0 up to but not including the next major version | Most common, equivalent to 1.3.0..<2.0.0 |
| .package(url: "…", exact: "1.3.0") | Lock to a single version | Use temporarily when a dependency has compatibility issues |
| .package(url: "…", "1.3.0"..<"1.6.0") | Specify a range | When you need to avoid a problematic version |
| .package(url: "…", branch: "main") | Follow a branch | Use features not yet released |
| .package(url: "…", revision: "abc123") | Lock to a specific commit | Temporarily verify a certain commit |
| .package(path: "../RunoobKit") | Local path dependency | Co-developing multiple packages on the same machine |
Below is an example using both remote and local dependencies.
Example
// File path: Package.swift
// swift-tools-version: 6.0
import PackageDescription
let package
=
Package
(
name
:
"RunoobApp"
,
platforms
:
[
.
macOS
(
.
v14
)
]
,
dependencies
:
[
// Remote dependency: use versions 1.3.0 and above, below 2.0.0
.
package
(
url
:
"https://github.com/apple/swift-argument-parser.git"
, from
:
"1.3.0"
)
,
// Local dependency: another package on the same machine
.
package
(
path
:
"../runoob-access-pkg"
)
,
]
,
targets
:
[
.
executableTarget
(
name
:
"RunoobApp"
,
dependencies
:
[
// Reference a specific product in the remote dependency
.
product
(
name
:
"ArgumentParser"
, package
:
"swift-argument-parser"
)
,
// Reference a specific product in the local dependency
.
product
(
name
:
"RunoobKit"
, package
:
"runoob-access-pkg"
)
,
]
)
,
]
)
Dependency resolution results are written to the Package.resolved file, which records the exact version each dependency was resolved to.
This file should be committed to the repository so that everyone on the team and CI all get exactly the same dependency versions.
Common Commands
In day-to-day development, only a handful of commands are truly high-frequency.
| Command | Purpose |
|---|---|
| swift build | Build the package, using the debug configuration by default |
| swift build -c release | Build using the release configuration |
| swift run |
Build and run an executable target |
| swift test | Build and run test targets |
| swift package resolve | Resolve dependencies and update Package.resolved |
| swift package update | Update dependencies to the latest versions allowed by the version constraints |
| swift package describe | Print the package's name, products, and target structure |
| swift package clean | Delete .build build artifacts |
Below is the real build output of the package from earlier, with some compilation progress lines and machine-dependent timing figures omitted.
$ swift build
Building for debugging...
[5/9] Emitting module RunoobCore
[6/9] Compiling RunoobCore RunoobCore.swift
[7/11] Compiling RunoobCLI RunoobCLI.swift
[9/11] Linking RunoobCLI
Build complete!
When running an executable target, it is compiled automatically first if the code has changed.
$ swift run RunoobCLI
Build of product 'RunoobCLI' complete!
RUNOOB (www.runoob.com)
swift package describe is useful for confirming how dependencies and targets resolve, especially when a dependency is not referenced correctly.
$ swift package describe
Name: RunoobKit
Path: /Users/runoob/RunoobKit
Tools version: 6.0
Dependencies:
Platforms:
Name: macos
Version: 14.0
Products:
Name: RunoobCore
Type:
Library:
automatic
Targets:
RunoobCore
Name: RunoobCLI
Type:
Executable: nil
Targets:
RunoobCLI
Targets:
Name: RunoobCore
Type: library
Path: Sources/RunoobCore
Test targets are run with swift test, and the output lists each test case as passed or failed.
Example
// File path: Tests/RunoobCoreTests/RunoobCoreTests.swift
import Testing
@testable import RunoobCore
@Test func testDescribe
(
)
async throws
{
let site
=
RunoobSite
(
name
:
"RUNOOB"
, url
:
"www.runoob.com"
)
#expect(site.describe() == "RUNOOB (www.runoob.com)")
}
This uses Swift's official new Testing framework, which ships with Xcode 16 and newer toolchains, so XCTest is no longer needed.
Integration with Xcode
Xcode has first-class support for SwiftPM, and several ways of using it are straightforward.
Double-click Package.swift, or use File > Open to open the directory containing the package; Xcode treats it as a project and automatically lists a scheme for each executable target.
In an existing App project, use File > Add Package Dependencies to add a dependency directly by repository URL, and Xcode writes the version constraint into the project configuration for you.
Once added, Xcode caches the remote dependency locally, so builds work offline afterward.
Tip:When editing Package.swift in Xcode, if you change the target structure but the list on the left does not update, just reset once with File > Packages > Reset Package Caches.
Common questions
Why types in a library target cannot be used in another target
Check whether public was left out.
Each target is a separate module; the default internal level is only visible within the module, and across targets it must be raised to public or open.
What should swift-tools-version be set to
Just use the lowest version your toolchain supports; it determines which APIs the manifest can use.
Set it too high and older toolchains cannot open the package; set it too low and you cannot use new parameters.
Should Package.resolved be committed to the repository?
For executables and Apps, it should be committed to keep everyone's dependency versions consistent.
For widely used libraries, committing is generally recommended too, to avoid resolution results drifting across environments.
Why can't swift run find my target?
Confirm that the target is declared with .executableTarget and that the name is spelled consistently.
A library target declared only with .target cannot be run by swift run.
Can local path dependencies and remote dependencies be mixed?
Yes; both are written in the dependencies array.
Local path dependencies are often used for developing multiple packages together, then swapped for a versioned remote dependency before release.