Create a Python Virtual Environment on Ubuntu 16.10
Updated by Linode Contributed by Christopher Piccini
What is a Python Virtual Environment?
A Python Virtual Environment - or
virtualenv - is a tool to create an isolated Python environment on your Linode. This can be extremely powerful as you can create a virtual environment and install all Python executables/packages to it, leaving no dependencies outside of your created virtual environment.
The purpose of this tutorial is to allow you to create and run Python virtual environments in your Ubuntu 16.10 Linode.
Before You Begin
This guide will use
sudowherever possible. Complete the sections of our Securing Your Server to create a standard user account, harden SSH access and remove unnecessary network services.
Update your system:
sudo apt update
Install Python Virtualenv
To install Python’s virtual environment:
sudo apt install virtualenv
Create New Directory in Home
Navigate to your users home directory:
Create a directory named python-environments:
Navigate in the newly created directory:
Create a Virtual Environment in Python 3
Create a virtual environment in Python 3 with the environment name of env:
virtualenv -p python3 env
Validate that environment is installed with python3:
Activate the newly created virtual environment (the name of the working environment will appear in parentheses):
source env/bin/activate (env) testuser@localhost:~/python-environments$
Now that the environment is active, you can install executables and packages only to this virtual environment.
To deactivate an active virtual environment:
Congratulations! You have created an isolated, Python Virtualenv on your Linode.
You may wish to consult the following resources for additional information on this topic. While these are provided in the hope that they will be useful, please note that we cannot vouch for the accuracy or timeliness of externally hosted materials.
This guide is published under a CC BY-ND 4.0 license.