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 /Users/user_name/.gem/ruby/2.6.0/gems/cocoapods-0.29.0/lib/cocoapods.rb
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
Now the command
pod --version should work. We have installed cocoapods without
sudo and successfully added its path. That’s it.