Dependency Management in iOS with Carthage

Share this article

Dependency Management in iOS with Carthage

Carthage was the capital city of the ancient Carthaginian civilization, situated on the eastern side of the Lake of Tunis in what is now Tunisia. The city developed from a Phoenician colony into the center of an empire dominating the Mediterranean during the first millennium BC.

As interesting a topic as this might be, this article will not cover ancient history, but a dependency management for iOS.

Carthage helps you install dependencies for your project and dependencies of those dependencies. If you install package A, that package may depend on package B, which may depend on package C. If you install package D it may depend on package C, but on a different version. Finding the appropriate version is a job that package managers handle for you.

Packages in iOS are ‘frameworks’, and there are two types, dynamic and static. Carthage only supports dynamic frameworks which arrived in iOS 8.


You can install Carthage on OS X with Homebrew or an installer package.

With Homebrew:

brew update
brew install carthage

Installing Carthage with Homebrew

If you don’t have Homebrew, download the installer from Carthage’s release page.

Setting up a New Project

Setting up Carthage with your project is simple. Start a new iOS project and create a file named Cartfile in the root directory of the project. Open that file and add:

github "Alamofire/Alamofire" ~> 3.4

In this example I have used Alamofire as a dependency to my project, with github as the source of the package. Carthage supports github and other git based sources. For a package hosted on another git repo, prefix the sentence with git instead of github. By the way, Carthage was created by a group of developers from Github.

Installing dependencies using Carthage

To install or update the packages run carthage update and wait for Carthage to download and build the packages.

Cartfile location and libraries build directory

In the Carthage/Build folder, Carthage builds a framework for every ‘deploy’ target it supports and Alamofire.framework is the file you need for the project.

Link libraries to the project

Link the package with the project by using the Linked Frameworks and Libraries pane in the project settings, click the plus button, and select Add Other to add Alamofire.

You are now ready to use Alamofire in your project, and follow the sma eprocess for any other package that supports Carthage.

Carthage vs Cocoa Pods

It’s likely that you have used CocoaPods for one of your XCode based projects, and it works well. Why does XCode need another dependency manager?

There’s no harm in multiple software projects doing the same thing. Options can act as a fail safe if one packaging manager fails. CocoaPods has had it’s own problems with github in the past.

Carthage is a decentralized dependency manager and CocoaPods is centralized. With CocoaPods you can search for packages on their site, but you cant with Carthage, where Github will serve as the main way of finding packages. This is a disadvantage to some, but this way, Carthage can’t remove packages itself.

Carthage is written in Swift and CocoaPods in Ruby, making it a more ‘native’ project.

Empirical Choice?

In this article, I introduced you to Carthage and showed how to install it, how to use it, and the main differences with CocoaPods. If you want to know more about Carthage, I recommend you read their docs.

In a future article I will cover how to make your own packages ‘Carthage-compatible’, but in the meantime I would love to hear your experiences using it in the comments below.

Frequently Asked Questions (FAQs) about Dependency Management in iOS with Carthage

What is the main difference between Carthage and CocoaPods?

Carthage and CocoaPods are both dependency managers for iOS, but they operate differently. Carthage is a decentralized dependency manager. It builds your dependencies and provides you with binary frameworks, but you need to integrate them yourself. On the other hand, CocoaPods is centralized. It not only downloads and builds your dependencies but also integrates them into your project workspace. This means CocoaPods is more automatic and easier to use, but Carthage gives you more control and flexibility.

How do I install Carthage on my Mac?

To install Carthage on your Mac, you need to use Homebrew, a package manager for macOS. Open Terminal and type the following command: brew install carthage. If you don’t have Homebrew installed, you can install it first by typing /bin/bash -c "$(curl -fsSL" in Terminal.

How do I add a framework to my project using Carthage?

After you’ve installed Carthage and specified your dependencies in a Cartfile, you can add a framework to your project by following these steps: 1) Open Terminal and navigate to your project directory. 2) Run carthage update. This will fetch and build the dependencies. 3) Go to your Xcode project’s “General” settings. Under “Linked Frameworks and Libraries”, click the “+” button. 4) Add the framework you want. It should be located in the Carthage/Build folder in your project directory.

Can I use both Carthage and CocoaPods in the same project?

Yes, you can use both Carthage and CocoaPods in the same project. However, it’s generally recommended to stick with one dependency manager for simplicity and consistency. If you choose to use both, make sure to carefully manage your dependencies to avoid conflicts.

How do I update the dependencies managed by Carthage?

To update the dependencies managed by Carthage, open Terminal, navigate to your project directory, and run carthage update. This will fetch the latest versions of your dependencies and rebuild them. If you want to update a specific dependency, you can specify it like this: carthage update DependencyName.

What should I do if Carthage fails to build a dependency?

If Carthage fails to build a dependency, the first thing you should do is check the error message. It will often give you clues about what went wrong. Common issues include incompatible Swift versions, missing dependencies, and network errors. If you can’t resolve the issue yourself, consider reaching out to the maintainers of the dependency or asking for help on a platform like Stack Overflow.

How can I specify the version of a dependency in Carthage?

You can specify the version of a dependency in Carthage by adding it to your Cartfile with the version number. For example, to use version 1.0.0 of a dependency, you would write github "username/dependency" "1.0.0" in your Cartfile. You can also specify a minimum version or a range of versions.

Can I use Carthage for projects that aren’t written in Swift?

Yes, you can use Carthage for projects that aren’t written in Swift. Carthage supports any project that can be built with Xcode, including those written in Objective-C.

How do I remove a dependency managed by Carthage?

To remove a dependency managed by Carthage, you need to remove it from your Cartfile and then run carthage update. You should also remove any references to the dependency in your Xcode project.

What are the advantages of using Carthage over other dependency managers?

The main advantage of using Carthage over other dependency managers is its simplicity and flexibility. Carthage doesn’t make any changes to your project structure or build settings, giving you full control over your project. It also builds all dependencies as binary frameworks, which can lead to faster build times. However, this also means that you need to manually integrate the frameworks into your project, which can be a disadvantage if you prefer a more automatic approach.

Aleksander KokoAleksander Koko
View Author

Aleksander is young developer who loves to play with the newest web technologies. In his free time, he reads about PHP, Firefox OS or experiments with a new language. Currently, his main interests are PHP design patterns, laravel, dart and cloud.

chriswdependenciesdependency managementpackagespluginsswift
Share this article
Read Next
Get the freshest news and resources for developers, designers and digital creators in your inbox each week