How Did I Install CocoaPods On My MacBook
Recently, I started learning iOS app development and the Swift language. Like with other programming languages, I encountered the head-scratching task of managing dependencies in my Swift projects. CocoaPods came to my rescue by simplifying the process of integrating third-party libraries into Xcode projects. However, the first hurdle I faced with CocoaPods was figuring out how to install it. In this blog post, I'll share the different approaches I tried, hoping they will be useful to others.
The Official Installation Guide
First off, if you've ever visited the official CocoaPods website, you might be thinking, "There's already an installation guide, so why bother writing a blog post?"
Here is the CocoaPods installation guide: link. To be honest, it's not very friendly for those who are very new to not only CocoaPods but also Ruby. In my blog post, I aim to provide a more comprehensive guide.
Installation Methods
Method 1: Install via RubyGems
This is the approach recommended by the installation guide.
Step 1: Install Homebrew
If you haven't already, install Homebrew by running:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
(if this command does not work, maybe Homebrew has changed something, you can find the latest command on https://brew.sh/)
Step 2: Install rbenv
With rbenv, you will be able to install multiple versions of Ruby on your machine, and switch between them when needed. You can install rbenv and initialize it by running:
brew install rbenv
rbenv init
Step 3: Install Ruby with rbenv
The first command to run is this:
rbenv install -l
This command will return a list of the latest stable Ruby versions, an example output:
$ rbenv install -l
3.1.6
3.2.6
3.3.6
3.4.1
jruby-9.4.9.0
mruby-3.3.0
picoruby-3.0.0
truffleruby-24.1.1
truffleruby+graalvm-24.1.1
You can choose a version from this list, and run
rbenv install x.y.z
# Example:
# rbenv install 3.4.1
Then, you can set a global Ruby version for your machine:
rbenv global x.y.z
# Example:
# rbenv global 3.4.1
After that, you will need to add the following lines to ~/.zprofile
export PATH="$HOME/.rbenv/bin:$PATH"
eval "$(rbenv init - zsh)"
Save the file, and apply the changes with:
source .zprofile
At this point, you can check your Ruby version by running:
ruby -v
Step 4: Install CocoaPods with RubyGems
Now things are easy, you can run the following to install CocoaPods:
gem install cocoapods
Last step, you can check if CocoaPods is installed correctly by running:
pod --version
Method 2: Install via Homebrew
A lot of steps in Method 1, right? I thought so too, and I was so happy when I figured it out. To share this with the community, I shared it on my BlueSky, and I got a very nice feedback from Cornelius Mark:
I was not aware of this at all, and this could have saved me a lot of time.
Step 1: Install Homebrew
(same command with the step in Mehod 1)
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Step 2: Install CocoaPods with Homebrew
Run this command to install CocoaPods with Homebrew:
brew install cocoapods
Then, you can verify the installation by running:
pod --version
Compare Two Methods
Method 2 is much simpler than Method 1, does it mean we should just discard Method 1? No!
In Method 1, Ruby is installed using rbenv, and CocoaPods is installed under a certain version of Ruby. In this way, CocoaPods can be considered to be more "isolated" from the system level. When you need to work on other projects and switch to another Ruby version, you don't need to worry about anything from CocoaPods installation.
In conclusion, if you are comfortable with Ruby and need better control over Ruby versions, Method 1 is likely the better choice. However, if you prefer a simpler installation process and are already using Homebrew, Method 2 might be the more convenient option. Here is all I learned about installing CocoaPods, happy coding!