How to Add a Swift Package Manager Dependency in Xcode (Step by Step)

Adding a third-party library to your iOS app with Swift Package Manager takes only a few clicks in Xcode. This step-by-step guide walks through adding, importing, and pinning a dependency the right way.

Before You Start

Adding a Swift package is one of the most common tasks in iOS development, and thankfully it is one of the easiest once you know the flow.

You will need a Mac with a current version of Xcode installed. Xcode is free from the Mac App Store and includes Swift Package Manager, so there is nothing extra to download.

You will also need an existing app project, or you can create a fresh one with File then New then Project.

Finally, have the package's Git URL ready. Most Swift packages live on GitHub, and the URL is simply the repository address ending in .git or the plain repo URL.

It also helps to skim the package's README first, since it usually states the minimum Swift or Xcode version it needs and the Apple platforms it supports.

Confirming that up front avoids the frustration of adding a dependency your project's deployment target cannot actually build against.

Throughout this guide, remember that SPM handles the library, but building and shipping the app itself still relies on Xcode and, for release, the Apple Developer Program.

Step 1: Open the Add Package Dialog

Open your project in Xcode and select the project file at the top of the Project Navigator, the blue icon representing your app.

From the top menu bar, choose File, then Add Package Dependencies. In some Xcode versions the wording is slightly different, but it always lives under the File menu.

Alternatively, select your project in the navigator, open the Package Dependencies tab, and click the plus button beneath the list.

Either route opens the same package search and configuration sheet.

This dialog is where you will paste the package URL, choose a version rule, and pick which targets consume the library. Take a moment to get familiar with its layout before proceeding, because the same sheet is reused every time you add a dependency.

Step 2: Enter the Package URL

In the search field at the top right of the dialog, paste the package's Git repository URL. For a GitHub package this is usually the repository web address.

Xcode will contact the repository and display the package, along with its available versions and branches.

If the repository is private, Xcode will prompt you to authenticate. You can connect your GitHub or source-control account in Xcode's Settings under Accounts so that private packages resolve without repeated prompts.

Wait for Xcode to finish fetching the package metadata. This can take a few seconds depending on repository size and your connection.

Once the package appears with its details on the right side of the sheet, you are ready to choose how strictly to pin its version.

Step 3: Choose a Version Rule

SPM uses semantic versioning, and choosing a sensible version rule protects you from surprise breaking changes.

The safest common choice is 'Up to Next Major Version.' This accepts bug-fix and feature updates while blocking major-version jumps that may break your code.

You can also pin to an exact version for maximum stability, or track a specific branch such as main when you deliberately want the latest unreleased code. Branch tracking is riskier and generally reserved for development or forks.

For most production apps, 'Up to Next Major Version' starting from the latest stable release is the right default.

Make your selection from the dropdown, confirm the starting version number, and then click the Add Package button to continue.

Step 4: Assign the Product to a Target

After resolving, Xcode shows the products the package provides, typically one or more libraries. You must attach each product you need to a target in your app.

In the 'Add to Target' column, select your app target for the library product you want to use. If your project has multiple targets, such as an app and an app extension, choose each that needs the dependency.

Getting this step wrong is the most common cause of the dreaded 'No such module' error, so double-check that the product is attached to the target where you will import it.

Click Add Package to finish.

Xcode will download and integrate the package, and you will see it appear under Package Dependencies in the Project Navigator.

Step 5: Import and Use the Library

With the package added, you can now use it in code. Open the Swift file where you need the library and add an import statement at the top, using the module name the package exposes.

For example, if the package's library product is named NetworkKit, you write import NetworkKit near your other imports.

Build the project with Command plus B. If the module is found and the build succeeds, the dependency is wired up correctly.

If you see 'No such module,' revisit Step 4 to confirm the product is attached to the current target, then clean the build folder with Shift plus Command plus K and build again.

Once it compiles, start calling the library's APIs as documented by its authors.

Step 6: Commit Package.resolved

When you add a dependency, SPM writes a Package.resolved file that records the exact versions it chose. This file is essential for reproducible builds.

Commit Package.resolved to your version control system alongside your project. Doing so guarantees that teammates and your continuous integration server build against the identical versions you tested.

For an app project, Package.resolved typically lives inside the .xcodeproj or .xcworkspace bundle, in the swiftpm directory. Your source-control client should pick it up automatically.

Do not add resolved dependency source code to your repository; SPM re-downloads it on demand based on the manifest and the resolved file.

With the file committed, your dependency setup is complete and reproducible across every machine that clones the project.

Updating and Removing Packages Later

Dependencies are not set-and-forget. To update a package, choose File then Packages then Update to Latest Package Versions, which advances each dependency within the version rules you set.

To change a single package's version rule, select the project, open the Package Dependencies tab, double-click the package, and adjust its rule.

To remove a dependency, select it in that same Package Dependencies list and click the minus button. Then delete any now-unused import statements from your code.

After any change, build and run your test suite so you catch behavior differences early.

Keep in mind that updating dependencies is a code change like any other; review release notes, especially before accepting a major-version bump, and commit the updated Package.resolved so the rest of your team stays in sync.

Frequently Asked Questions

Where is the Add Package option in Xcode?

It is under the File menu, usually labeled Add Package Dependencies. You can also select your project, open the Package Dependencies tab, and click the plus button.

Why do I get 'No such module' after adding a package?

Most often the package product is not attached to the target you are importing it into. Reopen the package configuration and assign the library product to the correct target, then clean and rebuild.

Should I commit Package.resolved to Git?

Yes. Committing Package.resolved ensures teammates and CI build against the exact resolved versions, which keeps builds reproducible.

What version rule should I choose?

For most apps, 'Up to Next Major Version' from the latest stable release is a good default. Pin to an exact version when you need maximum stability.

Can I add a private Swift package?

Yes. Add your source-control account in Xcode Settings under Accounts, then paste the private repository URL. Xcode will authenticate and resolve it.