HomeLinux Administration

How to install and configure Virtual Environment for Python – Virtualenv

How to install and configure Virtual Environment for Python – Virtualenv
Like Tweet Pin it Share Share Email
1x1.trans - How to install and configure Virtual Environment for Python - Virtualenv

Virtual Environment is a tool to keep the dependencies required by different projects in separate places, by creating virtual Python environments for them. It solves the “Project X depends on version 1.x but, Project Y needs 4.x” dilemma, and keeps your global site-packages directory clean and manageable.

For example, you can work on a project which requires Django 1.10 while also maintaining a project which requires Django 1.8.

Virtual Environments in Python allow us to keep project-specific dependencies in a separate place than our global site-packages. This is extremely useful when you have different versions of packages for different projects.

Key features of virtual environments:

  1. Specify the separate version of Python for your individual applications
  2. Install required packages and modules which will be available only for your application
  3. Keep different versions of same package/module in separate environments (you can work on a project which requires Django 1.10 while also maintaining a project which requires Django 1.8) .
  4. Avoid messing up system-wide packages, so you can keep the main system clean
  5. Easily delete or copy virtual environments when needed.


  1. python-pip
  2. setuptools
  3. python-devel   – for Python 2.7
  4. libevent-devel
  5. python34-devel
  6. gcc
  7. gcc-c++
  8. cython

Following commands will help you to setup the virtualenv on CentOS/Redhat Linux 7.


This article will help you to create a lower version of Python Virtualenv (v2.7)., however, these instructions should work fine on Python latest version—if you are still using it, for some reason.

Before you go any further, make sure you have Python installed, and that it’s available from your command line. These instructions assume you’ve already installed a custom version of Python 3. After it’s installed and your shell is using this version

You can check available python version by simply running the below command. If it’s not installed then follow the link to install Python-pip. I have multiple python versions on my server. So it may show different version in your case.

1. First, we need to install the virtualenv package globally:


2. just create a destination directory, then we can directly go and create your virtual environment by specifying Python version (/user/bin/pythonXX) and destination directory: 

if you want to create a different version of python virtualenv, then specify available/installed python binary file location.


3. After virtual environment was created you can switch to it:  using source command you can activate the virtualenv.

You’ll see that default prompt will change. That means your virtualenv is now activated.


4. Then you can install required packages and run scripts: 

From now on, any package that you install using pip will be placed in the folder (python_v.27), isolated from the global Python installation.

Install packages as usual, for example:


5. If you are done working in the virtual environment for the moment, you can deactivate it:

To leave the python virtual environment use deactivate command.

This puts you back to the system’s default Python interpreter with all its installed libraries.


6. If you want to run your script from cron (or from the main system or another script) you just need to specify Python interpreter from the virtual environment:


7. Check with available versions of python virtual environments on the server:


8. To delete a virtual environment, just delete its folder. (In this case, it would be rm -rf python_v2.7.)

To delete a virtual environment from your server, simply delete the project folder which you have created. In this case, delete the  project folder, run the following command:


How to export/freeze the installed packages/libraries from the existing env –  Install the same packages from the source file to new virtualenv.

  • First activate the environment which you want to export the libraries from:

In order to keep your environment consistent, it’s a good idea to “freeze” the current state of the environment packages. To do this, run#


This will create a requirements.txt file, which contains a simple list of all the packages in the current environment, and their respective versions. Later it will be easier for a different developer (or you, if you need to re-create the environment) to install the same packages using the same versions:

Now go to the target/new virtualenv then copy the requirements.txt file from the source location and run the below pip install command like below.

This can help ensure consistency across installations, across deployments, and across developers.

Additional documentation on virtual environments:

Comments (0)

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.