Wednesday, May 7, 2014

Setting up python virtualenv without sudo

Setup virtualenv and packages locally without sudo/root

I wanted to create a deployable package into production machines in which I neither have root access nor sudo and also they are firewalled so something simple such as getting pip installation etc is not going to work since the ports are blocked outwards as well.

I usually have ssh and scp access to the machine so I can place files on the server but anything else is pretty locked down.

So after some web research, I came across the following which helped with scripting a solution:
Couple of notes:
  • At the end of this setup, it would have installed a virtualenv with the tools that were packaged in the setup script's directory.
  • We will have the use of pip to make it easier for subsequent installs.
  • However, pip may or may not work to download packages if the system is constrained (with either network firewalling or automatic proxies - I could not figure a way past auto proxies).
  • You have to provide any required packages as well as provide correct versions of packages.
  • This will be a fully isolated setup.
Steps:
  1. Get virtualenv.py from : https://raw.github.com/pypa/virtualenv/master/virtualenv.py 
  2. Get pip from: https://pypi.python.org/pypi/pip#downloads
  3. Get setuptools from https://pypi.python.org/pypi/setuptools, the download is towards the bottom (clicking on download button didn't work for me).
    https://pypi.python.org/packages/source/s/setuptools/setuptools-17.1.1.zip
  4. Get additional packages as tar.gz format from pypi etc. and place them into pypackage directory
  5. Setup the virtual env: (setting up to homedir/myenv shown below)
  6. python $SCRIPTDIR/virtualenv.py --no-setuptools ~/myenv
    
  7. Activate this environment (note the leading 'dot' is required with space following)
  8. . ~/myenv/bin/activate
    
  9. Unpack the pip to some folder (not the virtual env folder):
  10. tar xvzf pip-7.0.3.tar.gz
    
  11. Unpack the setuptools directory:
  12. unzip setuptools-17.1.1.zip
  13. cd to the unpacked directory and setup pip into virtual env:
    cd setuptools-17.1.1
    python setup.py build
    python setup.py install
  14. cd to the unpacked directory and setup pip into virtual env:
  15. cd pip-7.0.3
    python setup.py build
    python setup.py install
    
From this point forward, once you activate the environment, you can run pip on a locally downloaded tar.gz file as:
pip install <path to tar.gz>
And it will install into the virtual environment.

No comments:

Post a Comment