π 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
- You can keep different environments for different projects.
- Use a
.gitignorefile to ignore virtual environment folders in Git projects:
venv/
myenv/
Written by A.M. Rinas