65

After trying to install virtualenv with pip

$ pip install virtualenv

I got a permission denied error

IOError: [Errno 13] Permission denied: '/Library/Python/2.7/site-packages/virtualenv.py'

So I used sudo to install virtualenv

$ sudo pip install virtualenv

But then a warning showed up:

The directory '/Users/petertao/Library/Caches/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.

The directory '/Users/petertao/Library/Caches/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.

What does sudo's -H flag do?

Peter Tao
  • 1,668
  • 6
  • 18
  • 29
  • 4
    Could have found this via searching for `sudo` and see that [sudo manual](https://www.sudo.ws/man/1.8.18/sudo.man.html) page with `-H` stating "set the HOME environment variable to the home directory specified by the target user's password database entry." – metatoaster Apr 26 '17 at 00:31
  • 9
    Thanks for getting back and I apologize if this seems like a trivial question for everybody. I am just starting to use python, pip, and the terminal. What I am still trying to understand is how setting the HOME environment variable to the home directory fixes these warnings. – Peter Tao Apr 26 '17 at 01:21
  • 13
    While this exact question is painfully trivial, the *unstated* question of "Why does `pip` complain about non-superuser permissions and ownership when running as the superuser?" is non-trivially interesting. As [user3141593](https://stackoverflow.com/users/3147394/user3141593)'s [well-authored answer](https://stackoverflow.com/a/43623102/2809027) explains, `pip` complains because creating superuser-owned subdirectories in non-superuser home directories is a bad idea. Fortunately, this question remained open long enough to receive a useful response. – Cecil Curry Aug 19 '17 at 05:58
  • 15
    What's with the pretentious attitudes? – John R Perry Dec 19 '17 at 15:08
  • 5
    @JohnRPerry: Same here. I found the man page entry entirely unhelpful and wound up here after a Google search. Thankfully, user3141593 provided an answer that gave more insight. – JS. Jul 24 '18 at 00:04

1 Answers1

101

Generally

man sudo (the exact text may vary, but it will be similar):

-H

The -H (HOME) option requests that the security policy set the HOME environment variable to the home directory of the target user (root by default) as specified by the password database. Depending on the policy, this may be the default behavior.

So why is this even an option? Normally using "sudo" does not change the $HOME environment variable.

for example:

 echo $HOME $USER
/home/testuser testuser

 sudo bash -c 'echo $HOME $USER'
/home/testuser root

 sudo -H bash -c 'echo $HOME $USER'
/home/root root

You can see that a normal sudo changes which user I am from "testuser" to "root", but not what $HOME is set to, while a sudo -H also changes the variable from "my" home directory to root's home directory.

In your Case

pip is warning you that it was executed as the user root and wanted to modify things in $HOME, which was set to '/Users/petertao', which is not owned by root (most likely the "petertao" user). the warning indicates that pip uses $HOME to cache files, but has disabled its own caching because of the folder ownership discrepancy.

Of course while executing as root pip can modify '/Users/petertao/Library/Caches/pip' because root is (almost) almighty. This can become troublesome later because a program running without root could no longer overwrite or modify these files. Instead pip refuses to write to a directory owned by another user.

SergiyKolesnikov
  • 7,369
  • 2
  • 26
  • 47
user3141593
  • 1,114
  • 1
  • 8
  • 7
  • 11
    So is there any reason not to use "sudo -H" or is the warning to conservative? Based on your thorough explanation it appears that we should always use -H but the warning from pip is "you __may__ want to use sudo -H" instead of "to enable caching while using sudo please use -H" – ndemou Jan 13 '18 at 19:05
  • 3
    Can you explain the difference between double quotes and single quotes here? I tried `sudo -H bash -c "echo $HOME $USER"` and `sudo -H bash -c 'echo $HOME $USER'` and these commands give different results: `/home/ubuntu ubuntu` and `/root root` respectively. I used to think that there is no difference between quotes. – through.a.haze Apr 18 '19 at 09:48
  • 2
    @through.a.haze https://stackoverflow.com/questions/6697753/difference-between-single-and-double-quotes-in-bash – Arne Jun 03 '19 at 13:30