When you write Python programs on Ubuntu, it’s best to use something called a virtual environment. It helps keep your projects clean and organized.
I use virtual environments every time I write Python code. They’ve saved me a lot of stress, especially when different projects need different versions of packages.
In this post, I’ll show you how to set one up step by step.
What Is a Virtual Environment?
Let’s break it down.
A virtual environment is like a mini version of Python that lives in its own folder. It keeps your project’s tools and libraries separate from the rest of your system.
So if Project A needs version 1 of a package, and Project B needs version 2, they won’t fight each other. Each one gets its own little bubble to live in.
This helps avoid conflicts, errors, and broken projects.
Think of it like cooking in separate kitchens. You don’t want your chocolate cake to smell like garlic, right?
Why Use Virtual Environments?
Here are a few reasons I always use them:
- Isolation – Each project has its own Python packages.
- Safety – You don’t mess up your system’s Python by accident.
- Control – You choose exactly what packages to install, and what versions.
It also makes it easier to work with others. If someone else is working on the same project, you can both have the same setup.
Step 1: Update Your System
Let’s start with some cleanup.
Open your terminal and run:
sudo apt update
sudo apt upgrade
This makes sure your system is up to date. It can help avoid weird bugs later.
I like to do this before installing anything new.
Step 2: Install Python3 and pip
Most Ubuntu systems already have Python 3. You can check with:
python3 --version
If you see something like Python 3.10.12
, you’re good.
Next, check if you have pip
, the tool to install Python packages:
pip3 --version
If not, install it with:
sudo apt install python3-pip
Now we’re ready to roll.
Step 3: Install venv
(Virtual Environment Tool)
Ubuntu comes with a tool called venv
that helps create virtual environments.
To install it:
sudo apt install python3-venv
If you don’t install this, you might see errors later. So better safe than sorry.
Step 4: Create a Virtual Environment
Let’s make one.
Go to your project folder (or create one):
mkdir my_project
cd my_project
Now run:
python3 -m venv venv
What does this do?
python3 -m venv
is the command to make a virtual environment.venv
is the name of the folder it creates. You can name it anything, butvenv
is a common choice.
This will create a folder called venv
with a copy of Python and some tools inside.
Step 5: Activate the Virtual Environment
To start using the virtual environment:
source venv/bin/activate
You’ll see your prompt change. It might look like this:
(venv) user@laptop:~/my_project$
That means you’re inside the virtual environment. Now, when you install packages, they go into this folder—not the whole system.
To leave the virtual environment later, just type:
deactivate
That’s it.
Step 6: Install Packages Inside the Virtual Environment
Once your virtual environment is activated, you can install packages just like normal:
pip install requests
It’ll install the requests
library only for this project.
To see what packages you’ve installed:
pip list
If you ever want to copy the list and share it:
pip freeze > requirements.txt
And to install the same packages on another system:
pip install -r requirements.txt
That makes sharing your project easier than sharing snacks at a LAN party.
Three Handy Commands You Should Know
Here are three simple commands I use all the time:
- Create a virtual environment:
python3 -m venv venv
- Activate it:
source venv/bin/activate
- Deactivate it:
deactivate
Keep these in your back pocket—they’ll save you time.
Real-Life Example
I once worked on a Flask app and a Django app on the same computer. Each needed different versions of the same package. Without virtual environments, everything broke. Python yelled at me.
So I created one venv
for Flask and another for Django. No more yelling. Everyone was happy—even my laptop.
How Is This Better Than Using Global Python?
Good question.
Using system Python is like throwing all your tools in one giant toolbox. It works, but it gets messy fast.
Virtual environments let you have a small toolbox for each project. That way, you don’t lose your screwdriver under a pile of hammers.
Three Times You Should Always Use Virtual Environments
If you’re not sure when to use one, here’s a quick list:
- When you’re starting a new project
- When you’re working on multiple Python apps on one system
- When you’re collaborating with other developers
If you’re doing any of these, using venv
will make your life easier. Trust me—I’ve learned the hard way.
Extra Tips
Here are a few tips I picked up while using venv
over the years:
- You can have as many virtual environments as you want.
- You can delete a virtual environment just by deleting the folder.
- Don’t install packages with
sudo pip install
. It can mess up your system Python.
And one more thing—some people like to name their virtual environments things like .venv
(with a dot) to hide them. Up to you. I personally just use venv
.
Comparing venv to Other Tools
There are other tools like virtualenv
and conda
. But I stick with venv
because:
Feature | venv | virtualenv | conda |
---|---|---|---|
Built into Python | ✅ | ❌ | ❌ |
Easy to use | ✅ | ✅ | ❓ |
Works on Ubuntu | ✅ | ✅ | ✅ |
If you’re a beginner on Ubuntu, venv
is the easiest way to start.
Final Thoughts
Setting up a Python virtual environment on Ubuntu isn’t hard once you’ve done it a few times. It just takes a few commands.
And once you get used to using them, you’ll never go back.
I use virtual environments on every Python project, big or small. They keep things clean, help avoid errors, and make teamwork easier.
Next time you write a Python script, try using venv
. It’s like giving your code a nice little home—with just the right amount of space.
Got any fun Python projects in mind? Why not start one today?