Swift Package Manager basics: creating a package and managing dependencies

Swift Package Manager (SwiftPM) is the build tool and dependency manager that ships with Swift. It is built into Xcode and the swift command-line tool, and it is the default way to share Swift code now that CocoaPods’ Trunk repository is going read-only (on 2 December 2026). This post creates a library package, adds a dependency, and covers updating and removing it.
Key concepts
Section titled “Key concepts”- Package: a directory with a
Package.swiftmanifest at its root, plus sources and tests. - Target: a module, a set of source files compiled together. Regular targets, executable targets, test targets, plugins and macros are all targets.
- Product: what a package exposes to others, either a library or an executable, built from one or more targets.
- Dependency: another package, declared in the manifest with a URL (or local path) and a version requirement. A target then depends on one of that package’s products.
Creating a package
Section titled “Creating a package”mkdir MyPackagecd MyPackageswift package initswift package init creates a library package by default. Use --type executable for a command-line program, or --type tool for one that uses swift-argument-parser. The result looks like this:
MyPackage├── Package.swift├── Sources│ └── MyPackage│ └── MyPackage.swift└── Tests └── MyPackageTests └── MyPackageTests.swiftRecent toolchains generate tests using Swift Testing (import Testing, @Test) rather than XCTest. The manifest looks like this (the generator adds explanatory comments, removed here):
import PackageDescription
let package = Package( name: "MyPackage", products: [ .library( name: "MyPackage", targets: ["MyPackage"] ), ], targets: [ .target( name: "MyPackage" ), .testTarget( name: "MyPackageTests", dependencies: ["MyPackage"] ), ])The first line is not a plain comment: swift-tools-version sets the minimum Swift version that can read the manifest and which PackageDescription APIs it may use. swift package init writes the version of the toolchain you ran it with.
Check that everything works:
swift buildswift testAdding a dependency
Section titled “Adding a dependency”A dependency is declared in two places: the package-level dependencies array says where to fetch the package and which versions are allowed, and each target that uses it lists one of the package’s products. Here is the manifest with Alamofire added:
import PackageDescription
let package = Package( name: "MyPackage", platforms: [.macOS(.v12), .iOS(.v15)], products: [ .library( name: "MyPackage", targets: ["MyPackage"] ), ], dependencies: [ .package(url: "https://github.com/Alamofire/Alamofire.git", from: "5.12.2"), ], targets: [ .target( name: "MyPackage", dependencies: [ .product(name: "Alamofire", package: "Alamofire"), ] ), .testTarget( name: "MyPackageTests", dependencies: ["MyPackage"] ), ])A few details:
from: "5.12.2"means “5.12.2 or newer, below 6.0.0”. Use.upToNextMinor(from:)for a tighter range,exact:to pin one version, orbranch:/revision:for unreleased code.package:in.product(name:package:)is the package’s identity, which is the last path component of the URL without.git.platformsis there because the manifest Alamofire 5.12 uses with the newest Swift tools version declares a minimum of macOS 12 and iOS 15. If your package’s minimum is lower than a dependency’s, SwiftPM refuses to build it, so set yours at least as high.
Recent toolchains can make the same edits from the command line. Run swift package add-dependency --help to check that your version has it:
swift package add-dependency https://github.com/Alamofire/Alamofire.git --from 5.12.2swift package add-target-dependency Alamofire MyPackage --package AlamofireThen swift build resolves the dependency, clones it, and writes the exact versions it chose to Package.resolved.
In an Xcode app project, which has no Package.swift, use File > Add Package Dependencies… instead and pick the target to link the product to.
Updating and removing dependencies
Section titled “Updating and removing dependencies”SwiftPM does not move to a newer version on its own once Package.resolved exists:
swift package updatere-resolves every dependency to the newest version allowed by the manifest and rewritesPackage.resolved. Pass a package name (swift package update Alamofire) to update just one.- To allow a new major version, change the requirement in
Package.swift(for example tofrom: "6.0.0"), then runswift package update. swift package resolveresolves without updating, using the pinned versions where it can.swift package show-dependenciesprints the resolved dependency tree.
To remove a dependency, delete its .package(...) entry and every .product(...) that refers to it, then run swift package update so Package.resolved drops it.
Gotchas
Section titled “Gotchas”- Commit
Package.resolvedfor apps and executables, so every machine and CI run builds the same versions. Libraries often leave it out, because consumers resolve versions themselves. - If resolution gets stuck after editing the manifest,
swift package resetclears the.builddirectory’s checkouts and state. - Version tags must be full semantic versions (
1.2.0, not1.2), or SwiftPM ignores them.
Further reading
Section titled “Further reading”© 2026 David Li · Home · Blog · RSS ·Classic site · Built with Astro, Starlight and three.js · Logos:Font Awesome Free (CC BY 4.0)