Skip to content
DeveloperMemos

Upgrading Swift Package Manager (SPM) Packages in Xcode

Swift, Xcode, Swift Package Manager, Package Management2 min read

Swift Package Manager (SPM) simplifies package management for Swift projects by allowing you to easily add, update, and remove dependencies. In this article, we'll explore the steps to upgrade your SPM packages within Xcode.

Adding a Package Dependency

Adding a package to your Xcode project is the first step toward leveraging external libraries or tools. Here's how you can do it:

  1. Open Your Xcode Project

    • Launch Xcode and open your existing project or create a new one.
  2. Navigate to Package Dependencies

    • Go to File > Add Packages....
    • In the search field, enter the repository URL of the package you want to add. For example, if you're interested in the powerful Swift Algorithms package, use this URL: Swift Algorithms Repository.
  3. Select the Desired Version

    • Choose the appropriate version for your dependency rule.
    • Click the "Add Package" button to integrate it into your project.

Alternatively, you can add a package dependency directly from your target's General pane:

  1. Click on the target where you want to add the package.
  2. Scroll down to the "Frameworks, Libraries, and Embedded Content" section.
  3. Click the "+" button, select "Add Other...", and choose "Add Package Dependency".

Updating a Package Dependency

Keeping your packages up-to-date ensures that you benefit from bug fixes, performance improvements, and new features. Here's how to update individual packages or all of them at once:

  1. Individual Package Update

    • Find the package you want to update in the Project Navigator (usually located at the bottom under "Package Dependencies").
    • Right-click on the package and choose "Update Package".
    • Alternatively you can also upgrade a package by selecting your project's Target and then clicking "Package Dependencies". From here you can double dclick and raise the minimum version for the package you want to upgrade and it will automatically update it aslong as the constraints are valid.
  2. Updating All Packages

    • Right-click on the "Package Dependencies" section header.
    • Select "Update to Latest Package Versions".
      • Alternatively, you can achieve the same by going to File > Packages > Update to Latest Package Versions.

Removing a Package Dependency

If you no longer need a package, follow these steps to remove it:

  1. Locate the Package

    • Click on the project root file in the Project Navigator.
    • Select the "Package Dependencies" pane.
  2. Delete the Package

    • Locate the Swift package you want to remove from the list.
    • Click the minus (-) button to delete the selected package.

Conclusion

By mastering SPM package management in Xcode, you'll streamline your development process and ensure that your project stays up-to-date with the latest libraries. Remember to regularly check for updates and explore new packages to enhance your Swift projects!