Getting "Permission Denied" when running pip as root on my Mac
Solution 1
Use a virtual environment:
$ virtualenv myenv
.. some output ..
$ source myenv/bin/activate
(myenv) $ pip install what-i-want
You only use sudo
or elevated permissions when you want to install stuff for the global, system-wide Python installation.
It is best to use a virtual environment which isolates packages for you. That way you can play around without polluting the global python install.
As a bonus, virtualenv does not need elevated permissions.
Solution 2
Is it acceptable & safe to run
pip install
undersudo
?
It's not safe and it's being frowned upon – see What are the risks of running 'sudo pip'?
To install Python package in your home directory you don't need root privileges. See description of --user
option to pip.
Solution 3
Your original problem is that pip cannot write the logs to the folder.
IOError: [Errno 13] Permission denied: '/Users/markwalker/Library/Logs/pip.log'
You need to cd into a folder in which the process invoked can write like /tmp
so a cd /tmp
and re invoking the command will probably work but is not what you want.
BUT actually for this particular case (you not wanting to use sudo
for installing python packages) and no need for global package installs you can use the --user
flag like this :
pip install --user <packagename>
and it will work just fine.
I assume you have a one user python python installation and do not want to bother with reading about virtualenv (which is not very userfriendly) or pipenv.
As some people in the comments section have pointed out the next approach is not a very good idea unless you do not know what to do and got stuck:
Another approach for global packages like in your case you want to do something like :
chown -R $USER /Library/Python/2.7/site-packages/
or more generally
chown -R $USER <path to your global pip packages>
Solution 4
Because I had the same problem, I want to stress that actually the first comment by Brian Cain is the solution to the "IOError: [Errno 13]"-problem:
If executed in the temp directory (cd /tmp
), the IOError does not occur anymore if I run sudo pip install foo
.
Solution 5
It looks like your permissions are messed up. Type chown -R markwalker ~
in the Terminal and try pip
again? Let me know if you're sorted.
markwalker_
I'm a Python (Django) developer, maintainer of a few packages and tech lead of django-cms. #SOreadytohelp django-bleach django-sql-explorer django-sys-indicator
Updated on November 05, 2021Comments
-
markwalker_ over 2 years
I've started to use my Mac to install Python packages in the same way I do with my Windows PC at work; however on my Mac I've come across frequent permission denied errors while writing to log files or site-packages.
Therefore I thought about running
pip install <package>
undersudo
but is that a safe/acceptable use of sudo considering I'm just wanting this to be installed under my current user account?Example traceback from a logfile I/O error:
Command /usr/bin/python -c "import setuptools;__file__='/Users/markwalker/build/pycrypto/setup.py';exec(compile(open(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --single-version-externally-managed --record /var/folders/tq/hy1fz_4j27v6rstzzw4vymnr0000gp/T/pip-k6f2FU-record/install-record.txt failed with error code 1 in /Users/markwalker/build/pycrypto Storing complete log in /Users/markwalker/Library/Logs/pip.log Traceback (most recent call last): File "/usr/local/bin/pip", line 8, in <module> load_entry_point('pip==1.1', 'console_scripts', 'pip')() File "/Library/Python/2.7/site-packages/pip-1.1-py2.7.egg/pip/__init__.py", line 116, in main return command.main(args[1:], options) File "/Library/Python/2.7/site-packages/pip-1.1-py2.7.egg/pip/basecommand.py", line 141, in main log_fp = open_logfile(log_fn, 'w') File "/Library/Python/2.7/site-packages/pip-1.1-py2.7.egg/pip/basecommand.py", line 168, in open_logfile log_fp = open(filename, mode) IOError: [Errno 13] Permission denied: '/Users/markwalker/Library/Logs/pip.log'
Update This was likely down to permissions, however the best approach is to use virtual environments for your python projects. Running
sudo pip
should be avoided unless absolutely necessary. -
hd1 about 11 yearsIf his permissions are messed up for his home directory, using virtualenv is not likely to help him
-
Burhan Khalid about 11 yearsAlthough this may solve the permissions problem, it does not answer the question.
-
hd1 about 11 yearsYes, it will, but it has already happened, so he needs to fix it before continuing.
-
markwalker_ about 11 yearsSolving problems I didn't know I had is a bonus!
chown
is givingOperation not permitted
on a lot of hidden dirs like.shsh
& I assume it's working through files it can set now, but I'll see what happens when the cli prompt returns. -
markwalker_ about 11 yearsThanks guys, I've read about virtualenv before so hopefully these two solutions together will get me back on track :)
-
Chris almost 9 yearsAny chance you can explain why this solves the problem for you?
-
Chris almost 9 yearsyou are still using
sudo pip
with this "solution" and thus installing packages with root privileges, which is probably not what you want? -
Edgar almost 9 yearsI can only guess why this works: I think that some part of (some) pip installation scripts require write access to current directory, but with a different user. Therefore, if executed while in your home directory, it mysteriously fails because of the lack of write access. If called from within
/tmp
it works, because everyone has write access there. -
jimijazz over 7 yearsalso, for installing virtualenv you need to sudo... or is there a workaround?
-
Piotr Dobrogost over 7 years-1 Changing ownership of global site-packages folder is a terrible thing to do. The
--user
option for pip was given as a solution in my answer which had already existed when you wrote yours. -
throws_exceptions_at_you over 7 yearsI don't see an argument here. Also given the fact that someone who asks such an entry level question is probably not familiar with unix's permission system and therefore running a 1-user install it doesn't matter. Also your answer actually fails to address the use case of me actually WANTING to install to global packages. After doing that I could easily revert the permissions back to pre-install.
-
Let Me Tink About It about 7 years+1 for actually writing the entire commands. Some people assume the OP knows how to implement an option at the command line when they, or other readers, might not. Don't you agree, @PiotrDobrogost?
-
Piotr Dobrogost almost 7 yearsThe --user option for pip was given as a solution in my answer which had already existed when you wrote yours. This should have been a comment not an answer.
-
Honghao Zhang over 6 yearsadding
--user
helps me! -
charlesreid1 over 6 yearsChanging the permissions for the system Python's entire
site-packages
directory is akin to "fixing" a pump by whacking on it with a wrench. It's protected for a reason - you're not supposed to install stuff there. The real solution is not to mix the system Python distribution with day-to-day programming. Install a different Python distribution (from Python.org, Homebrew, Canopy, etc.). -
Alex Belyaev over 6 yearsI don't understand why this is the best answer. The question is NOT about virtual environments. It's about validity of using
sudo pip install
. Let's say I need to install some package that I'll use in many projects or at system level. Such as some CLI tool likepgcli
. Obviously I don't need a virtual env for it, I want to install it globally. Should I usesudo pip install
or there are some more correct practices? THAT is the question. -
throws_exceptions_at_you over 6 yearsHe doesn't have write access to '/Users/markwalker/Library/Logs/pip.log'
-
Piotr Dobrogost about 6 years@AlexBelyaev The title does not correspond exactly to the body of the question, where OP asks is that a safe/acceptable use of sudo considering I'm just wanting this to be installed under my current user account? So yes, there are basically two different questions asked here and yes, answers which suggest using virtualenv answer one of these two questions.
-
loved.by.Jesus almost 6 yearsI could only use
pip
withsudo
to install packages, and changing the ownership of the globalsite-packages
helped me, though I know is quite a brutal action. ;) -
mehmet over 5 yearsyou say NOT recommended but official installation notes say OK to install virtualenvwrapper with sudo. Same goes for virtualenv. The question asked here make no reference to those two, so I assume all other answers here are for general python packages.
-
Let Me Tink About It over 5 yearsTo future readers, I struck through my "not recommended" flag in my answer due to the above comment but I have not yet verified it. That's why I did not delete the flag yet.
-
Edenshaw over 4 yearsAlthough your solution was the first one that actually worked, @throws_exceptions_at_you created a response with actual code and not a redirection to documentation
-
WestCoastProjects about 4 yearsUsing a
virtualenv
is not always the [best] solution. This should not be tagged as "the" answer but rather "an alternative" -
Emre Değirmenci about 4 yearsI did
sudo pip install
not know damages of using it. How can I undo this command or blocking to run undersudo
?