Getting started with CocoaPods - Update Ruby Version and Install CocoaPods on Mac OS X 10.11 El Capitan

Category: Swift
Getting started with CocoaPods - Update Ruby Version and Install CocoaPods on Mac OS X 10.11 El Capitan

No matter what kind of app you’re creating, you’re likely to use code from other developers in the form of frameworks and libraries. CocoaPods is a dependency manager for Swift and Objective-C Cocoa projects. The latest version of CocoaPods will require a newer version of Ruby. In order to update Ruby and overcome new security measures we will need to install Homebrew, a package manager for OS X.

What is a dependency? A module of code, often referred to as a library or framework that your application’s code depends on to accomplish its end goals is the dependency. You’re likely familiar with UIKit and Foundation, both of which are examples of Apple-provided frameworks.

CocoaPods is a dependency manager for Swift and Objective-C Cocoa projects. It has over 22 thousand libraries and is used in over 1 million apps. CocoaPods can help you scale your projects elegantly.

If you try installing latest version of CocoaPods you will run into an error saying that your Ruby version is out of date.

CocoaPods is built with Ruby and it should be installable with the default Ruby available on macOS. You can use a Ruby Version manager. However, it’s recommend that you use the standard Ruby available on macOS unless you know what you're doing.

Mac OS X 10.11 El Capitan ships with Ruby 2.0.0. The latest version of CocoaPods will require a newer version of Ruby. To check which version of Ruby you currently have simply open your terminal and type ruby --version. Therefore you will need to update Ruby.

With the system update Apple has implemented new security measures that prevent package managers from being installed into the default location. To be more specific, Apple has enabled a new default security oriented featured called System Integrity Protection, often called rootless, in OS X 10.11 onward. The rootless feature is aimed at preventing Mac OS X compromise by malicious code, whether intentionally or accidentally, and essentially what SIP does is lock down specific system level locations in the file system while simultaneously preventing certain processes from attaching to system-level processes. Hence you can't modify anything in /System, /bin, /sbin, or /usr (except /usr/local).

In order to update Ruby and overcome new security measures we will need to install Homebrew, a package manager for OS X. Homebrew installs packages to their own directory and then symlinks their files into /usr/local

To install Homebrew you simply issue the following command in your terminal (you can always visit Homebrew home page):

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Now that you have Homebrew installed, you can use it to install Ruby. In order to manage your Ruby versions there are several Ruby version management tools you can use. We're going to use rbenv to install and manage our Ruby versions.

rbenv is a lightweight Ruby version management tool. As described at RubyInside.com: the established leader in the Ruby version management scene is RVM but rbenv is an interesting alternative if you want or need something significantly lighter with fewer features.

In order to install CocoaPods and start developing iOS apps we don’t need any fancy tools for Ruby, we just need Ruby’s latest version so rbenv is more than enough.

To do this, run the following commands in your Terminal:

brew install rbenv ruby-build

# Add rbenv to bash so that it loads every time you open a terminal
echo 'if which rbenv > /dev/null; then eval "$(rbenv init -)"; fi' >> ~/.bash_profile
source ~/.bash_profile # Install Ruby rbenv install 2.3.1 rbenv global 2.3.1 ruby --version

At the moment of writing this article the latest stable version is 2.3.1, but in order to make sure you are installing the latest version visit the official Go Rails docs for setting up Ruby on macOS.

So, at this point you should have Ruby updated on you Mac, and you can install CocoaPods with no issues.

Installing CocoaPods

To install CocoaPods you need to run Terminal and enter sudo gem install cocoapods.

When you hit enter you will be prompted for password. That’s because we are using sudo keyword which means Super User Do.

When you enter your password give the installation process couple of minutes. Don’t rush to close Terminal window. Finally, you will get the success message and CocoaPods will be installed on your machine. To check if everything has been installed properly you can issue a pod command. Terminal will display a list of the commands and options for CocoaPods.

NOTE: As already mentioned new macOS system implements new security measures that prevents package manager from being installed into the default location. To get around this you can additionally provide installation path sudo gem install –n /usr/local/bin cocoapods.

Conclusion

Yeah, this has been a very simple process. I hope now you can’t wait to dive into the development of amazing apps with the help of great frameworks and libraries developed for us by very kind people. I’ll post a follow up article on using CocoaPods and downloading your first library for your Cocoa project. Meanwhile if you would like to learn more about Cocoapods, gems and how to choose reliable frameworks I would recommend you watching video tutorials by Pluralsight - Managing Xcode Project Dependencies with CocoaPods. Happy coding!