We may face issues when our Linux distribution only offers certain versions of Python and its packages, when we actually need newer or older versions.

We can install new versions of Python on the server, however this will be more complex because we will have some dependency issues when trying to compile everything we need.

Virtual environments make this very easy to manage and set up, we can have different versions of Python and packages in each environment, and it will be isolated from the main system.

Installing Virtual Environment on Ubuntu 18.04 -or later from 16.04, is fairly easy task and it shouldn’t take more then 10 minutes to finish.

Install pip

Install pip3 if you don’t have already:

$   sudo apt-get install python3-pip

Instal virtualenv

Install virtualenv package;

$   sudo pip3 install virtualenv

Build New virtualenv

Build a new virtualenv

$   virtualenv .venv

.venv or any $NAME for your virtual environment.

Activate Your virtualev

Activate the virtual environment you above created;

$   source .venv/bin/activate

and you see your .venv activated

(.venv) ~/project$


You can also use Python2.7 interpreter for your virtualenv but I would not recommend that since Python 2.7 will not be maintained after January 1, 2020. But if you like to:

$   virtualenv -p /usr/bin/python2.7 .venv

Install new packages

After activating your virtualenv

$  pip install <some-package>

Deactivate Your virtualev

In order to deactivate the virtual environment just run:

$   deactivate

Bonus: Automation

If you are a lazy developer -like me, you like to to automate this kind of stuff. Instead of typing above commands every time while creating and/or activating a virtual environment, you can use an automation script.

Here is the related article and the script: automate-python-virtualev.

All done!


  • 2022-04-26 : Added automation script link