🐍 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/

Written by A.M. Rinas