Normally, there's an ad running in this spot, but you're using an ad blocker. To help support our blog, which provides free tutorials for everybody, we just ask that you whitelist or follow us on facebook, twitter, or subscribe using the form to the leftabove. Thank you!

    Python Virtual Environments with Virtualenv

    Select Your OS:

    VirtualEnv allows you us to create isolated Python environments, where a project's dependencies, executables, and even python version are self-contained. This keeps the global filesystem safe and allows us to create multiple Python projects without worrying about potential conflicts.

    Before we can use Virtualenv, we need to install Python, so make sure not to skip the prerequisites.

    System requirements

    • A 32-bit or 64-bit computer with at least 8GB of RAM. 16GB or more is recommended.
    • At least 16GB of free disk space.

    Install Virtualenv

    We're now ready to install Virtualenv.

          # first check if the universe repository is enabled
          sudo nano /etc/apt/sources.list
          # if not, modify the file so it does
          deb xenial main universe
          # update our system and install
          sudo apt-get update
          sudo apt-get install virtualenv
          sudo pip install virtualenv

    Set Up Virtualenv

    Finally, we can start up a Virtualenv. For training purposes, our project's name will be "project". Once you get the hang of it, you may name your project anything you like.

            cd ~
            mkdir project
            cd project
            virtualenv --python=/usr/bin/python2 venv
            source venv/bin/activate
            cd ~
            mkdir project
            cd project
            virtualenv --python=/usr/local/lib/python2.7 venv
            source venv/bin/activate

    Notice that your username in the command line is now prepended with the Virtualenv session you've activated for that terminal session. Run ls to confirm that the venv folder was created.


    This venv folder contains all the files and dependencies for our project, as well as the Virtualenv files. Every project we contain will have its own venv folder, with its own unique dependencies.

    Deactivating Virtualenv

    Use the following commands to exit.

          # from any directory

    Reactivating Virtualenv

    Use the following commands to re-enter the Virtualenv.

          # from the same directory as the venv folder
          source venv/bin/activate

    Did you like this tutorial? Help us pay for server costs by following us on Facebook, Twitter, and subscribing below, where you'll get post notifications, training webinar invites, and free bundles.