Recommended way for sudo-free installation of Python software with virtualenv

When installing Python software, sudo easy_install and sudo pip are something you should do very seldom. sudo means you are messing with your operating system files. easy_install means that it is easy to install, but impossible to uninstall. You most likely step on the toes your operating system package manager and make your Python installation damaged – damaged in a way you cannot reliably use OS package manager to install or upgrade the software in the future. There is also a high chance that two software depends on the different version of a library X and easy_install will happily overwrite anything with different version in your system.

Even though being badly broken, still plenty of Python software documentation recommends sudo easy_install as an installation method.

virtualenv is a Python tool for creating an isolated Python environment for a normal user. They are isolated in a sense, that only the software you install there will see and mess with the environment. When you are running virtualenv’ed python, pip and easy_install can freely pull in any libraries from pypi without the worry that you break some other software on your computer.

Here is a recipe for installing virtualenv’ed Python software which should across UNIXes and Windows. It will totally run as your normal user privileges, no sudo or admin needed. Only an installed Python interpreter and a working python command are the requirements.

So fire up your console, go the folder where you wish to perform the installation and type the following (some commenting added to explain the process):

# First create the folder where you wish to install the software
# with mkdir command. Then go to this folder with cd command.

# Windows users: Please manually download virtualenv.py
# to the target folder  ith your web browser. UNIX users
# can use curl command line downloader as below.

# Note: Don't rely on operating system virtualenv command.
# It might be hassle to instruct virtualenv package installation
# due to distribution flavours.
# Old Ubuntus ship really old virtualenv.py and it has not worked
# on all cases.
# Github virtualenv.py is the msot reliable method.
curl -L -o virtualenv.py https://raw.github.com/pypa/virtualenv/master/virtualenv.py

# Create a virtualenv environment
# where the software and its dependencies
# will be pulled from PyPi. In our case
# we call the created virtualenv folder "venv"
python virtualenv.py venv

# Activate the virtualenv environment.
# This will set your PATH environment
# variable so that following "python"
# command executes from under the virtualenv,
# not from your global system setup.
#

# Windows equivalent: .\venv\Scripts\activate
. venv/bin/activate

# Now when virtualenv is activated,
# pip and easy_install will install any software
# under this virtualenv environment, not on your operating system files
pip install YOUR_PACKAGE_NAME_ON_PYPI.PYTHON.ORG

# Usually, if you install Python command line software,
# new launcher scripts get created in venv/bin
# folder. When venv environment is active,
# this folder takes precedence in PATH environment
# variable. Meaning, when you have virtualenv activated
# you can simply type in the installed command name
# without full path to execute it.

And again with a real life example:

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

I have tested this recipe with vvv and Skype sevabot and have found it working. However, I wish to get some feedback and ideas how this could be further enhanced, so please send in your ideas.

Some notes

 Subscribe to RSS feed Follow me on Twitter Follow me on Facebook Follow me Google+

11 thoughts on “Recommended way for sudo-free installation of Python software with virtualenv

  1. While this is decent advice for application specific dependencies, the “–user” option to “pip install“ is a much simpler choice if you just want to install some Python packages for your own use without touching the system Python directories.

  2. Nick: That implies that you have pip installed on your system. Again for that you need to have instructions for OS package manager specific instructions how to get pip on your computer in the first place.

  3. The dot notation for sourcing is not standard, either. For example, it does not work with zsh.

  4. Pingback: psutil – advanced OS process management utilities for Python

  5. Pingback: This documentation was brought you by these awesome persons

  6. Pingback: Accepting and spending bitcoins in a Django application

  7. Pingback: Sublime Text 3 for Python, JavaScript and web developers

  8. @Brian: it’s the other way around: “.” is the standard, “source” is a bash synonym.

    @Hieu: “.” may be faster to write, but has *very* poor readability, and it is often overlooked/misnderstood (try to explain a reader that `. ./.file` is NOT a typo :)

    Code is read way more times than it’s written, so being “faster” to write is usually a bad idea. If bash is known to be available, use “source”

  9. Pingback: Turbocharge your Python prompt and Django shell with IPython Notebook

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>