How To Install virtualenv And virtualenvwrapper In Ubuntu

A virtual environment is very useful to python developers as it allows to test different versions of a package in an isolated and dedicated environment inside your machine. The virtualenv tool is used by programmers to create isolated python environments with the main goal of solvingĀ dependencies and versions. I find it very useful and use it every time I am coding something in python.

For example, a web developer uses Django 1.6 Ā for all his projects. How is he going to run a django web application that makes use of Django 1.7Ā without affecting the global site_packages directory? This is a problem that can be solved with the help of the irtualenv tool which can be used to createĀ a virtual environment for a test project with django 1.7 installed.

Note: Every virtual environment you create with virtualenv tool has its ownĀ installation directories, that doesnā€™t share libraries with other virtualenv environments.

How To Install virtualenv In Ubuntu

Before installing virtualenv in Ubuntu we need to install pip which is aĀ is a package management system used to install and manage software packages written in Python. If you are using Ubuntu 12.04Ā like me then use the following commands to install pip3 in your machine.

sudo apt-get install python3-setuptools
sudo easy_install3 pip

Users that make use ofĀ modern versions of Ubuntu can easily install pip3 with the help of the apt-get install command like shown below.

sudo apt-get install python3-pip

Then install virtualenv with the help of pip like shown below.

sudo pip3 install virtualenv

If you like you can also get the latest unreleased version from github, but it is not recommended as it might have bugs and other problems.

Get the latest unreleased dev version:

sudo pip install https://github.com/pypa/virtualenv/tarball/develop

Or install globally from source:

curl -O https://pypi.python.org/packages/source/v/virtualenv/virtualenv-X.X.tar.gz
tar xvfz virtualenv-X.X.tar.gz
cd virtualenv-X.X
sudo python setup.py install

How To Use The virtualenv Tool, Basic Usage

Once the installation of the virtualenv tool has finished you can easily create a virtual environment by typing and running the following command on your terminal.

virtualenv MYVENV

Now cd into the python virtual environment you created. What do you see? If you ls inside MYVENV you will notice three folders have been created:Ā bin, include and lib folder. As you can see the virtualenv you just created is not yet activated, you need to use the following command in order to activate it.

source bin/activate

Now that you learned about creating and activating python isolated virtual environments with the help of the virtualenv tool it is time to makes things a bit easier.

A very helpful tool that helps when working with virtual environments isĀ virtualenvwrapperĀ which is a bash script created by Doug Hellmann with the purpose of making the work with virtual environments easier and less time consuming. Who likes to run source bin/activate every time a python virtual environment needs activation?

Nobody!

So open a new terminal and use the following command.

sudo pip3 install virtualenvwrapper

Add the following two lines in your ~/.bashrc script:

export WORKON_HOME="$HOME/.virtualenvs"
source $HOME/bin/virtualenvwrapper_bashrc
close the bashrc file and source them:

Close the file and run the following command.

source ~/.bashrc

Now everytime you need to create a virtual environment for your python project you should use the following command.

mkvirtualenv unixmen_project

If you like to work on a specific virtual environment just use the workon command followed by the specific name of the virtual environment in which you want to work in.

For example I use the following command to work on virtual environment unixmen_project.

workon unixmen_project

No need for activation. If you want to deactivate the virtual environment just run the following command.

deactivate

Cheers!