Working inside a vagrant environment, inside a python virtual environment, when I try to install a python package using
(venv) vagrant@vagrant-ubuntu-trusty
The root problem is that sudo
does not by default inherit the user's environment as it executes the command. This is what you want - trust me on this.
In your case, your pip
is either guided to the venv that it can't write to or - under sudo - to root's environment where you don't want it to be.
The solution you posted is actually valid: If you use sudo, be sure to tell it exactly what to do, how to do it and whom to do it to! All of the aforementioned can be controlled by the user's environment variables so caution is key.
You may also use sudo -E
, which does inherit the calling user's environment and should therefore preserve your venv. Be sure to read sudo
's man-page or do some googling about all the possible trouble you could get in, though.
I had the same problem with pip vs sudo pip and virtualenv pip vs local pip. I was logged in as root user when I created my venv months ago. So when I wanted to install a new pip package got permission denied. So tried the same command with sudo, but then it installed the package on my local pip.
Lesson learned. I should not use sudo inside a venv.
Fixed it with:
chmod -R 0777 venv_folder_path_here
-R switch for recursive change in venv folder.
And then activate your venv and try pip install:
/home/username_here/venv/project_name_here/bin/activate
(venv_name) pip install package_name_here
Like Daniel said in comments, you should fix the permissions issue with your virtual environment directory. It could be that you already installed something in that directory with sudo. Or you created it with sudo. Which is not ideal. I recommend destroying the virtualenv and then creating it again with the vagrant user. If you are using pyvenv
, make sure you pass --copies
option.
As user27... said in their answer, the pip you run with sudo is probably not the same pip you run as vagrant user. You can always check that with which pip
.
I'd recommend starting with which python
inside your python virtual environment. Perhaps you have activated the wrong virtual environment, not related to your vagrant user at all.