109

I attempt to deploy a Python package with pip in a virtual environment on an Ubuntu machine, but encounter a permission-related issue. For example:

(TestVirtualEnv)test@testServer:~$ pip install markdown2

terminates by:

error: could not create '/home/test/virtualenvs/TestVirtualEnv/lib/python3.3/site-packages/markdown2.py': Permission denied

I can't sudo, since it will install the package globally, and not within the virtual environment. I chowned site-packages; ls shows only directories related to easy_install, pip and setuptools, and nothing related to Markdown.

How to deploy a package in a virtual environment with pip without encountering permission-related errors?

Kai - Kazuya Ito
  • 6,454
  • 5
  • 44
  • 50
Arseni Mourzenko
  • 48,123
  • 32
  • 108
  • 189
  • 1
    Just to clarify: did you create this virtualenv with `sudo`? – sebastian_oe Oct 19 '13 at 22:49
  • @sebastian_oe: I *believe* I did. I'll recreate one to be sure. – Arseni Mourzenko Oct 19 '13 at 22:51
  • 5
    Then this might be the problem. Try creating a virtualenv without `sudo`. – sebastian_oe Oct 19 '13 at 22:51
  • 1
    @sebastian_oe: I created a virtual environment without `sudo`. Indeed, the package installs correctly now. Please, can you promote your comment to an answer so I can accept it? – Arseni Mourzenko Oct 19 '13 at 22:58
  • 1
    Also if you are switching between different users, make sure you are using user that owns (or has write access) `virtualenv` directory, because you can activate `virtualenv` with other user, but it won't let you install any packages and will give permission access. – Andrius Oct 20 '17 at 14:47
  • Another possible cause is creating the virtualenv from linux on an NTFS partition. Try recreating it on e.g. an ext3 or ext4 partition. – miyalys Jul 26 '20 at 13:19
  • one option, change permissions for the virtual environment to all users, see https://superuser.com/a/630093/1235905 – Joe' Oct 25 '21 at 19:45

9 Answers9

129

Solution:

If you created the virtualenv as root, run the following command:

sudo chown -R your_username:your_username path/to/virtuaelenv/

This will probably fix your problem.

Cheers

Vingtoft
  • 11,443
  • 17
  • 68
  • 114
  • 1
    This worked for me (chown /usr/local/lib/python3.4), except I don't think I had created a virtualenv as root, I had actually installed Python3.4 as root. I'm still testing, but do you think I will run into any more issues with this solution, or will this chown fix most errors? – ntk4 Jun 22 '17 at 06:37
  • 4
    How do you find out if you've created virtualenv or python as root? – A__ Jan 30 '18 at 01:25
  • This fixed my problem. I was in a similar situation as the OP in that I needed to create a virtual environment in /opt/ directory which I couldn't do without sudo privileges. – sir_chocolate_soup Sep 20 '20 at 17:48
  • 1
    This works. Thanks. – gilzero Sep 24 '21 at 16:26
117

virtualenv permission problems might occur when you create the virtualenv as sudo and then operate without sudo in the virtualenv.

As found out in your question's comment, the solution here is to create the virtualenv without sudo to be able to work (esp. write) in it without sudo.

sebastian_oe
  • 7,076
  • 2
  • 17
  • 20
6

In my case, I was using mkvirtualenv, but didn't tell it I was going to be using python3. I got this error:

mkvirtualenv hug
pip3 install hug -U

....
error: could not create '/usr/lib/python3.4/site-packages': Permission denied

It worked after specifying python3:

mkvirtualenv --python=/usr/bin/python3 hug
pip3 install hug -U
nealmcb
  • 10,965
  • 6
  • 61
  • 85
6

If you created virtual environment using root then use this command

sudo su

it will give you the root access and then activate your virtual environment using this

source /root/.env/ENV_NAME/bin/activate
5

I didn't create my virtualenv using sudo. So Sebastian's answer didn't apply to me. My project is called utils. I checked utils directory and saw this:

-rw-r--r--   1 macuser  staff   983  6 Jan 15:17 README.md
drwxr-xr-x   6 root     staff   204  6 Jan 14:36 utils.egg-info
-rw-r--r--   1 macuser  staff    31  6 Jan 15:09 requirements.txt

As you can see, utils.egg-info is owned by root not macuser. That is why it was giving me permission denied error. I also had to remove /Users/macuser/.virtualenvs/armoury/lib/python2.7/site-packages/utils.egg-link as it was created by root as well. I did pip install -e . again after removing those, and it worked.

AliBZ
  • 3,883
  • 12
  • 43
  • 65
1

You did not activate the virtual environment before using pip.

Try it with:

$(your venv path) . bin/activate

And then use pip -r requirements.txt on your main folder

jmrueda
  • 1,182
  • 14
  • 27
1

I've also had this happen (by accident) after creating a new venv while inside an existing virtual environment. an easy way to diagnose this would be to see where the python is symlinked to, i.e. run:

ls -l venv/bin/python

and make sure it points to the appropriate Python binary. For most systems this will be /usr/bin/python or /usr/bin/python3. while if it points to an existing virtual environment it'll be something like /home/youruser/somedir/bin/python. if it's the latter than I'd suggest recreating the venv while making sure that you aren't "inside" any existing virtualenv (i.e. run deactivate )

Sam Mason
  • 12,674
  • 1
  • 33
  • 48
  • posted here because it's a popular question that mentions the appropriate keywords, hence is more likely to be found and be useful to other people – Sam Mason Jan 03 '20 at 10:52
1

I was getting permission denied when trying to activate my virtual environment. I landed on this page trying to find solutions so perhaps this could also help others who are facing similar issues

source your_env_name_goes_here/bin/activate

I was using the wrong command (without the source), to activate my environment. If you're on zsh that's the correct command to use. If not, python docs has a table of the commands to use depending on your platform and shell (windows or mac, bash or powershell etc)

Juan Hurtado
  • 1,330
  • 7
  • 9
0

While creating virtualenv if you use sudo the directory is created with root privileges.So when you try to install a package with non-sudo user you won't have permission to install into it. So always create virtualenv without sudo and install without sudo.

You can also copy packages installed on global python to virtualenv.

cp -r /lib/python/site-packages/* virtualenv/lib/python/site-packages/
Vkreddy
  • 1,448
  • 15
  • 13
  • This was not the problem: if you read the comments, the OP had created the virtual env with sudo in the first place. – NickD Sep 16 '17 at 13:58