Swift Package Manager is Swift’s native tool for distributing libraries, resolving dependencies and building projects. It works from the command line and is integrated into Xcode, so the same package can support app projects, server-side Swift and command-line tools.
What a Swift package contains
Every package starts with a Package.swift manifest. The manifest defines the package name, supported platforms, products, targets and dependencies. Source files normally live under Sources, while tests live under Tests.
Create a library package
mkdir MyLibrary
cd MyLibrary
swift package init --name MyLibrary --type library
swift test
The command creates a manifest, one library target and one test target. Running swift test immediately is a useful check that the package structure and local toolchain are working.
Declare a dependency
// swift-tools-version: 6.0
import PackageDescription
let package = Package(
name: "MyLibrary",
platforms: [.iOS(.v17), .macOS(.v14)],
products: [
.library(name: "MyLibrary", targets: ["MyLibrary"])
],
dependencies: [
.package(
url: "https://github.com/apple/swift-collections.git",
from: "1.1.0"
)
],
targets: [
.target(
name: "MyLibrary",
dependencies: [
.product(name: "Collections", package: "swift-collections")
]
),
.testTarget(
name: "MyLibraryTests",
dependencies: ["MyLibrary"]
)
]
)
The dependency declaration tells Swift Package Manager where to find the package and which versions are allowed. The target dependency then selects the product that your code imports.
Choose version requirements deliberately
from:accepts the selected version and later compatible releases up to the next major version.exact:pins one version. It can help with a temporary compatibility problem but makes updates harder.- A version range gives precise control, but broad ranges can admit changes you have not tested.
- A branch or revision is useful during development. It is usually a poor choice for a stable release because it does not express semantic compatibility.
For applications, commit Package.resolved so the team and CI use the same resolved versions. For libraries, define compatible requirements in the manifest and test against the versions you claim to support.
Add a package in Xcode
In an Xcode project, use File > Add Package Dependencies, enter the repository URL, choose the dependency rule and add the required product to the correct target. Xcode resolves the dependency and records the selected version in the project workspace.
Keep package boundaries useful
A package boundary should represent a real capability with a small public API. Avoid splitting code into packages only to reproduce the folder structure of the app. Each package adds build, versioning and ownership decisions, so the boundary should make the system easier to understand or reuse.
Before publishing, run the package tests on every supported platform, document the public API and tag a semantic version. The current command and manifest options are documented in the Swift Package Manager reference.



