sudo gem install not recommended
I used to install Cocoapods by following the officially recommended way
sudo gem install cocoapods
However, not anymore. Sudo
might 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:
- https://github.com/wmorgan/killergem
- https://twitter.com/timcharper/status/26202857990
- https://robots.thoughtbot.com/psa-do-not-use-system-ruby
- http://stackoverflow.com/questions/2119064/sudo-gem-install-or-gem-install-and-gem-locations
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 source ~/.zprofile
Now the command pod --version
should work. We have installed cocoapods without sudo
and successfully added its path. That’s it.