Thursday 7 December 2017

Install virtualenv and virtualenvwrapper on Ubuntu

virtualenv is a tool to create isolated environment for Python, so you can develop your dreams without hassels of dealing with version conflicts of required libraries. let say you are working on company accounts system on server using Django 1.7 and you are deploying new analytics tool that employs latest Django 2.0; Now you can not install both versions of Django on server root globally. Any change in libraries or versions of Django can break applications. virtualenv solve this by creating its own installation directories, that doesn't share libraries with other virtualenv environments, thus every environment is independent to work on. this also help when you don't have access to install packages into the global site-packages directories like shared host. Thus virtualenv is a must have tool for python developments. By using the virtual environment, you can use different versions of Python and Python packages on a per project basis.

virtualenvwrapper is a set of extensions to Ian Bicking’s virtualenv tool. The extensions include wrappers for creating and deleting virtual environments and otherwise managing your development workflow, making it easier to work on more than one project at a time without introducing conflicts in their dependencies.

Step-by-Step guide to install virtualenv and virtualenvwrapper


Start terminal and install packages using following commands.
sudo apt-get install python-pip python-dev build-essential

sudo pip install virtualenv virtualenvwrapper
Upgrade pip installed version.

sudo pip install --upgrade pip
Setup virtualenvwrapper in ~/.bashrc to load configurations on system startup.

# Create a backup of your .bashrc file
cp ~/.bashrc ~/.bashrc-org

# Be careful with this command
printf '\n%s\n%s\n%s' '# virtualenv' 'export WORKON_HOME=~/virtualenvs' \
'source /usr/local/bin/virtualenvwrapper.sh' >> ~/.bashrc

Enable the virtual environment for the first time only.

source ~/.bashrc

mkdir -p $WORKON_HOME
Now virtualenv is ready with virtualenvwrapper, let's create our first independent virtual environment.

mkvirtualenv testapp

# Exit the 'testapp' virtual environment
deactivate

Now to enable testapp virtual environment type following command.
workon testapp

To deactivate testapp virtual environment type following command.
workon testapp

Now you are ready to go!

No comments:

Post a Comment