Creating and Managing Python Virtual Environments with virtualenv on Ubuntu/Debian

Managing Python projects can often lead to conflicts between dependencies and differing versions of packages. This is where virtualenv comes into play. Virtualenv is a tool that allows users to create isolated Python environments, ensuring that each project has its own dependencies and packages, independent of others. This tutorial will guide you through the steps to virtualenv create environment on Ubuntu/Debian Linux systems, providing a clean and controlled workspace for your Python projects.

In this tutorial you will learn:

  • How to install virtualenv
  • Creating a new virtual environment
  • Activating and deactivating the virtual environment
  • Installing packages and testing the environment
  • Managing multiple projects with different environments
Creating and Managing Python Virtual Environments with virtualenv on Ubuntu/Debian
Creating and Managing Python Virtual Environments with virtualenv on Ubuntu/Debian

Setting Up virtualenv

Before diving into creating virtual environments, it’s crucial to set up virtualenv on your system. This section will cover the installation process and initial steps to get started.

  1. Installing virtualenv: First, ensure that Python is installed on your system. This can be done by running python --version or python3 --version in your terminal. If Python is not installed, you can easily install it using Ubuntu’s package manager. Next, install virtualenv by executing the following commands:
    $ sudo apt update
    $ sudo apt install python3-virtualenv

    Alternatively, if you have pip (Python’s package installer) installed, you can install virtualenv using pip with pip install virtualenv. This is a preferred method for users who want to use the latest version of virtualenv.

  2. Creating a New Virtual Environment: With virtualenv installed, you can now create a new isolated environment. Navigate to the directory where you want your environment to be set up and run:
    $ virtualenv MyPythonProject

    Here, MyPythonProject is the name of your virtual environment. It’s a common convention to name it venv, but you’re free to choose any name you prefer. This command creates a directory named MyPythonProject which contains the Python executable files and a copy of the pip library.
  3. Activating the Virtual Environment: To start using the virtual environment, you need to activate it. Activating the environment adjusts your PATH temporarily and sets up your shell to use the environment’s packages and settings. Activate your environment using:
    $ source MyPythonProject/bin/activate

    Once activated, your terminal prompt will likely change to show the name of the activated environment. This indicates that any Python commands you run will now use the environment’s Python interpreter and configuration.

    Activating the Virtual Environment
    Activating the Virtual Environment

Working with virtualenv

  1. Installing Packages and Testing the Environment: With your environment activated, you can now install Python packages within it. For instance, to install Flask, simply run:
    (MyPythonProject) $ pip install flask

    These packages will be local to the virtual environment and won’t interfere with your system-wide Python installation or other virtual environments.

    Installing Packages and Testing the Environment
    Installing Packages and Testing the Environment

    To test if your environment is set up correctly, create a Python script that imports the installed packages and run it. If the script executes without any issues, your environment is ready for development.

  2. Deactivating the Virtual Environment: When you’re done working in the virtual environment, you can deactivate it by running:
    (MyPythonProject) $ deactivate

    This will return your terminal settings to normal and use your system’s default Python interpreter.

    Deactivating the Virtual Environment
    Deactivating the Virtual Environment


Virtualenv is an essential tool for Python developers working on multiple projects or those who need to manage different versions of Python and packages. By following the steps outlined in this tutorial, you can virtualenv create environment on your Ubuntu/Debian system, allowing for a more organized and conflict-free development experience. Remember, each environment is independent, so you can customize each project’s setup as needed without affecting other projects.


Frequently Asked Questions (FAQs)

  1. What is virtualenv?Virtualenv is a tool to create isolated Python environments, allowing for separate project dependencies.
  2. Why use virtualenv in Python?It enables managing different project environments without conflicts in dependencies and package versions.
  3. How to install virtualenv on Ubuntu/Debian?Use sudo apt-get install python3-virtualenv or pip install virtualenv.
  4. How do I create a virtual environment using virtualenv?Navigate to your project directory and run virtualenv venv.
  5. How to activate a virtual environment?Use the command source venv/bin/activate in the terminal.
  6. Can I install packages in a virtual environment?Yes, use pip to install packages, which will be local to that environment.
  7. How to know if I’m in a virtual environment?Check the terminal prompt for the environment name or use echo $VIRTUAL_ENV.
  8. How to deactivate a virtual environment?Simply run deactivate in the terminal.
  9. Does virtualenv work with both Python 2 and 3?Yes, virtualenv supports both Python 2 and 3, but Python 2 reached end-of-life in 2020.
  10. Can I use virtualenv for different projects?Absolutely. It’s recommended to create a separate environment for each project.
  11. Is virtualenv the only tool for creating virtual environments?No, there are other tools like pyenv and venv, but virtualenv is widely used for its simplicity and features.

Comments and Discussions
Linux Forum