Getting "Permission Denied" when running pip as root on my Mac
Asked Answered
D

6

129

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> under sudo 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.

Dorso answered 22/2, 2013 at 16:21 Comment(9)
I find that 'cd /tmp; sudo pip install foo' is an adequate workaround.Ermine
Possible duplicate of What are the risks of running 'sudo pip'?Stoughton
In essence, the current accepted answer (and update in the question) suggests to run "sudo pip" -- something that pip's maintainers (myself being one of them) are actively telling people not to do since that can result in breaking your operating system on MacOS and many (all?) major Linux distributions. I landed here while using terms that someone debugging their situation might and just wanted to make this redirect people to a location with better advice. Didn't think about actually bringing this question and it's answer better in line with the above statement. (out of characters)Stoughton
@Dorso would you be willing to remove that advice from the question and possibly replacing it with either better advice (using --user or a virtualenv)?Stoughton
Looking at this case closer, it might just have been a permissions issue with the said file (maybe because of running sudo pip earlier)?Stoughton
@Stoughton Quite possibly, but it was so long ago I don't really know. I like your point though, I've considered this like historical record rather than changing the accepted answer. However it's better to update the accepted answer for people coming to this now and so I'll swap the accepted answer to that suggesting a virtual env as that's best practice.Dorso
I assume this question is about general python packages. Not virtualenv, and virtualenvwrapper. Their installation documentations note sudo could be used. The logic behind this should be that, these two are more likely to be shared between python applications/environments.Nabonidus
@Nabonidus I asked this question before I knew about virtualenv, way back in 2013. At that time I was installing packages to system python and I think that was a lot to do with the permissions issues.Dorso
@Dorso I just wanted to clarify because there are answers suggesting sudo'ing virtualenv or virtualenvwrapper is wrong.Nabonidus
G
110

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.

Georgenegeorges answered 22/2, 2013 at 16:25 Comment(7)
If his permissions are messed up for his home directory, using virtualenv is not likely to help himVickery
Yes, it will, but it has already happened, so he needs to fix it before continuing.Vickery
Thanks guys, I've read about virtualenv before so hopefully these two solutions together will get me back on track :)Dorso
also, for installing virtualenv you need to sudo... or is there a workaround?Pendulum
I 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 like pgcli. Obviously I don't need a virtual env for it, I want to install it globally. Should I use sudo pip install or there are some more correct practices? THAT is the question.Parry
@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.Exterior
Using a virtualenv is not always the [best] solution. This should not be tagged as "the" answer but rather "an alternative"Clingy
E
49

Is it acceptable & safe to run pip install under sudo?

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.

Exterior answered 8/5, 2016 at 21:22 Comment(2)
Although your solution was the first one that actually worked, @throws_exceptions_at_you created a response with actual code and not a redirection to documentationSoundless
I did sudo pip install not know damages of using it. How can I undo this command or blocking to run under sudo?Fayum
C
28

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>
Cenac answered 10/7, 2016 at 23:19 Comment(6)
-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.Exterior
I 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.Cenac
+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?Perl
adding --user helps me!Phyletic
Changing 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.).Rendezvous
I could only use pip with sudo to install packages, and changing the ownership of the global site-packages helped me, though I know is quite a brutal action. ;)Boyfriend
U
10

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.

Unbraid answered 14/10, 2013 at 16:40 Comment(4)
Any chance you can explain why this solves the problem for you?Buffer
you are still using sudo pip with this "solution" and thus installing packages with root privileges, which is probably not what you want?Buffer
I 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.Unbraid
He doesn't have write access to '/Users/markwalker/Library/Logs/pip.log'Cenac
V
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.

Vickery answered 22/2, 2013 at 16:25 Comment(2)
Although this may solve the permissions problem, it does not answer the question.Georgenegeorges
Solving problems I didn't know I had is a bonus! chown is giving Operation 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.Dorso
P
5

I had a problem installing virtualenvwrapper after successfully installing virtualenv.

My terminal complained after I did this:

pip install virtualenvwrapper

So, I unsuccessfully tried this (NOT RECOMMENDED):

sudo pip install virtualenvwrapper

Then, I successfully installed it with this:

pip install --user virtualenvwrapper
Perl answered 26/2, 2017 at 2:5 Comment(3)
The --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.Exterior
you 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.Nabonidus
To 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.Perl

© 2022 - 2024 — McMap. All rights reserved.