Virtualenv is a tool to create virtual Python environments. Virtualenv creates an environment that has its own installation directories, that doesn’t share libraries with other virtualenv environments and optionally doesn’t access the globally installed libraries either.
Before we go ahead and install virtualenv, let's update apt-get and install necessary packages. You can do this by executing the following commands.
# sudo apt-get update # sudo apt-get upgrade # sudo apt-get install build-essential
Now we have necessary packages installed, there are two ways to install virtualenv. The easiest way to install virtualenv is by using apt-get. Use the command below to install virtualenv using apt-get.
# sudo apt-get install python-virtualenv
Alternatively, you can also use pip to install virtualenv, pip is a package management system used to install and manage software packages written in Python. Use following commands to install pip and virtualenv.
# sudo apt-get install python-pip # pip install virtualenv
Creating a new virtual Python environment is easy. First, we need to create a new directory to setup our environment, in this tutorial I will be using "/pytest" directory.
# mkdir /pytest # cd /pytest # virtualenv .
Now we have the virtual environment setup, we can activate the virtualenv by using the following command.
# source /pytest/bin/activate
To deactivate virtualenv, use the following command.