Carthage for Swift: Complete Guide with Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

📦 Carthage for Swift: Decentralized Dependency Manager

\n\n

Carthage is a decentralized dependency manager for the Apple ecosystem (iOS, macOS, tvOS, watchOS). Unlike CocoaPods, Carthage does not modify your project automatically — it builds frameworks, and you connect them to Xcode yourself. This gives you full control over the build process and minimal "magic".

\n\n

The library uses Carthage (the name matches the tool) and works through a Cartfile — a declarative file that lists dependencies. Carthage clones repositories, builds them (using xcodebuild), and places the resulting .framework files in the Carthage/Build folder. You manually add them to your project.

\n\n

Why use Carthage? If you value transparency, want to avoid version conflicts, and dislike tools that automatically modify your project. It's ideal for teams where each member decides how to connect dependencies, and for projects with strict build security requirements.

\n\n
\n\n

1. Installing Carthage

\n\n

Carthage can be installed via Homebrew (recommended) or by downloading the .pkg from GitHub.

\n\n
\n Exact installation command (macOS):\n
\n\n
# Installation via Homebrew\nbrew install carthage\n\n# Check version\ncarthage version
\n\n

If Homebrew is not installed, install it first: /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

\n\n

Linux or Windows are not officially supported by Carthage (macOS only).

\n\n
\n\n

2. Quick Start — Minimal Working Example

\n\n

Create a new Xcode project (e.g., Single View App). Then, in the project root, create a file named Cartfile (no extension) and add a dependency:

\n\n
# Cartfile\ngithub "Alamofire/Alamofire" ~> 5.8
\n\n

Now run the command in the terminal (from the project folder):

\n\n
carthage update --platform iOS
\n\n

After completion, Alamofire.framework will appear in the Carthage/Build/iOS folder. Open Xcode, drag this .framework into the Frameworks, Libraries, and Embedded Content section of your target. Select "Embed & Sign".

\n\n

Now you can import the library:

\n\n
import Alamofire\n\n// Minimal request\nAF.request("https://api.github.com/users/defunkt").response { response in\n    debugPrint(response)\n}
\n\n

Done! You have just connected a dependency via Carthage.

\n\n
\n\n

3. Main Methods and Functions of Carthage

\n\n

Below are the key commands and capabilities of Carthage. Most of them are invoked from the terminal.

\n\n
\n

🔹 1. carthage update

\n

Signature carthage update [--platform PLATFORM] [--configuration CONFIG] [--no-build] [--use-xcframeworks]

\n

What it does: Downloads and builds all dependencies from Cartfile.resolved (or updates to the latest versions if Cartfile.resolved is absent). Creates the Carthage/Build folder.

\n
# Update only for iOS, using xcframeworks\ncarthage update --platform iOS --use-xcframeworks
\n
\n\n
\n

🔹 2. carthage bootstrap

\n

Signature carthage bootstrap [--platform PLATFORM] [--configuration CONFIG] [--use-xcframeworks]

\n

What it does: Builds dependencies using the existing Cartfile.resolved (without updating versions). Ideal for CI/CD — guarantees reproducible builds.

\n
# Rebuild everything as is                    

Recommendations