Only in Mage Pro.Try our fully managed solution to access this advanced feature.
Overview
The dependency management system provides:- Isolated environments: Each unique combination of dependencies gets its own virtual environment
- Automatic caching: Environments are reused based on content fingerprinting
- Fast installation: Uses
uvfor blazing-fast package installation withpipfallback - Smart compatibility checking: Detects and reuses compatible packages from the global environment
- Safe concurrency: File-based locking prevents race conditions when multiple processes create environments
Using Dependency Management
You can manage dependencies in three ways:1. Via UI (Recommended for Quick Updates)
The easiest way to manage your project dependencies is through the Mage UI:- Navigate to the Terminal page in your Mage workspace
-
Open your requirements file: Browse to
/home/src/[project_path]/requirements.txtand open it in the editor -
Edit your dependencies: Add, remove, or update package requirements in the
requirements.txtfile -
Click the “Install dependencies” button: This will automatically:
- Open a terminal at the bottom of the page
- Execute
mage deps installcommand - Show real-time installation progress and output
2. Via Mage Terminal
For more control, you can runmage deps commands directly in the Mage terminal:
- Open the Terminal page in your Mage workspace
-
Run dependency commands:
3. Via Cluster Restart (Automatic)
When you restart your Mage cluster, the dependency management system automatically:- Reads your latest
requirements.txt: Checks for any changes since the last environment was created - Creates or updates the virtual environment:
- If the requirements haven’t changed, reuses the existing environment
- If requirements have changed, creates a new environment with updated dependencies
- Activates the environment: Makes it available for all pipeline executions
requirements.txt after each cluster restart, with no manual intervention required.
How It Works
Automatic Server-Side Management
When the Mage server starts, the dependency management system automatically handles virtual environment setup:- If no environment exists: A new virtual environment is automatically created based on your project’s
requirements.txt - If an environment exists: The matching environment is automatically activated for use
Environment Fingerprinting
The system computes a deterministic fingerprint hash for each environment based on:- Python version (e.g.,
3.11.8) - Normalized
requirements.txtcontent uvversion (if available)
Directory Structure
Virtual environments are stored under your Mage data directory:.mage_env.json metadata file that tracks:
fingerprint: Environment fingerprint hashpython_version: Python versioncreated_at: Creation timestamp (ISO 8601 format)last_used_at: Last usage timestamp (ISO 8601 format)dependency_source: Source of dependencies (e.g.,requirements.txt)
Package Installation Strategy
The system uses a smart installation strategy to minimize redundant installations:- Global package detection: Scans globally installed packages
- Compatibility filtering: Filters out requirements already satisfied by global packages
- Constrained installation: Installs remaining packages with global package versions as constraints
- Fallback handling: Automatically retries without constraints if dependency conflicts occur
Installation Tools
- Primary:
uv pip install- Fast, modern Python package installer - Fallback:
pip install- Standard Python package installer - Virtual environment creation:
uv venv --system-site-packages
--system-site-packages flag allows the virtual environment to inherit global packages while still allowing user packages to override them when needed.
CLI Commands
mage deps ensure
Create or reuse a virtual environment for your project’s dependencies.
Usage:
project-path(optional): Path to the Mage project (defaults to current directory)--python-version(optional): Python version to use (defaults to current Python version)--output-file(optional): File to write environment path to
mage deps install
Install dependencies from a requirements file using the best available installer.
Usage:
project-path(optional): Path to the Mage project (defaults to current directory)-r, --requirements-file(optional): Path to requirements file (defaults torequirements.txtin project)--override-global-version(optional): Skip global package constraints (default:False)
- Checks if running in an existing virtual environment (
VIRTUAL_ENV) - If yes: Installs packages using advanced features (filtering, constraints, diff analysis)
- If no: Installs packages system-wide using standard pip
mage deps list
List all dependency virtual environments with their metadata.
Usage:
--project-path(optional): Path to the Mage project (defaults to current directory)--python-version(optional): Filter by Python version (e.g.,3.11.8)--verbose(optional): Show detailed information including full paths
mage deps cleanup
Clean up stale virtual environments to free disk space.
Usage:
--project-path(optional): Path to the Mage project (defaults to current directory)--ttl-seconds(optional): Time-to-live in seconds (defaults to 604800 = 7 days)--max-retention(optional): Maximum number of environments to retain across all Python versions--dry-run(optional): Show what would be deleted without actually deleting (default:False)--verbose(optional): Show detailed information about cleanup process
- Identifies environments unused for longer than TTL
- Skips environments with active installation or activation locks
- If
max_retentionis set, keeps the N most recently used environments even if they exceed TTL - Removes eligible environments and their metadata
metadata.yaml:
Features in Detail
Automatic Package Filtering
The system intelligently filters out packages that are already satisfied by globally installed packages:Global Package Constraints
To ensure consistency with the base environment, the system creates aglobal.lock file containing all globally installed package versions and uses it as a constraint file during installation:
requirements.txt doesn’t specify an exact version, the installer uses the same version as the global environment (if compatible), maintaining consistency.
Installation Diff Analysis
After installation, the system analyzes the difference between global and virtual environment packages:Concurrency Safety
The system uses cross-platform file locking to prevent race conditions:- POSIX systems (Linux, macOS): Uses
fcntl.flock() - Windows: Uses
msvcrt.locking() - Lock timeout: 10 minutes (configurable)
- Installation lock (
.install.lock): Exclusive lock held during environment creation and dependency installation - Activation lock (
.activate.lock): Shared lock held when environment is being used - Cleanup lock (
.cleanup.lock): Global lock preventing simultaneous cleanup operations
Environment Validation
After creation, environments are validated to ensure they’re properly initialized:Error Handling
The system provides detailed error messages for common issues:Lock Acquisition Timeout
Installation Failure
Requirements File Not Found
Best Practices
1. Pin Package Versions
For reproducibility, pin exact versions inrequirements.txt:
2. Use Virtual Environments in Development
Always activate the virtual environment when developing:3. Update Dependencies Carefully
When updating dependencies:- Update
requirements.txt - Run
mage deps ensureto create a new environment - Test thoroughly before deploying
4. Monitor Disk Usage
Virtual environments can consume significant disk space. Use the cleanup command to remove stale environments:5. Handle Dependency Conflicts
If you encounter dependency conflicts:- Try without
--override-global-versionfirst (uses global constraints) - If that fails, retry with
--override-global-version - Review the conflict and adjust
requirements.txtif needed
Troubleshooting
Environment Not Found After Creation
Symptom:mage deps ensure succeeds but environment doesn’t exist
Solution: Check for validation failures in logs. The environment may have been created but failed validation and was cleaned up.
Slow Environment Creation
Symptom: Creating environments takes a long time Possible causes:- Network issues downloading packages
- Many packages to install
- Slow disk I/O
- Install
uvfor faster installation:pip install uv - Check network connectivity
- Review
requirements.txtfor unnecessary packages
Permission Errors
Symptom: Cannot create virtual environment due to permission errors Solution: Ensure you have write permissions to the Mage data directory:Stale Lock Files
Symptom: Lock acquisition timeout errors persist Solution: Manually remove stale lock files:Technical Details
Fingerprint Algorithm
The fingerprint is computed as follows:Requirements Normalization
Requirements are normalized before fingerprinting:- Leading/trailing whitespace stripped from each line
- Empty lines removed
- Comments (lines starting with
#) removed - Order preserved
requirements.txt don’t trigger new environment creation.
Lock File Semantics
- .install.lock: Exclusive lock preventing concurrent installation/creation of environment
- .activate.lock: Shared lock indicating environment is actively being used
- .cleanup.lock: Global lock preventing simultaneous cleanup operations across all environments
- global.lock: Snapshot of globally installed packages at environment creation time (used as constraints)
- venv.lock: Snapshot of packages installed in the virtual environment (for diff analysis)
See Also
- Python Packages - Basic requirements.txt usage
- Custom Files - Managing custom Python files
- CLI Commands - Complete CLI reference