pod install says 'You cannot run CocoaPods as root'
[!] You cannot run CocoaPods as root.Short answer
CocoaPods has refused to run as root since version 0.32.0, because root-owned files can leave it in a broken state. Run pod without sudo. If an earlier sudo run left root-owned caches or a Pods folder, delete them and install again. On a CI container where root is unavoidable, pass --allow-root.
Fix it fast
1Find out which cause you have
Check who the shell runs as and who owns the CocoaPods cache and the Pods folder.
whoami ls -ld ~/Library/Caches/CocoaPods Pods2Match what you see, then jump to its fix
- 1
[!] You cannot run CocoaPods as root.You ran pod with sudoFix - 2
Permission denied errors for files in the cache, the specs repo or Pods/ after you stopped using sudo.An earlier sudo run left root-owned filesFix - 3
The error appears in a pipeline where there is no other user.A CI job or Docker container runs everything as rootFix
- 1
What does the error look like?
[!] You cannot run CocoaPods as root.
Cause 1 of 3
You ran pod with sudo
1Check
Look at the command you typed. A sudo in front of pod install triggers the refusal.
whoami2Fix
Run the same command as your normal user, without sudo.
pod install3Verify
pod install finishes and creates the workspace.
ls -d *.xcworkspaceConfirmed by guides.cocoapods.org
Cause 2 of 3
An earlier sudo run left root-owned files
1Check
If files are owned by root, your normal user cannot rewrite them.
ls -ld ~/Library/Caches/CocoaPods ~/.cocoapods/repos/master Pods2Fix
The CocoaPods troubleshooting guide says to delete the root-owned cache and repo, then the Pods folder if errors remain, and install again as your user.
sudo rm -fr ~/Library/Caches/CocoaPods/
sudo rm -fr ~/.cocoapods/repos/master/
sudo rm -fr Pods/
pod install3Verify
Files created by the new run belong to your user.
ls -ld PodsConfirmed by guides.cocoapods.org
Cause 3 of 3
A CI job or Docker container runs everything as root
1Check
In a container the default user is often root. Confirm with whoami in the job log.
whoami2Fix
Either run the job as a normal user, or pass the --allow-root flag added in CocoaPods 1.10.0.
pod install --allow-root3Verify
The job passes the install step.
pod --versionConfirmed by github.com
How do I stop it coming back?
- Never put sudo in front of pod. Only the gem install step may need elevated rights, and a Ruby version manager removes that need.
- In CI, create an unprivileged user in the image and run the build as that user.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Related fixes
Mobile apps
Running pod on a React Native project fails with can't find gem cocoapods
can't find gem cocoapods (>= 0.a) with executable pod
The pod command exists, but the Ruby that is running it has no CocoaPods gem.
3 causes, each with a fixMobile apps
Running flutter prints 'Error: Unable to find git in your PATH'
Error: The Flutter directory is not a clone of the GitHub project.
The flutter launcher script runs git before anything else.
3 causes, each with a fixMobile apps
adb cannot start because something already holds its port
adb start-server failed
The adb server must bind local TCP port 5037, and every adb client talks to it there.
2 causes, each with a fixMobile apps
adb cannot connect to its daemon or start the server
* daemon started successfully *
The adb server is a background process on your computer that listens on TCP port 5037.
2 causes, each with a fixMobile apps
adb prints 'server version doesn't match this client; killing...'
adb server version doesn't match this client; killing...
adb runs a background server, and every adb command is a client.
3 causes, each with a fixMobile apps
adb tcpip 5555 fails because no device is found
adb: more than one device/emulator
adb tcpip only works while the device is attached and listed by adb, because it tells the phone to start listening on port 5555.
4 causes, each with a fix