Post

Essential Swift Package Manager Commands

Core Swift Package Manager commands for building, testing, cleaning, and managing dependencies.

Essential Swift Package Manager Commands

Swift Package Manager provides essential commands beyond basic building for managing Swift packages efficiently.

Build Commands

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# Build debug configuration
swift build

# Output
> Build complete! (5.23s)

# Build optimized release
swift build -c release

# Show binary location
swift build --show-bin-path

# Output
> /Users/user/project/.build/debug

Run Commands

1
2
3
4
5
6
7
8
9
10
# Build and run executable
swift run

# Run specific target
swift run MyTargetName

# Output
> Building for debugging...
> Build complete! (1.45s)
> [Program output]

Testing Commands

1
2
3
4
5
6
7
8
9
10
11
12
# Run all tests
swift test

# Output
> Test Suite 'All tests' passed at 2026-01-07 02:45:00.000.
> Executed 12 tests, with 0 failures (0 unexpected) in 2.341 (2.345) seconds

# Run specific test case
swift test -s TestModule.TestCase

# Run specific test method
swift test -s TestModule.TestCase/test1

Clean Commands

1
2
3
4
5
# Delete .build directory
swift package clean

# Remove .build and Packages
swift package clean --clean=dist

Package Management

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# Create new executable package
swift package init --type executable

# Create new library package
swift package init --type library

# Update dependencies
swift package update

# Resolve dependencies
swift package resolve

# Show dependency tree
swift package show-dependencies

# Output
> MyPackage
> └── Alamofire<https://github.com/Alamofire/Alamofire.git@5.8.1>

Useful Options

1
2
3
4
5
6
# Change working directory
swift build --chdir /path/to/project
swift build -C /path/to/project

# Custom build directory
swift build --build-path /custom/path

Common Workflows

Fresh build after dependency changes:

1
swift package clean && swift build

Release build with binary location:

1
swift build -c release --show-bin-path

Run specific target with clean build:

1
swift package clean && swift run TargetName

When to Use Each Command

  • swift build: Daily development builds
  • swift build -c release: Production binaries, performance testing
  • swift package clean: Resolve build cache issues
  • swift package clean --clean=dist: Fix dependency resolution problems
  • swift test: Pre-commit validation
  • swift package show-dependencies: Audit dependency tree

Important Notes

  • swift build --clean removed in Swift 4, use swift package clean
  • --show-bin-path useful for CI/CD scripts
  • -c release enables optimizations (-O flag)
  • swift run combines build and execution

☕ Support My Work

If you found this post helpful and want to support more content like this, you can buy me a coffee!

Your support helps me continue creating useful articles and tips for fellow developers. Thank you! 🙏

This post is licensed under CC BY 4.0 by the author.