Dev ❤ Ops

How to install Virtual Environment – Python

A virtual environment is one of the very important tools for testing Python code without affecting the system python and its dependency. We can consider this as a temporary sandbox environment where we can install and use any version of python dependency without modifying system packages.

Step 1: Open terminal
Step 2: Check whether pip package manager is available, if not install it.
[ ~]$ which pip
/usr/bin/which: no pip in (/home/DevOpsForU/.local/bin:/home/DevOpsForU/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/usr/ucb:/bin:/usr/bin)
[ ~]$ which pip3
[ ~]$ pip3 --version 
pip 9.0.3 from /usr/lib/python3.6/site-packages (python 3.6)
Step 3: Install the virtual environment and check
[ ~]$ sudo pip3 install virtualenv
Collecting virtualenv
  Downloading (8.8MB)
    100% |████████████████████████████████| 8.8MB 146kB/s 
  Downloading (461kB)
    100% |████████████████████████████████| 471kB 2.4MB/s 
Installing collected packages: platformdirs, zipp, importlib-resources, typing-extensions, importlib-metadata, filelock, distlib, virtualenv
Successfully installed distlib-0.3.4 filelock-3.4.1 importlib-metadata-4.8.3 importlib-resources-5.4.0 platformdirs-2.4.0 typing-extensions-4.1.1 virtualenv-20.14.1 zipp-3.6.0
Step 4: Create a virtual environment
[ ~]$ virtualenv mytestenv
created virtual environment in 344ms
  creator CPython3Posix(dest=/home/DevOpsForU/mytestenv, clear=False, no_vcs_ignore=False, global=False)
  seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=/home/DevOpsForU/.local/share/virtualenv)
    added seed packages: pip==21.3.1, setuptools==59.6.0, wheel==0.37.1
  activators BashActivator,CShellActivator,FishActivator,NushellActivator,PowerShellActivator,PythonActivator
Step 5: Activate the virtual environment and check
[ ~]$ source mytestenv/bin/activate
(mytestenv) [ ~]$ python --version
Python 3.6.8
Step 6: Install dependency and verify the virtual environment

systools is one of the example shown below this will be only installed under the current virtual environment and will not be present once will be out of the current virtualenv.

(mytestenv) [ ~]$ pip install systools
Collecting systools
  Downloading systools-0.1.0-py3-none-any.whl (266 kB)
     |████████████████████████████████| 266 kB 6.9 MB/s            
Collecting parsedatetime
  Downloading parsedatetime-2.6-py3-none-any.whl (42 kB)
     |████████████████████████████████| 42 kB 277 kB/s  
Step 7: Deactivate the virtual environment
(mytestenv) [ ~]$ deactivate 
[ ~]$ 

Once the virtual environment is activated, it gets visible on the left side of the terminal (Eg: (mytestenv) [ ~]$). We can have multiple virtual environments with different-different of python versions and select them as per need.

This article is created based on experience but If you discover any corrections or enhancements, please write a comment in the comment section or email us at You can also reach out to us from Contact-Us Page.

For more information please visit

Follow us on LinkedIn for updates!

Leave a comment

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