Key points
- A Bash script that creates a Python virtual environment, installs
python3-venvwhen it is missing, sets the ownership and installs the packages you name. - On Debian 12 and 13 and Ubuntu 24.04, pip refuses system-wide installs (PEP 668), so a virtual environment is the normal way to install packages.
- uv does the same job faster, with
uv venvanduv pip install.
Simplify Python Development Setup with This Bash Script
Setting up a Python development environment can often feel like a daunting task, especially when managing dependencies and ensuring all tools are up to date. This Bash script simplifies the entire process, automating the installation and configuration of essential components, allowing you to focus on coding.
Updated October 2026: on Debian 12 and 13 and Ubuntu 24.04, pip install outside a virtual environment now stops with an “externally-managed-environment” error (PEP 668), so a virtual environment like the one this script creates is the normal way to install packages. Two notes on the script below: its last line prints a path from my own machine (/home/kodachi/Desktop/epy/venv), so use the path printed just above it; and if you prefer a faster tool, uv does the same job with uv venv and uv pip install.
What is Python Environment Setup?
A Python environment consists of Python interpreters, libraries, and tools required for your project. Setting up this environment ensures that your Python applications run smoothly, avoiding conflicts and missing dependencies.
This script automates tasks such as installing the Python virtual environment module, creating virtual environments and installing required libraries, providing a robust foundation for your Python projects.
Key Features of the Script
- Virtual Environment Creation: Sets up an isolated Python environment for each project, avoiding conflicts with global packages.
- Dependency Installation: Automatically installs
python3-venvif it is missing (pip comes inside every new environment). - Customizable Environment Names: Allows you to name the virtual environment according to your project’s needs.
- Automatic Permissions Management: Ensures correct ownership and permissions for the virtual environment directory.
- Package Installation: Supports installing specific Python packages as arguments to the script.
- Ease of Use: Provides clear instructions for activating and managing the virtual environment.
How It Works
1. Checks for Python Virtual Environment Support
Ensures that python3-venv is installed. If not, the script installs it automatically.
2. Creates a Virtual Environment
- Prompts the user to specify a name for the environment.
- Sets up the virtual environment with correct permissions and ownership.
3. Activates the Environment
Automatically activates the virtual environment and confirms activation.
4. Installs Python Packages
If package names are provided as arguments, the script installs them using pip.
5. Provides Usage Instructions
Displays detailed guidance on activating and deactivating the virtual environment.
Quick Start
1. Download the Script
Save the script at the end of this post as setup_python_env.sh.
2. Make the Script Executable
chmod +x setup_python_env.sh
3. Run the Script
Execute the script with optional Python package names:
./setup_python_env.sh pdfkit markdown reportlab beautifulsoup4
4. Activate the Virtual Environment
source venv/bin/activate
5. Install Additional Packages
Use pip within the activated environment to install more libraries:
pip install tabulate html5lib xhtml2pdf
Benefits
- Time-Saving: Automates repetitive tasks like package installation and environment setup.
- Conflict-Free: Ensures isolated environments for each project, avoiding dependency clashes.
- Beginner-Friendly: Provides clear prompts and error handling for ease of use.
- Scalable: Works seamlessly for single projects or multiple environments.
Conclusion
This Bash script simplifies the process of setting up a Python development environment, making it an indispensable tool for developers of all levels. By automating tedious tasks, it enables you to focus on building and deploying applications efficiently.
Equip your projects with a solid foundation: try this script today and elevate your Python development experience! For a project that runs in such an environment, see my EML to HTML, Markdown and PDF converter.
#!/bin/bash
# =========================================
# Virtual Environment Setup Script
# =========================================
#
# Version: 1.1
# Script written by Warith AL Maawali
# (c) 2024
#
# Discord channel: https://discord.gg/KEFErEx
# Twitter: https://x.com/warith2020
# Linkedin: https://www.linkedin.com/in/warith1977
# Website: https://www.digi77.com
#
# This script creates and activates a Python
# virtual environment, and optionally installs
# specified pip packages.
#
# Usage: ./1.sh [pip_package1 pip_package2 ...]
# =========================================
# Get the directory where the script is located
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# Check if python3-venv is installed
if ! dpkg -s python3-venv >/dev/null 2>&1; then
echo "python3-venv is not installed. Installing now..."
sudo apt update
sudo apt install -y python3-venv
if [ $? -ne 0 ]; then
echo "Failed to install python3-venv. Please install it manually and run this script again."
exit 1
fi
fi
# Ask for the virtual environment name
read -p "Enter the name for the virtual environment (default: venv): " VENV_NAME
VENV_NAME=${VENV_NAME:-venv}
# Name of the virtual environment directory
VENV_DIR="$SCRIPT_DIR/$VENV_NAME"
# Create the virtual environment with correct permissions
if [ ! -d "$VENV_DIR" ]; then
echo "Creating virtual environment in $VENV_DIR..."
python3 -m venv "$VENV_DIR"
if [ $? -ne 0 ]; then
echo "Failed to create virtual environment. Please check your Python installation and try again."
exit 1
fi
# Set correct permissions and ownership for the virtual environment
chmod -R u+w "$VENV_DIR"
chown -R $(logname):$(logname) "$VENV_DIR"
echo "Virtual environment created with correct permissions and ownership."
else
echo "Virtual environment already exists in $VENV_DIR."
# Ensure correct permissions and ownership for existing virtual environment
chmod -R u+w "$VENV_DIR"
chown -R $(logname):$(logname) "$VENV_DIR"
echo "Permissions and ownership updated for existing virtual environment."
fi
# Activate the virtual environment
echo "Activating virtual environment..."
if [ -f "$VENV_DIR/bin/activate" ]; then
source "$VENV_DIR/bin/activate"
else
echo "Failed to find the activation script. The virtual environment may not have been created correctly."
exit 1
fi
# Confirm activation
if [[ "$VIRTUAL_ENV" != "" ]]; then
echo "Virtual environment activated."
else
echo "Failed to activate the virtual environment."
exit 1
fi
# Switch to the virtual environment directory
echo "Switching to virtual environment directory..."
cd "$VENV_DIR"
echo "Current directory: $(pwd)"
# Install pip packages if provided as arguments
if [ $# -gt 0 ]; then
echo "Installing pip packages: $@"
pip install "$@"
fi
# Inform the user how to deactivate
echo "To deactivate the virtual environment, run: deactivate"
# Provide instructions to use pip
echo "You can now use pip within this virtual environment."
# Instructions to navigate to and activate the virtual environment
echo -e "\n### How to Navigate and Activate the Virtual Environment ###"
echo "1. Open a new terminal window or tab."
echo "2. Change to the virtual environment directory by running:"
echo " cd $VENV_DIR"
echo "3. Once inside the directory, activate the virtual environment with:"
echo " source bin/activate"
echo ""
echo "Alternatively, you can activate the virtual environment from any directory using:"
echo " source $VENV_DIR/bin/activate"
echo "============================================================"ShellScript