April 5, 2020 in Tutorial2 minutes

In this tutorial, we learn how to install Python Virtualenv on Ubuntu 16.04.
Virtualenv is a tool to create isolated Python environment. It will create a directory that contains all the necessary executables to the packages that a Python project would need.
This tutorial assumes that you already have pip installed on your Ubuntu 16.04.
sudo pip install virtualenvThat’s it. Installing virtualenv only have 1 step. Now let’s learn how to use virtualenv.
To check the virtualenv version installed, we can use the command below.
$ virtualenv --version
virtualenv 20.0.16 from /usr/local/lib/python3.5/dist-packages/virtualenv/__init__.pyTo check virtualenv path we can use the command below
$ which virtualenv
/usr/local/bin/virtualenvTo create new virtualenv we just need to pass the virtualenv name. This name will also be used as the directory name of the virtualenv. The command below will create new virtualenv named getip
virtualenv getipTo activate virtualenv we can use source to load the virtualenv. The path pattern will be (virtualenvname)/bin/activate
source getip/bin/activateOf course, we can also use . to load the virtualenv
. getip/bin/activateIn this tutorial, we install requests packages as an example
pip install requestsNow let’s check whether the requests packages already installed by listing all packages
To deactivate current virtualenv we can use the command below.
deactivateNow when we list all pip packages installed it will also show request but the version is different.
pip listThis shows that the requests package installed inside virtualenv is not affecting the package already installed on the system.
In this tutorial, we learn how to install Python Virtualenv on Ubuntu 16.04. We also learn how to use virtualenv on ubuntu 16.04