33

I have easy_install and pip.

I had many errors on my Linux Mint 12, I just re-installed it and I want to install everything from scratch again.

This is one of the errors that I had. I received an interesting answer there:

Stop using su and sudo to run virtualenv.
You need to run virtualenv as your normal user.
You have created the virtualenv with sudo which is why you are getting these errors.

So how to install virtualenv without using sudo? Can i use pipor easy_install without using sudo? Or is there another way?

Community
  • 1
  • 1
Lynob
  • 4,069
  • 12
  • 47
  • 98
  • 2
    Note that when it says `su` and `sudo`, what it actually means is `as root`. You're running `virtualenv` as root, and `virtualenv` doesn't want to be run as root. Drop privileges and try again. – Thomas Wouters Feb 19 '12 at 11:55
  • There is a big difference between installing `virtualenv` as root, verses *running* it as root. The quote you inserted is not talking about installing `virtualenv`, but using it to set up a virtual environment for a Python project. – jpaugh Dec 28 '15 at 15:03

10 Answers10

29

This solution is suitable in cases where no virtualenv is available system wide and you can not become root to install virtualenv. When I set up a debian for python development or deployment I always apt-get install python-virtualenv. It is more convenient to have it around than to do the bootstrap pointed out below. But without root power it may be the the way to go:

There is a bootstrap mechanism that should get you going.

Read: http://peak.telecommunity.com/DevCenter/EasyInstall#creating-a-virtual-python

In essence you would do this in your home directory in a unix environment:

Given your python is version 2.6


    $ mkdir ~/bin
    $ mkdir -p ~/lib/python2.6
    $ mkdir -p ~/local/lib/python2.6/dist-packages
    $ wget http://peak.telecommunity.com/dist/virtual-python.py
    $ python virtual-python.py --no-site-packages
    $ wget http://peak.telecommunity.com/dist/ez_setup.py
    $ ~/bin/python ez_setup.py
    $ ~/local/bin/easy_install virtualenv
    $ ~/local/bin/virtualenv --no-site-packages thereyouare

There may be room for optimization. I don't like the local path. Just bin and lib would be nice. But it does its job.

mustaccio
  • 16,627
  • 14
  • 44
  • 49
itsafire
  • 4,150
  • 3
  • 30
  • 43
  • 1
    Thank you a lot for your solution! Using `python virtual-python.py --no-site-packages --prefix=\`pwd\``, we can install `virtual-python` into folder other than the home folder, to prevent directory pollution. – dmitry_romanov Oct 16 '13 at 14:41
  • 2
    with the -p switch you only need the third line... recursive directory creation just happens. – RobotHumans Apr 13 '14 at 14:29
  • 18
    Perhaps this was valid for older versions of virtualenv. For now, if you want to run virtualenv on a machine you're not root: download and untar one if these: `https://github.com/pypa/virtualenv/releases`, then then just run `python virtualenv.py /path/to/new/virtualenv/to/create` – Claude Feb 10 '15 at 22:17
15

The general idea is to install virtualenv itself globaly, i.e. sudo easy_install virtualenv or sudo pip install virtualenv, but then create the actual virtual environment ("run virtualenv") locally.

ThiefMaster
  • 285,213
  • 77
  • 557
  • 610
  • 5
    // , The question is, though, how. Many of the virtualenv packages, especially for CEntOS 6, end up requiring sudo once installed. – Nathan Basanese Apr 04 '16 at 04:17
13

You can also use the command below, it worked for me without sudo access. You may also need to modify your PYTHONPATH environment variable using export, see this SO answer for more details.

pip install --user virtualenv

Community
  • 1
  • 1
champost
  • 395
  • 3
  • 8
10

http://opensourcehacker.com/2012/09/16/recommended-way-for-sudo-free-installation-of-python-software-with-virtualenv/ suggests the following:

curl -L -o virtualenv.py https://raw.githubusercontent.com/pypa/virtualenv/master/virtualenv.py
python virtualenv.py vvv-venv
. vvv-venv/bin/activate
pip install vvv

It seems to work well. It lets me install https://github.com/miohtama/vvv with pip.

If you get:

Cannot find sdist setuptools-*.tar.gz
Cannot find sdist pip-*.tar.gz

Try --extra-search-dir after downloading the tarballs at https://github.com/pypa/virtualenv/tree/develop/virtualenv_support

Philip Durbin
  • 3,912
  • 2
  • 23
  • 35
4

This worked for me:

pip install --target=$HOME/virtualenv/ virtualenv
cd somewhere/
python $HOME/virtualenv/virtualenv.py env
. env/bin/activate

Now I can pip install whatever I want (except for everything that needs to compile stuff with gcc and has missing dependencies such as the python development libraries and Python.h).

Community
  • 1
  • 1
jozxyqk
  • 14,520
  • 8
  • 69
  • 153
2

I've created a "portable" version of virtualenv.

wget https://bitbucket.org/techtonik/locally/raw/tip/06.get-virtualenv.py
python 06.get-virtualenv.py

It downloads virtualenv.py script with dependencies into .locally subdir and executes it from there. Once that's done, the script with .locally/ subdir can be copied anywhere.

anatoly techtonik
  • 17,421
  • 8
  • 111
  • 131
  • Using your solution I get this error: `Checking hash/size for /files/spegni/.locally/virtualenv.py Traceback (most recent call last): File "06.get-virtualenv.py", line 163, in getsecure(LOOT, filespec) File "06.get-virtualenv.py", line 132, in getsecure check(filepath, entry['hashsize'], quiet) File "06.get-virtualenv.py", line 113, in check % (filepath, shize, hashsize(filepath))) __main__.HashSizeCheckFailed: Hash/Size mismatch for /files/spegni/.locally/virtualenv.py exp: a506a836ef76301bc19e5a209984a4286529a79b 98477 act: d205cbd6783332a212c5ae92d73c77178c2d2f28 9` – FSp Mar 12 '15 at 12:37
  • @FSp, pypa team killed the branch for 1.11.x version. Fixed. Try now. – anatoly techtonik Mar 13 '15 at 09:44
  • 1
    Updated to virtualenv 12.0.7 – anatoly techtonik Mar 13 '15 at 10:00
2

Basically the idea is to install virtualenv (or any other python package) into ${HOME}/.local. This is the most appropriate location since it is included into python path by default (and not only Python).

That you do by pip3 install virtualenv --prefix=${HOME}/.local (you may need to expand ${HOME}). Make sure that you have export PATH=${HOME}/.local/bin:${PATH} in your ~/.profile (you may need to source ~/.profile it if just added)

Slava
  • 1,470
  • 15
  • 19
2

The easiest way I have seen so far is to install Anaconda. It may be an overkill for you. For me the centOS running on the remote server had only python2.6 installed. Anaconda by default installs everything locally + it is python2.7

curl -O https://repo.continuum.io/archive/Anaconda2-4.2.0-Linux-x86_64.sh

Then

bash Anaconda2-4.2.0-Linux-x86_64.sh

Boom. You have all the packages like numpy and pip installed. Then if you want virtualenv, just type

pip install virtualenv
Binu Jasim
  • 260
  • 2
  • 9
  • This doesn't answers OP's question. He already has pip and wants to install virtualenv without doing sudo. – Mohammad Yusuf Dec 03 '16 at 02:53
  • @Ghazi The question says: "I want to install everything from scratch again". Again even if it doesn't answer the OP's question precisely, someone else will find it useful as I. – Binu Jasim Dec 03 '16 at 03:50
  • And what is "everything" defined as? Anaconda is everything according to you? – Mohammad Yusuf Dec 03 '16 at 04:05
  • It is open to interpretation unless the OP clarifies it. I understood it as similar to my situation where he had access to a remote server, so he doesn't have access to root privileges. So he wants to install virtualenv. I think installing anaconda is the easiest way to solve this problem. He needs no sudo privilege for doing that. Moreover he has all the python packages pre-bundled. – Binu Jasim Dec 03 '16 at 08:12
  • 1
    Ok. Fair enough. Your answer just popped up in the review queue for quality check. – Mohammad Yusuf Dec 03 '16 at 09:12
1

You might want to consider using Anaconda. It's a full-fledged Python distribution, that lives in a folder in e.g. your home directory. No sudo is necessary at any point and you get most of the popular packages.

$ wget https://.../Anaconda2-2.5.0-Linux-x86_64.sh # check the website for the exact URL, it can change
$ bash Anaconda2-2.5.0-Linux-x86_64.sh
$ conda install virtualenv
stpk
  • 1,565
  • 1
  • 12
  • 22
1

I solved my problem installing virtualenv for each user.

python3 -m pip install --user virtualenv
Weenit
  • 11
  • 2