Before starting the fine-tuning process, you need to set up the right Python environment with all the necessary libraries. These libraries handle everything from loading the base model, applying LoRA fine-tuning, managing datasets, to extracting content from PDFs.

  1. Uninstall Conflicting Versions (Optional)
  2. Core Machine Learning & Fine-Tuning Libraries
  3. PDF Extraction & Text Processing Helpers
  4. Optional Utilities

Step 1 : Create a Virtual Environment

NOTE : We need python version below 3.12 to make this work , latest version doesn’t support Unsloth. Also if using NVIDIA GPU install CUDA version that is supported by PyTorch - https://developer.nvidia.com/cuda-toolkit-archive VS Code → Terminal → New Terminal, then run:

# Create a folder for your project
mkdir gemma_finetune_project
cd gemma_finetune_project

# Create virtual environment
py -3.11 -m venv .venv

Step 2 : Activate Virtual Environment

On Windows PowerShell:

.venv\Scripts\Activate

You should now see (.venv) prefix in your terminal — that means venv is active.


Step 3 : Install Required Packages

With venv activated, install everything you need:

# OPTIONAL: Clean up old conflicting installs
pip uninstall -y unsloth peft

# ✅ Step 1: Install PyTorch stack with specific CUDA version (don't skip index)
pip3 install torch torchvision torchaudio --index-url <https://download.pytorch.org/whl/cu129>

# ✅ Step 2: Install core ML + fine-tuning libraries WITHOUT touching torch
pip install --no-deps unsloth trl peft accelerate bitsandbytes transformers datasets

# ✅ Step 3: Install Unsloth Zoo separately
pip install diffusers xformers==0.0.27.post2 --no-deps
pip install unsloth_zoo

# ✅ Step 4: PDF/Text helpers
pip install pdfplumber tiktoken sentencepiece

# ✅ Step 5: Optional model storage utility
pip install safetensors

NOTE : If all goes wrong rebuild the virtual environment from scratch using the below command.

COMMAND : deactivate; Remove-Item -Recurse -Force .venv; python -m venv .venv; .\.venv\Scripts\Activate.ps1