← Back to Swift Course | Chapter 14: Standard Library & Best Practices | Lesson 5 of 7

Swift Package Manager

Swift Package Manager is a tool that organizes your project's code and downloads any outside libraries it needs to work.
Syntax
swift
swift package init --type executable
swift build
swift run
swift test

Initializing a New Package

swift package init creates a new package skeleton, including a Package.swift manifest and a starter source file.

Example: Initializing a New Package

bash
swift package init --type executable

⚠️ Run this in your own terminal or Node.js environment.

Building and Running a Package

swift build compiles all of a package's targets, and swift run builds (if needed) and then executes its main executable target.

Example: Building and Running a Package

bash
swift build
swift run

⚠️ Run this in your own terminal or Node.js environment.

Running Tests

swift test compiles and runs all test targets declared in the package's manifest, reporting pass/fail results.

Example: Running Tests

bash
swift test

⚠️ Run this in your own terminal or Node.js environment.

Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Trying to run swift build or swift run in a folder without a Package.swift manifest file; SPM requires this file to know how to build the project.
  2. Confusing a package's products (what other packages can import) with its targets (the actual source modules being compiled).
  3. Forgetting swift test looks specifically for a target ending in Tests by convention, matching the package's testing setup.
Chapter Summary
  • swift package init scaffolds a new package, generating a Package.swift manifest.
  • Package.swift declares the package's name, dependencies, targets, and products.
  • swift build compiles the package, and swift run builds and executes an executable target.
  • swift test runs the package's test targets.
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.