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.

einpoklum
  • 118,144
  • 57
  • 340
  • 684
markwalker_
  • 12,078
  • 7
  • 62
  • 99
  • 13
    I find that '`cd /tmp; sudo pip install foo`' is an adequate workaround. – Brian Cain Feb 22 '13 at 16:28
  • 1
    Possible duplicate of [What are the risks of running 'sudo pip'?](https://stackoverflow.com/questions/21055859/what-are-the-risks-of-running-sudo-pip) – pradyunsg May 29 '18 at 08:17
  • 3
    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) – pradyunsg May 29 '18 at 18:35
  • @markwalker_ would you be willing to remove that advice from the question and possibly replacing it with either better advice (using --user or a virtualenv)? – pradyunsg May 29 '18 at 18:36
  • Looking at this case closer, it might just have been a permissions issue with the said file (maybe because of running sudo pip earlier)? – pradyunsg May 29 '18 at 18:38
  • @pradyunsg 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. – markwalker_ May 29 '18 at 23:53
  • 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. – mehmet Dec 04 '18 at 15:13
  • @mehmet 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. – markwalker_ Dec 04 '18 at 16:45
  • @markwalker_ I just wanted to clarify because there are answers suggesting sudo'ing virtualenv or virtualenvwrapper is wrong. – mehmet Dec 04 '18 at 16:51

6 Answers6

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.

Burhan Khalid
  • 169,990
  • 18
  • 245
  • 284
  • 2
    If his permissions are messed up for his **home directory**, using virtualenv is not likely to help him – hd1 Feb 22 '13 at 16:30
  • 1
    Yes, it will, but it has already happened, so he needs to fix it before continuing. – hd1 Feb 22 '13 at 16:33
  • 1
    Thanks guys, I've read about virtualenv before so hopefully these two solutions together will get me back on track :) – markwalker_ Feb 22 '13 at 16:40
  • 1
    also, for installing virtualenv you need to sudo... or is there a workaround? – jimijazz Oct 18 '16 at 00:25
  • 24
    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. – Alex Belyaev Oct 31 '17 at 19:04
  • @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. – Piotr Dobrogost Apr 11 '18 at 09:04
  • Using a `virtualenv` is not always the [best] solution. This should not be tagged as "the" answer but rather "an alternative" – WestCoastProjects Mar 12 '20 at 20:37
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.

Community
  • 1
  • 1
Piotr Dobrogost
  • 41,292
  • 40
  • 236
  • 366
  • 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 documentation – Edenshaw Feb 10 '20 at 20:27
  • 1
    I did `sudo pip install` not know damages of using it. How can I undo this command or blocking to run under `sudo`? – Emre Değirmenci Apr 06 '20 at 12:08
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>
David Brabant
  • 41,623
  • 16
  • 83
  • 111
  • 9
    -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. – Piotr Dobrogost Dec 21 '16 at 10:41
  • 1
    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. – throws_exceptions_at_you Dec 23 '16 at 08:00
  • 2
    +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? – Let Me Tink About It Feb 25 '17 at 23:34
  • adding `--user` helps me! – Honghao Z Sep 06 '17 at 14:20
  • 3
    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.). – charlesreid1 Oct 10 '17 at 21:21
  • 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. ;) – loved.by.Jesus Jun 20 '18 at 18:41
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.

Community
  • 1
  • 1
Edgar
  • 293
  • 2
  • 11
  • 2
    Any chance you can explain why this solves the problem for you? – Chris May 28 '15 at 09:11
  • 8
    you are still using `sudo pip` with this "solution" and thus installing packages with root privileges, which is probably not what you want? – Chris May 28 '15 at 09:14
  • 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. – Edgar Jul 03 '15 at 13:36
  • He doesn't have write access to '/Users/markwalker/Library/Logs/pip.log' – throws_exceptions_at_you Feb 16 '18 at 15:00
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.

hd1
  • 33,938
  • 5
  • 80
  • 91
  • Although this may solve the permissions problem, it does not answer the question. – Burhan Khalid Feb 22 '13 at 16:32
  • 2
    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. – markwalker_ Feb 22 '13 at 16:40
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
Let Me Tink About It
  • 15,156
  • 21
  • 98
  • 207
  • 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. – Piotr Dobrogost Aug 08 '17 at 08:29
  • you say NOT recommended but [official installation notes](https://virtualenvwrapper.readthedocs.io/en/latest/install.html) 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. – mehmet Dec 04 '18 at 15:07
  • 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. – Let Me Tink About It Dec 05 '18 at 16:03