Carthage is a dependency manager for iOS and macOS projects that allows developers to integrate third-party libraries into their applications in a simple and efficient way. Unlike other tools such as CocoaPods, Carthage focuses on being minimalist and non-invasive, which means it does not change the project’s structure and only requires a simple text file, a Cartfile, listing the dependencies. This has made it a popular choice among developers who want to keep control of their project setup while benefiting from third-party libraries and frameworks.
Carthage works by building binary frameworks, which can then be integrated directly into Xcode projects. This approach means developers can benefit from faster build times and a more streamlined development process. With Carthage, dependencies can easily be added, updated and managed without complex configuration files or unwanted changes to the project. In return, the built frameworks are added to Xcode manually. This makes Carthage a good tool for those who want a simple but powerful dependency management solution in their iOS and macOS development projects.
Carthage is a decentralised dependency manager for iOS and macOS projects. It fetches and builds third-party libraries as frameworks, which the developer then adds to their Xcode project themselves, without the project file being changed automatically. This gives full control over the project's structure and can provide faster build times, because the dependencies are only built when they are updated. Carthage is mainly used as a simpler alternative to CocoaPods, while Swift Package Manager is Apple's built-in solution for package management.
More about Carthage
Carthage is a dependency manager for iOS and macOS that offers a simple and flexible way of integrating third-party libraries into projects. Unlike other tools that change the project setup automatically, Carthage focuses on minimal setup and gives developers the freedom to manage their projects without unnecessary restrictions.
Technically, Carthage works by building binary frameworks from source code, or downloading prebuilt ones, which allows developers to include these frameworks directly in their Xcode projects. This means Carthage does not change the project files but instead generates a “Carthage/Build” folder in which the compiled frameworks are stored. This approach gives developers full control over their project structure and dependencies.
Carthage supports three primary functions, each offering a different way of handling dependencies:
Building dependencies: Carthage can automatically build all the dependencies specified in a “Cartfile”. This allows developers to focus on their own code while Carthage handles the compilation of external libraries. This function is ideal for projects that require several third-party libraries.
Updating dependencies: With a single command, Carthage can update all dependencies to the newest versions that satisfy the version requirements in the Cartfile. This makes it easy to keep libraries up to date, including security fixes, which is important for maintaining compatibility and security.
Integrating frameworks: Carthage generates frameworks that the developer adds to the Xcode project themselves, typically under “Frameworks, Libraries, and Embedded Content”. This requires a manual step but gives full insight into exactly which libraries are part of the project and makes it easy to benefit from new features and improvements in third-party libraries.
One of the greatest strengths of Carthage is its simplicity and flexibility. By avoiding automated project changes, Carthage allows developers to keep control of their projects. This reduces the risk of conflicts in the project file and makes it easier to integrate new dependencies without disrupting existing project structures.
Carthage’s approach to dependency management also saves time in day-to-day work. By only building dependencies when they are added or updated, and avoiding unnecessary project changes, Carthage minimises the time and resources required to maintain a project. This makes it a good tool for developers who want to optimise their workflows and focus on delivering high-quality software.
In practice, Carthage has had a significant impact on how developers manage dependencies in iOS and macOS projects. It has been used by both small and large development teams to keep their projects up to date, secure and easy to maintain. Today, however, development of Carthage has slowed considerably, and most libraries now target Swift Package Manager first.
In summary, Carthage is a tool that combines simplicity, flexibility and efficiency in a solution that makes dependency management more transparent for both individual developers and large teams. Its simple setup and minimal intervention in the project structure have made it a useful tool in app development, where fast and secure integrations are necessary.
Frequently asked questions about Carthage
What is Carthage, and how does it work in practice?
Carthage is a decentralised dependency manager for iOS, macOS, tvOS and watchOS projects written in Swift and Objective-C. There is no central package registry. Instead, each dependency points directly to a Git repository, typically on GitHub.
The dependencies are specified in a text file named Cartfile in the project’s root folder. When the command carthage update is run, Carthage finds suitable versions, fetches the source code into the Carthage/Checkouts folder and builds each library with Xcode’s own build tools. The finished frameworks are placed in Carthage/Build.
Carthage does not change the Xcode project file. The developer adds the built frameworks under Frameworks, Libraries, and Embedded Content in the relevant target. This gives full control over the project setup but requires a little more manual work than tools that integrate automatically.
How is Carthage installed, and how is a library added?
Carthage is most often installed with Homebrew using the command brew install carthage. Alternatively, an installer package can be downloaded from the project’s releases on GitHub.
A library is typically added in four steps:
- The file
Cartfileis created in the project’s root folder with a line such asgithub "Alamofire/Alamofire" ~> 5.0. - The command
carthage update --use-xcframeworks --platform iOSfetches and builds the library. - The finished
.xcframeworkfromCarthage/Buildis dragged into Frameworks, Libraries, and Embedded Content in the app’s target. - For dynamic frameworks, Embed & Sign is selected.
The ~> operator means that newer versions are accepted as long as the major version stays the same. The library can then be used in code with import Alamofire.
What is the difference between Cartfile and Cartfile.resolved?
Cartfile describes which dependencies the project needs and which version requirements they must meet, e.g. ~> 5.0, >= 2.1 or a specific branch. The file is edited manually and expresses intent.
Cartfile.resolved is generated automatically and contains the exact versions or commits that were actually chosen, including for the dependencies’ own dependencies. The file acts as a lock, so everyone on a team and every build server gets exactly the same code.
The difference shows in the commands:
carthage updatefinds the newest versions that matchCartfileand writes the result toCartfile.resolved.carthage bootstrapfetches and builds exactly the versions listed inCartfile.resolved.
Both files should therefore be kept in version control. Dependencies used only during development, such as test libraries, can be placed in Cartfile.private.
How does Carthage build XCFrameworks, and why does it matter?
Previously, Carthage built so-called fat frameworks, in which the code for physical devices and the simulator was combined into one file with the lipo tool. This stopped working with Xcode 12, because both devices and simulators on Apple Silicon use the arm64 architecture, and the same architecture cannot appear twice in one fat framework.
The solution is XCFrameworks, which Apple introduced with Xcode 11. An XCFramework is a bundle with a separate binary for each platform and variant, such as iOS device and iOS simulator. From Carthage 0.37, XCFrameworks are built with the --use-xcframeworks flag, e.g. carthage bootstrap --use-xcframeworks.
The result is placed as .xcframework folders in Carthage/Build. They are added directly to the app’s target, and the former build phase with carthage copy-frameworks is no longer needed.
What is the difference between Carthage, CocoaPods and Swift Package Manager?
The three tools solve the same task, but in different ways:
- Carthage is decentralised and fetches dependencies directly from the Git repositories specified in a
Cartfile. The libraries are built as binary frameworks, and the Xcode project is not changed. Integration is done manually. - CocoaPods uses a central library registry and a
Podfile. Thepod installcommand creates a workspace and changes the project setup, so integration happens automatically but is less transparent. CocoaPods is now in maintenance mode. - Swift Package Manager (SPM) is Apple’s own tool and is built into Xcode. Packages are added directly in Xcode or described in a
Package.swift, and no extra installation is required.
Carthage is useful when full control over the project file and shorter build times are wanted, because the dependencies are only built when updated. For new projects, SPM is now the most widely used choice, as most libraries support it.
How can build times with Carthage be kept down?
Because Carthage builds dependencies from source code by default, the first build can take a long time. Several options can reduce the time:
--platform iOSlimits the build to the platforms the project actually uses.--cache-buildsskips dependencies that have already been built in the same version, so only changed libraries are rebuilt.- Individual dependencies can be updated separately, e.g. with
carthage update Alamofire, instead of updating all of them. - Many libraries provide prebuilt frameworks in their GitHub releases, which Carthage downloads automatically. Closed-source SDKs can be specified with
binaryand a URL to a JSON specification.
On build servers, the Carthage/Build folder can also be cached between runs. Many teams keep the folder out of Git via .gitignore and recreate it with carthage bootstrap.
Carthage for dummies
Carthage is a tool that helps organise and manage the small building blocks that apps are made of, called “dependencies”. Imagine building an app that needs some ready-made parts that others have already created to make it work better. Carthage helps fetch and keep track of these parts, so they do not have to be made from scratch. It works a bit like a librarian who keeps track of all the books needed for a project, so the writer can focus on their own story. It makes developing apps easier and faster, because other people’s work can be reused while making sure everything fits together.