Python Virtual Environment Setup Guide (Ubuntu/Linux)

Prerequisites

Make sure Python and pip are installed:

python3 --version
pip3 --version

If not installed, run:

sudo apt update
sudo apt install python3 python3-pip -y

Install virtualenv

Install virtualenv globally using pip:

pip3 install virtualenv

Create a Virtual Environment

Option 1: Using Default Python Version

virtualenv venv_name

This uses your system’s default Python (usually Python 3).

Option 2: Using a Specific Python Version

virtualenv -p /usr/bin/python3.14 myenv

Replace python3.14 with the path to the version you want.

Activate the Virtual Environment

source myenv/bin/activate

You’ll see the environment name in your terminal prompt like this:

(myenv) user@machine:~$

Install Packages Inside the Environment

pip install package-name

Example:

pip install numpy

Deactivate the Virtual Environment

To exit the virtual environment:

deactivate

Remove a Virtual Environment (Optional)

rm -rf myenv

Tips

venv/
myenv/
A.M. Rinas

Written by A.M. Rinas

Full-Stack Software Engineer working on web applications, DevOps automation, and databases. More about me on the about page, or say hi through the contact page and LinkedIn.

← Back to all articles