Step-by-Step Guide to Creating Python Virtual Environments on Ubuntu

Table of contents
- π§ Step 1 β Install Python and tools
- π Step 2 β Creating your project folder
- π± Step 3 β Create your virtual environment (.venv)
- π Step 4 β Activate your virtual environment
- π¦ Step 5 β Managing dependencies with pip
- π« Step 6 β Setting up .gitignore
- βοΈ Step 7 β Quick practical example
- β οΈ Common pitfalls
- π― Best practices
- π Conclusion

When you're developing Python applications, properly managing your project's dependencies is crucial. Using virtual environments helps you isolate each project's dependencies, preventing conflicts and simplifying your workflow.
In this short guide, you'll learn step-by-step how to set up and manage Python virtual environments cleanly on Ubuntu.
π§ Step 1 β Install Python and tools
First, make sure you have Python installed. On Ubuntu, Python usually comes pre-installed, but let's ensure everything is up-to-date and install the necessary packages:
sudo apt update
sudo apt install python3 python3-pip python3-venv
π Step 2 β Creating your project folder
Start by creating a dedicated folder for your project and navigating into it:
mkdir my-python-project
cd my-python-project
π± Step 3 β Create your virtual environment (.venv
)
Inside your project folder, create a hidden virtual environment. Naming it .venv
ensures it remains hidden by default, keeping your project tidy:
python3 -m venv .venv
You now have a virtual environment located at my-python-project/.venv
.
π Step 4 β Activate your virtual environment
To start using your virtual environment, activate it with:
source .venv/bin/activate
Your prompt should change, indicating the virtual environment is active, for example:
(.venv) user@machine:~/my-python-project$
To deactivate it later, simply type:
deactivate
π¦ Step 5 β Managing dependencies with pip
With your virtual environment activated, install your project's dependencies using pip
.
For example, installing the popular requests
library:
pip install requests
To save your current dependencies into a requirements.txt
file (useful for version control and sharing):
pip freeze > requirements.txt
To install dependencies from an existing requirements.txt
file (e.g., when cloning a project):
pip install -r requirements.txt
π« Step 6 β Setting up .gitignore
Although Git typically doesn't include hidden folders by default, itβs best practice to explicitly specify folders and files you want to exclude. Create a .gitignore
file in your project root to exclude your environment and common Python caches:
touch .gitignore
Then edit your .gitignore
file with the following content:
# Python virtual environment
.venv/
# Python cache and compiled files
__pycache__/
*.pyc
*.pyo
*.pyd
# Python distribution/build files
build/
develop-eggs/
dist/
.eggs/
*.egg-info/
*.egg
# Misc
.cache/
.vscode/
.idea/
βοΈ Step 7 β Quick practical example
Hereβs a minimal example of setting up a new project using Flask as a quick recap:
# Create and enter project directory
mkdir flask-example && cd flask-example
# Create and activate virtual environment
python3 -m venv .venv
source .venv/bin/activate
# Install Flask
pip install Flask
pip freeze > requirements.txt
Here's a minimal Flask app you could use to test:
from flask import Flask
app = Flask(__name__)
@app.route('/')
def home():
return "Hello from Flask in a clean virtual environment!"
if __name__ == '__main__':
app.run(debug=True)
Run your application:
python app.py
Visit http://127.0.0.1:5000
in your browser, and you should see your Flask app running smoothly!
β οΈ Common pitfalls
π΄ Issue: Virtual environment not activating
Cause: Possibly using incorrect path.
Solution: Double-check your environment path and ensure youβre sourcing it from the correct directory.
source .venv/bin/activate
π΄ Issue: Dependencies not recognized after activation
Cause: You may have installed dependencies outside your virtual environment.
Solution: Always activate your virtual environment before installing dependencies with pip
.
π― Best practices
Always use a virtual environment per Python project.
Clearly name your project directories and virtual environments.
Include your
.venv
directory and common caches explicitly in.gitignore
.Regularly update and maintain your
requirements.txt
for easy collaboration.
π Conclusion
Using virtual environments for Python development under Ubuntu greatly enhances your workflow by isolating dependencies and avoiding conflicts.
Adopt this clean and simple approach systematically, and your Python projects will stay organized, maintainable, and conflict-free!
Happy Python coding! πβ¨
Subscribe to my newsletter
Read articles from Jean-Marc Strauven directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Jean-Marc Strauven
Jean-Marc Strauven
Jean-Marc (aka Grazulex) is a developer with over 30 years of experience, driven by a passion for learning and exploring new technologies. While PHP is his daily companion, he also enjoys diving into Python, Perl, and even Rust when the mood strikes. Jean-Marc thrives on curiosity, code, and the occasional semicolon. Always eager to evolve, he blends decades of experience with a constant hunger for innovation.