How To Install Virtualenv On Ubuntu 16.04

In this tutorial we learn how to install virtualenv on ubuntu 16.04. We will also learn how to activate, installl package and deactivate virtualenv.

Overview

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.

Prerequisites

This tutorial assumes that you already have pip installed on your Ubuntu 16.04.

Step 1 – Install Virtualenv On Ubuntu 16.04


sudo pip install virtualenv

That’s it. Installing virtualenv only have 1 step. Now let’s learn how to use virtualenv.

Using virtualenv on Ubuntu 16.04

Check virtualenv version and path

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__.py

To check virtualenv path we can use the command below


$ which virtualenv 
/usr/local/bin/virtualenv

Create new virtualenv on Ubuntu 16.04

To 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 getip

Activate Virtualenv

To activate virtualenv we can use source to load the virtualenv. The path pattern will be (virtualenvname)/bin/activate


source getip/bin/activate

Of course, we can also use . to load the virtualenv


. getip/bin/activate

Install packages inside virtualenv

In this tutorial, we install requests packages as an example


pip install requests

install virtualenv ubuntu 16.04 - list packages inside virtualenv

Now let’s check whether the requests packages already installed by listing all packages

Deactivate virtualenv

To deactivate current virtualenv we can use the command below.


deactivate

Now when we list all pip packages installed it will also show request but the version is different.


pip list

Install virtualenv ubuntu 16.04 - list system packages

This shows that the requests package installed inside virtualenv is not affecting the package already installed on the system.

Summary

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