117

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?

Super Kai - Kazuya Ito
42.9k23 gold badges259 silver badges259 bronze badges
asked Oct 19, 2013 at 22:46
7
  • 2
    Just to clarify: did you create this virtualenv with sudo? Commented Oct 19, 2013 at 22:49
  • @sebastian_oe: I believe I did. I'll recreate one to be sure. Commented Oct 19, 2013 at 22:51
  • 5
    Then this might be the problem. Try creating a virtualenv without sudo. Commented Oct 19, 2013 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? Commented Oct 19, 2013 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. Commented Oct 20, 2017 at 14:47

14 Answers 14

157

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

answered Mar 11, 2015 at 10:42
Sign up to request clarification or add additional context in comments.

5 Comments

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?
How do you find out if you've created virtualenv or python as root?
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.
@A__ the best way is to just never use sudo when installing or managing packages with Pip or venv. If Python came with your system, it is generally not suitable for development; it's there specifically to make your operating system work correctly. Only create user-level venvs, and use Pip at the user level to install in those. Avoid the system Python entirely - while you can use --user to install for the system environment and those packages are ignored by a root user, this can still interfere with using system scripts as a normal user.
@sir_chocolate_soup Why on Earth did the venv need to be there?
123

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.

answered Oct 19, 2013 at 23:02

3 Comments

if I want to create it in /opt which requires sudo, I think virtualenv won't install without sudo.
How to create the virtualenv without sudo?
If you did create the virtualenv with sudo, see answer by @vingtoft for how to fix it easily without recreating
10

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
answered Oct 7, 2019 at 10:02

2 Comments

this helped me with my permission denied errors when installing dependencies from requirements.txt (django) It seems that the venv has to be activated from root.
While this would solve the problem, it bypasses key security protections built in to Linux.
9

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)

answered May 14, 2022 at 10:02

Comments

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
answered Dec 22, 2016 at 5:20

Comments

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.

answered Jan 8, 2015 at 19:04

Comments

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

answered Aug 8, 2017 at 11:59

Comments

1

I had the same problem. I created *and activated a venv as a regular user in Git Bash within VS Code running on Windows 11 and got a "permission denied" when trying to run pip. I could not use sudo at all (corporate laptop).

My solution was:

Create your venv like normal (non-root user):

python -m venv <venv_name>

Activate your venv:

source venv/Scripts/activate

Then, assuming you're using a requirements.txt file, run:

python -m pip install -r requirements.txt

The key is the "python -m"

You may need to upgrade your pip if a package fails to install properly. Try:

python -m pip install --upgrade pip

then try reinstalling your requirements.


If you want to confirm this is the same error to the solution above, you can recreate the error by:

*after you activate your venv, run:

python --version

or

which python

and they should both indicate you're using the python within your venv (again, assuming you activated it)

Then run:

pip --version

and you should see your permission denied.

$ pip --version
bash: \venv/Scripts/pip: Permission denied

Then running

python -m pip --version

should show you the correct pip version of the venv and path within your venv.

answered Apr 20, 2023 at 17:28

Comments

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/
answered Sep 16, 2017 at 13:40

1 Comment

This was not the problem: if you read the comments, the OP had created the virtual env with sudo in the first place.
0

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 )

answered Jan 3, 2020 at 10:49

1 Comment

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
0

On centos 7 this worked:

first, create it

virtualenv --python=/usr/local/bin/python3.8 fastapi

then to activate

source fastapi/bin/activate
answered Oct 5, 2022 at 15:46

Comments

0

I have had this issue also. I've checked my file & directory ownership and permissions using ls -hal. I own the files and they have the right permissions. For me, the issue is running the pip command, but it's the wrong pip. Try running which pip to find out which version of pip is being used. From the activated virtual environment, run python3 -m pip install packagename instead of pip install packagename. If it gets worse, you can run /path/to/the/pip/you/want/pip install filename.

answered Mar 29, 2023 at 17:37

Comments

0

for me, somehow the repo I wanted to install locally had got some different permissions applied. I removed the repo, re-cloned and install worked fine.

cd /path/to/repo
rm -rf package
git clone [email protected]:pipeline/package.git
git install -e /path/to/repo/package
answered May 21, 2024 at 10:57

Comments

0

I was facing the same issue in Windows 11, however, I could not find any specific thread for the same. That's why I am giving the solution here.

  • While creating the venv, make sure the pip in the global installation is up to date. In simple words, before creating the venv, update pip in the main installation of python using \your\python\path.exe -m pip install --upgrade pip (To get the path, use where python).
  • After that, create the venv using \your\python\path.exe -m venv <venv name>
  • Now again list all the python environments using where python and copy the newly created venv path
  • Use this new venv path whenever using any python command. For example \your\new\venv\path -m pip install --upgrade pip or \your\new\venv\path main.py etc.
  • After activating the venv, you can use it normally without mentioning specified python path.

Hope this helps!

answered May 25, 2024 at 7:42

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.