How to instal Cocoapods on a Mac with ZSH shell

I used to install Cocoapods by following the officially recommended way sudo gem install cocoapods. Not anymore. Sudo can be a bad practice.

Joshua Moody in the Calabash-ios repository warns:

Using sudo is inherently dangerous. Executing a gem install with sudo is even more dangerous because gems can execute code at install time. This means they could erase parts of your hard drive or publish sensitive information to the internet. Installing gems with sudo on MacOS can overwrite the pre-installed system gems which could cause internal MacOS and Xcode tools to fail.

If it does not convince you, see what others have to say:

Jesse Squires in his post “zsh could not find CocoaPods” writes that another poor suggestion is specifying the bin dir to /usr/local/bin when installing the gem. This is not typically where you would want gems installed.

Cocoapods sudo-less installation on a Mac with zsh

To install the Cocoapods to our user directory we need to run the command:

$ gem install cocoapods --user-install

zsh: command not found

In the terminal, if we type pod --version the shell will probably respond with error zsh: command not found.

We need to set the PATH to the Cocoapods gem.

Setting the path to cocoapods in the .zprofile

The official guide recommends configuring .bash_profile. On the newest mac_os bash shell has been replaced with zsh shell, so we need to modify .zprofile file. The file resides in our home directory.

export GEM_HOME=$HOME/.gem
export PATH=$GEM_HOME/bin:$PATH

However, this might not work too, as the path is missing the ruby version.

First, let’s find out in the terminal the full path to the cocoapods gem:

$ gem which cocoapods

Now back to the .zprofile to update the path:

export GEM_HOME=$HOME/.gem
export PATH=$GEM_HOME/ruby/2.6.0/bin:$PATH

Testing the installation

Let us the shell know about our changes. If we have the terminal still reload the profile file with the command source ~/.zprofile

Now the command pod --version should work. We have installed Cocoapods without sudo and successfully added its path. That’s it.

Comment Rules: The goal is to be better at our craft. If you want to post code, insert it between the tags <code></code> Critical is fine, but if you’re rude, I'll delete your stuff. Please do not put your URL in the comment text and please use your PERSONAL name or initials and not your business name, as the latter comes off like spam. Have fun and thanks for adding to the converstaions!

Leave a Reply

Your email address will not be published. Required fields are marked *