Python

Install Python Packages Without Root Permissions with VirtualEnv

One great aspect of Python is that its available in all major distributions of Linux.  Another awesome aspect is the plethora of packages available, which can easily be installed and managed using PIP (Python’s package manager).

If you work for a larger company as a developer however, you will rarely have access to install anything.  Even if you do have some seniority, you might be hesitant about installing packages globally in a production server, or too shy to ask sys admins to install a package every time you need one.

The solution to this problem is virtualenv.  This neat utility allows you to create isolated python environments, without the need for root access to the machine.  Not only that, anything you install will be isolated to your environment, so you can upgrade package versions without fear of breaking any global dependencies.

Here is how to install it:

1. Get Latest Version of VirtualEnv

Use curl to get the latest version from GitHub, and extract it:

curl -O https://codeload.github.com/pypa/virtualenv/zip/master;unzip master
2. Create an Insolated Environment

Execute the virtualenv utility.  You may name the new environment anything.  Here we are calling it “python_env”.

python virtualenv-master/virtualenv.py python_env
3. Activate the Environment
source python_env/bin/activate

Your prompt should now look something like this:

(python_env) renan@linux $
4. Install Packages to Your Heart’s Content

You can now do things like:

pip list
pip install numpy
Final Note

One thing to keep in mind is that you will exit your python environment when you log off.  Whenever you need it, simply activate it by calling the command in step 3.  Just make sure that you are inside the directory where “python_env” is located.

For example:

source PATH_TO_PARENT_DIRECTORY_FOR_PYTHON_ENV/python_env/bin/activate