I am trying to setup Django.
When I run pip install -r requirements.txt
, I get the following exception:
Installing collected packages: amqp, anyjson, arrow, beautifulsoup4, billiard, boto, braintree, celery, cffi, cryptography, Django, django-bower, django-braces, django-celery, django-crispy-forms, django-debug-toolbar, django-disqus, django-embed-video, django-filter, django-merchant, django-pagination, django-payments, django-storages, django-vote, django-wysiwyg-redactor, easy-thumbnails, enum34, gnureadline, idna, ipaddress, ipython, kombu, mock, names, ndg-httpsclient, Pillow, pyasn1, pycparser, pycrypto, PyJWT, pyOpenSSL, python-dateutil, pytz, requests, six, sqlparse, stripe, suds-jurko
Cleaning up...
Exception:
Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/pip/basecommand.py", line 122, in main
status = self.run(options, args)
File "/usr/lib/python2.7/dist-packages/pip/commands/install.py", line 283, in run
requirement_set.install(install_options, global_options, root=options.root_path)
File "/usr/lib/python2.7/dist-packages/pip/req.py", line 1436, in install
requirement.install(install_options, global_options, *args, **kwargs)
File "/usr/lib/python2.7/dist-packages/pip/req.py", line 672, in install
self.move_wheel_files(self.source_dir, root=root)
File "/usr/lib/python2.7/dist-packages/pip/req.py", line 902, in move_wheel_files
pycompile=self.pycompile,
File "/usr/lib/python2.7/dist-packages/pip/wheel.py", line 206, in move_wheel_files
clobber(source, lib_dir, True)
File "/usr/lib/python2.7/dist-packages/pip/wheel.py", line 193, in clobber
os.makedirs(destsubdir)
File "/usr/lib/python2.7/os.py", line 157, in makedirs
mkdir(name, mode)
OSError: [Errno 13] Permission denied: '/usr/local/lib/python2.7/dist-packages/amqp-1.4.6.dist-info'
What's wrong and how do I fix this?
Option a) Create a virtualenv, activate it and install:
Option b) Install in your homedir:
My recommendation use safe (a) option, so that requirements of this project do not interfere with other projects requirements.
You are trying to install a package on the system-wide path without having the permission to do so.
In general, you can use
sudo
to temporarily obtain superuser permissions at your responsibility in order to install the package on the system-wide path:Find more about
sudo
here.If you don't want to make system-wide changes, you can install the package on your per-user path using the
--user
flag.All it takes is:
Finally, for even finer grained control, you can also use a virtualenv, which might be the superior solution for a development environment, especially if you are working on multiple projects and want to keep track of each one's dependencies.
After activating your virtualenv with
$ my-virtualenv/bin/activate
the following command will install the package inside the virtualenv (and not on the system-wide path):
pip install -r requirements.txt
We should really stop advising the use of
sudo
withpip install
. It's better to first trypip install --user
. If this fails then take a look at the top post here.The reason you shouldn't use
sudo
is as follows:When you run pip with
sudo
, you are running arbitrary Python code from the Internet as a root user, which is quite a big security risk. If someone puts up a malicious project on PyPI and you install it, you give an attacker root access to your machine.Perform
chmod -0777 -R
on the virtual environment and runpip install -r requirements.txt
Just clarifying what worked for me after much pain in linux (ubuntu based) on permission denied errors, and leveraging from Bert's answer above, I now use ...
or if running pip on a requirements file ...
and these work reliably for every pip install including creating virtual environments.
However, the cleanest solution in my further experience has been to install
python-virtualenv
andvirtualenvwrapper
withsudo apt-get install
at the system level.Then, inside virtual environments, use
pip install
without the--user
flag AND withoutsudo
. Much cleaner, safer, and easier overall.User doesn't have write permission for some Python installation paths. You can give the permission by:
So you should give permission, then try to install it again, if you have new paths you should also give permission: