11

I am trying to run a python3 install like this:

pip3 install django

I get the error:

PermissionError: [Errno 13] Permission denied: '/usr/local/lib/python3.6/site-packages/pytz'

I then try:

sudo pip3 install django

I get the error:

sudo: pip3: command not found

Here is what my $PATH looks like:

/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/ec2-user/.local/bin:/home/ec2-user/bin

How can I get sudo access to pip3?

0
9

Sudo is not using the same PATH as your current user for security reasons. In /etc/sudoers there is an option secure_path which specifies the path used when running commands with sudo. Mine looks like this:

Defaults        secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin"

From the sudo manpage:

 secure_path   Path used for every command run from sudo.  If you don't trust the people running sudo to have a sane PATH environment
               variable you may want to use this.  Another use is if you want to have the “root path” be separate from the “user path”.
               Users in the group specified by the exempt_group option are not affected by secure_path.  This option is not set by
               default.

The pip3 command is probably not in the path defined by the sudoers file. You can either add the path to the secure_path or provide the full path to the pip3 command like this:

sudo /path/to/pip3 install django
1

It's possible that sudo is not preserving your $PATH variable upon use. Try using the full path to the pip3 binary in the command, for example: sudo /path/to/pip3 install django. If you're unsure of the full path, do which pip3.

3
  • which pip3 outputs: /usr/local/bin/pip3 . The $PATH variable looks good as well: /usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/ec2-user/.local/bin:/home/ec2-user/bin – petey Jan 2 '18 at 20:44
  • does sudo echo $PATH produce anything different? Did you try the command with the full path to pip3? Any difference in results? – Safado Jan 2 '18 at 20:48
  • sudo outputs the same: /usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/ec2-user/.local/bin:/home/ec2-user/bin – petey Jan 2 '18 at 20:49
0

You can always do:

sudo env "PATH=$PATH" pip3 install django

As it mentioned in this answer.

0

I issued the django install this way:

pip3 install --user django

This fixed the problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.