How to create a Python Virtual Environment on Ubuntu 18.04

Updated , by Linode

Traducciones al Español
Estamos traduciendo nuestros guías y tutoriales al Español. Es posible que usted esté viendo una traducción generada automáticamente. Estamos trabajando con traductores profesionales para verificar las traducciones de nuestro sitio web. Este proyecto es un trabajo en curso.
Try this guide to receive $100 at signup on a new account.
Marquee image for How to create a Python Virtual Environment on Ubuntu 18.04

What is a Python Virtual Environment?

A Python virtual environment is an isolated project space on your system that contains its own Python executable, packages, and modules. Your Python applications and projects often have their own specific dependencies. With a virtual environment you can manage each of your project’s distinct dependencies without having them interfere with each other. You can use the virtualenv tool to create a virtual environment on your system. This guide will show you how to use virtualenv to create and run a Python virtual environment on an Ubuntu 18.04 Linode.

Before You Begin

  1. Complete the Getting Started and Securing Your Server guides to prepare your system.

  2. Update your system:

    sudo apt-get update && sudo apt-get upgrade
    
    Note
    This guide is written for a non-root user. Commands that require elevated privileges are prefixed with sudo. If you’re not familiar with the sudo command, you can check our Users and Groups guide.

Create a Python Virtual Environment

Note
Python 3.6 is the default Python interpreter for the Ubuntu 18.04 distribution.
  1. Install the virtualenv tool using your package manager:

    sudo apt install virtualenv
    
  2. Create a python-environments directory in your user’s home directory and navigate to it:

    mkdir ~/python-environments && cd ~/python-environments
    
  3. Create a Python virtual environment. By default, virtualenv will attempt to use the Python 2.5 interpreter to create a new environment. Since Ubuntu 18.04 does not have Python 2 installed, you should use the --python option to tell virtualenv to use your system’s Python 3.6 interpreter. Replace env with the name you would like to assign to your virtual environment.

     virtualenv --python=python3 env
    

    The command will create a new directory with the name you assigned to your virtual environment. This directory will contain all of the isolated files, packages, modules, and executables that will be used by your new environment.

  4. Validate that your environment is installed with the version of Python that you expect:

    ls env/lib
    

    You should see your env environments Python version:

      python3.6
    

Activate Your Virtual Environment

  1. Activate the newly created virtual environment:

     source env/bin/activate
    

    The name of the working environment will appear in parentheses after it’s created.

      (env) [email protected]:~/python-environments$
    

    You can now begin installing Python packages and libraries that will remain isolated to your virtual environment.

Deactivate a Virtual Environment

  1. To deactivate an active virtual environment, issue the following command:

     deactivate
    

    Your virtual environment will be deactivated and you should no longer see its name listed next to your command line’s prompt

      [email protected]:~/python-environments$
    

This page was originally published on


Your Feedback Is Important

Let us know if this guide helped you find the answer you were looking for.