2.3 Library Management: Workspace, Compute-Scoped, Notebook, & Init Scripts
Key Takeaways
- Notebook-scoped libraries installed via %pip or %conda execute in session-isolated Python environments without requiring cluster restarts, ideal for ad-hoc exploration and interactive development.
- Compute-scoped (cluster-level) libraries are installed across the driver and all worker nodes upon cluster startup, ensuring uniform dependency availability across all users and production jobs sharing the cluster.
- Cluster init scripts execute as root during node initialization before the Apache Spark JVM boots; modern governance mandates storing init scripts in Unity Catalog Volumes instead of legacy DBFS paths.
- Init script execution follows a strict deterministic hierarchy: Global Workspace Init Scripts execute first, followed by cluster-scoped init scripts in their explicitly configured order.
Library Management: Workspace, Compute-Scoped, Notebook, & Init Scripts
Data engineering pipelines in Azure Databricks rely heavily on external Python packages, Java/Scala JARs, and custom OS-level drivers. Managing these dependencies requires selecting the appropriate installation scope to prevent version conflicts, ensure security isolation, and avoid unnecessary compute restarts.
1. Library Scoping Models in Azure Databricks
Azure Databricks provides four distinct scoping tiers for library installation:
+-----------------------------------------------------------------------------------------+
| LIBRARY SCOPING HIERARCHY |
+-----------------------------------------------------------------------------------------+
| 1. WORKSPACE LIBRARIES | Stored in Workspace UI / Volumes; available for attachment |
| | to multiple clusters across the organization. |
+---------------------------+-------------------------------------------------------------+
| 2. COMPUTE-SCOPED | Installed on Driver & all Workers during cluster launch. |
| (CLUSTER-LEVEL) | Uniform across all notebooks; requires restart to modify. |
+---------------------------+-------------------------------------------------------------+
| 3. NOTEBOOK-SCOPED | Installed dynamically via %pip / %conda in REPL session. |
| | Session-isolated; no cluster restart; resets on disconnect. |
+---------------------------+-------------------------------------------------------------+
| 4. TASK / JOB-SCOPED | Defined in Lakeflow Jobs DAG task configuration. |
| | Automatically installed on ephemeral job compute. |
+-----------------------------------------------------------------------------------------+
Comprehensive Scope Comparison
| Feature | Workspace Libraries | Compute-Scoped Libraries | Notebook-Scoped (%pip) | Task-Scoped (Jobs) |
|---|---|---|---|---|
| Target | Workspace Repository | Specific Compute Resource | Active Notebook Session | Specific Workflow Task |
| Availability | Available to attach | All users on the cluster | Isolated to current user | Isolated to job task |
| Installation Lifecycle | Manual attachment | Node boot / restart | Dynamic at cell runtime | Ephemeral cluster start |
| Restart Required? | Yes, if attached to compute | Yes | No (Zero Downtime) | No |
| Storage Location | UC Volumes / Maven / PyPI | UC Volumes / CRAN / PyPI | PyPI / Wheel / Repo | UC Volumes / PyPI / JAR |
| Best For | Organization-wide standards | Core team shared dependencies | Fast prototyping & ML testing | Production orchestration |
2. Deep Dive: Notebook-Scoped %pip Management
Notebook-scoped libraries allow developers to install, update, and manage Python packages dynamically within an active notebook session without restarting the cluster or impacting other data engineers working on the same shared cluster.
How %pip Operates Internally
When you execute %pip install <package> in a Databricks notebook cell:
- The Python package is downloaded and installed into the driver node's session-isolated virtual environment.
- Databricks automatically generates a serialized environment snapshot and broadcasts the required wheels/binaries to all active worker nodes.
- The Python REPL process updates its
sys.pathdynamically. - Spark workers automatically load the library into their executor Python worker daemons.
# Command Cell 1: Installing notebook-scoped dependencies
%pip install azure-storage-blob==12.19.0 azure-identity==1.15.0 --quiet
# Command Cell 2: Installing from requirements file stored in a UC Volume
%pip install -r /Volumes/dev_catalog/data_eng/config_volume/requirements.txt
# Command Cell 3: Verifying active environment packages
%pip list | grep azure
Critical %pip Rules for the DP-750 Exam
- First Cell Placement: Always place
%pipinstallation commands in the very first command cell of the notebook. Running%pipafter Python code has already initialized custom classes or C-extensions can lead to classloader conflicts. - Session Isolation: If Engineer A installs
pandas==1.5.0via%pipin Notebook 1, and Engineer B installspandas==2.1.0via%pipin Notebook 2 on the same shared cluster, both engineers execute in complete isolation with zero dependency collision. - Restart Persistence: Notebook-scoped libraries do not persist after the cluster terminates or when the notebook is detached. For automated production jobs, define libraries at the Job/Task level or cluster level.
3. Cluster Init Scripts Architecture & Evolution
An Init Script (initialization script) is a shell script (.sh) that executes during the startup phase of each cluster node—running on the underlying Ubuntu virtual machine before the Spark Driver and Worker JVM daemons launch.
+-----------------------------------------------------------------------------------------+
| CLUSTER NODE BOOTSTRAP & INIT TIMELINE |
+-----------------------------------------------------------------------------------------+
| 1. Cloud VM Provisioning (ARM allocates VM, attaches VNet NIC, mounts OS disk) |
| |
| v
| 2. Base Container Launch (Ubuntu Linux container initializes) |
| |
| v
| 3. GLOBAL WORKSPACE INIT SCRIPTS EXECUTE (Configured by Workspace Admins) |
| |
| v
| 4. CLUSTER-SCORED INIT SCRIPTS EXECUTE (Configured in Compute UI / API in order) |
| - Runs as root user |
| - Installs OS packages (apt-get), custom certificates, ODBC drivers, monitoring |
| |
| v
| 5. Spark JVM & Photon Daemons Start (SparkSession initialized, cluster enters RUNNING) |
+-----------------------------------------------------------------------------------------+
Common Use Cases for Init Scripts
- Installing system-level Linux utilities and packages via
apt-get install(e.g.,unixodbc-dev,libpq-dev). - Installing enterprise network security certificates (custom SSL CA certificates for private enterprise firewalls).
- Configuring custom JVM parameters, security daemons, or enterprise APM monitoring agents (Datadog, Dynatrace, New Relic).
- Configuring custom Linux environment variables (
export ENV_VAR=value).
Unity Catalog Volumes vs. Legacy DBFS Storage
Historically, init scripts were stored in the legacy root DBFS (dbfs:/databricks/init_scripts/ or dbfs:/FileStore/). In modern Azure Databricks environments governed by Unity Catalog, storing init scripts in root DBFS is deprecated and strongly discouraged due to security vulnerabilities (lack of fine-grained access control, unencrypted storage, and broad workspace read access).
+-----------------------------------------------------------------------------------------+
| INIT SCRIPT STORAGE SECURITY EVOLUTION |
+-----------------------------------------------------------------------------------------+
| LEGACY DBFS (DEPRECATED) | UNITY CATALOG VOLUMES (BEST PRACTICE) |
| - Stored in dbfs:/databricks/init_scripts/ | - Stored in /Volumes/catalog/schema/volume/ |
| - No fine-grained access control | - Controlled via UC 'READ VOLUME' privilege |
| - Visible to all workspace users | - Fully audited via UC system tables |
| - High risk of privilege escalation | - Centralized governance & encryption |
+-----------------------------------------------------------------------------------------+
Configuring an Init Script from a Unity Catalog Volume
- Upload the shell script to a governed Unity Catalog Volume:
# Sample init.sh script content
#!/bin/bash
echo "Installing Custom PostgreSQL ODBC Driver..."
apt-get update -y
apt-get install -y unixodbc odbc-postgresql
echo "Setting environment variables..."
echo "DB_TIMEOUT=300" >> /etc/environment
- In the Compute Configuration (or JSON API definition), reference the Unity Catalog Volume path:
{
"init_scripts": [
{
"volumes": {
"destination": "/Volumes/production_catalog/governance_schema/init_volume/install_odbc.sh"
}
}
]
}
Init Script Execution Order & Failure Behavior
- Order of Execution:
- Global Init Scripts (configured in Admin Settings) execute first on every node in the workspace.
- Cluster-Scoped Init Scripts execute second, in the exact sequence specified in the cluster configuration array.
- Execution Privileges: Init scripts run as the
rootuser on Ubuntu Linux. - Failure Handling: If an init script returns a non-zero exit code (
exit 1), the node bootstrap fails immediately. The cluster aborts startup and transitions to anINIT_SCRIPT_FAILUREterminated state, preventing a broken or unconfigured compute environment from serving traffic.
A data engineer needs to install a custom Python library (azure-cosmos==4.5.1) for an exploratory interactive analysis in a shared development cluster without impacting other data engineers who are using conflicting package versions on the same cluster. What is the recommended approach?
Which storage location for cluster init scripts is considered the modern, recommended best practice under Databricks Unity Catalog governance?
In what exact sequence are cluster initialization scripts executed during the boot phase of an Azure Databricks compute node?