How to properly install Cocoapods(You don’t have write permissions for the /Library/Ruby/Gems/2.6.0 directory)
We are going to see how to properly install cocoapods, probably when coding for ios/swift or crossplatform/flutter.
Step 1:
Run the code below in your terminal.
sudo gem install cocoapods
Using sudo means you are trying to install cocoapods with the system installed ruby, and this ruby version may be an outdated version and if true would throw an error as
‘You don’t have write permissions for the /Library/Ruby/Gems/2.6.0 directory’.
Step 2:
If you run into the error in step 1 above then proceed with this step else your cocoapods is installed successfully.
If your system has Homebrew move over to step 3, else install Homebrew using the code below:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Once the installation is successful, quit and restart Terminal, then check if Homebrew is ready to go:
brew doctor
If you get Your system is ready to brew
, you can move on to step 3. Otherwise, read what Homebrew is saying very carefully. They usually provide great instructions that you should follow.
On Apple Silicon Macs, Homebrew might tell you to run a few commands after the installation, such as:
echo "eval $(/opt/homebrew/bin/brew shellenv)" >> ~/.zprofile
eval $(/opt/homebrew/bin/brew shellenv)
Make sure to run those commands.
Quit and restart terminal, then check if everything is working so far:
brew doctor
Step 3:
Install chruby and ruby-install:
brew install chruby ruby-install
Then install the latest Ruby (currently 3.2.2):
ruby-install ruby
Next, configure your shell:
echo "source $(brew --prefix)/opt/chruby/share/chruby/chruby.sh" >> ~/.zshrc
echo "source $(brew --prefix)/opt/chruby/share/chruby/auto.sh" >> ~/.zshrc
echo "chruby ruby" >> ~/.zshrc
Quit and relaunch terminal, then check that everything is working:
ruby -v
You should see something like this:
Step 4:
Set ruby version with the newly installed chruby on Mac:
source /usr/local/share/chruby/chruby.sh
source /usr/local/share/chruby/auto.sh
Then, switch from system installed ruby to your newly installed ruby using the code below:
sudo ln -s /usr/bin/ruby /usr/local/share/chruby
Check which ruby you are on, if the switch happened:
which ruby
Step 5:
Finally, lets install cocoapods using our latest ruby version:
gem install cocoapods
Everything should work fine now…Happy coding ✨