Unable to create spark session
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
The "Unable to create SparkSession" error in PySpark typically comes from missing Java/JDK installation, incorrect JAVA_HOME or SPARK_HOME environment variables, version mismatches between PySpark and Java, or port conflicts from previous Spark instances. The fix depends on the specific error message — the most common resolution is installing Java 8 or 11, setting JAVA_HOME, and ensuring PySpark can find the Spark binaries.
Basic SparkSession Creation
If this fails, check the error message to identify which fix applies.
Fix 1: Install Java and Set JAVA_HOME
Spark requires Java 8, 11, or 17 (depending on Spark version). The most common error is:
Add to your shell profile (~/.zshrc or ~/.bashrc):
Fix 2: Set SPARK_HOME
If you installed Spark manually (not via pip):
For PySpark installed via pip, SPARK_HOME is usually set automatically:
Fix 3: Version Compatibility
Spark, Java, and Python versions must be compatible:
| Spark Version | Java Versions | Python Versions |
| 3.5.x | 8, 11, 17 | 3.8 - 3.12 |
| 3.4.x | 8, 11, 17 | 3.7 - 3.11 |
| 3.3.x | 8, 11, 17 | 3.7 - 3.10 |
| 3.2.x | 8, 11 | 3.6 - 3.9 |
Common mismatch error:
Fix 4: Kill Existing Spark Processes
Previous Spark sessions may hold ports:
Fix 5: Memory Configuration
Insufficient memory allocation causes startup failures:
On systems with limited RAM:
Fix 6: Hadoop/WinUtils on Windows
On Windows, Spark requires winutils.exe:
Download winutils.exe from the Hadoop winutils repository matching your Hadoop version.
Fix 7: Jupyter Notebook Configuration
SparkSession in Jupyter needs findspark or proper environment:
Diagnostic Script
Run this to identify the issue:
Common Pitfalls
- Java not installed or wrong version: Spark 3.x requires Java 8, 11, or 17. Java 21+ may not be compatible with older Spark versions. Check with
java -versionand install a supported JDK. JAVA_HOMEpointing to JRE instead of JDK: Spark needs the full JDK, not just the JRE.JAVA_HOMEshould point to the JDK directory containingbin/javac, not justbin/java.- Calling
SparkSession.builder.getOrCreate()multiple times with different configs:getOrCreate()returns the existing session if one exists, ignoring new configuration. To change configs, callspark.stop()first, then create a new session with the new configuration. - Port 4040 already in use from a previous crash: If a previous Spark session did not shut down cleanly, port 4040 is still bound. Either kill the orphaned Java process or configure a different port with
.config("spark.ui.port", "4041"). - Missing
winutils.exeon Windows: Spark uses Hadoop libraries that expect Unix-like file operations. On Windows, downloadwinutils.exeand setHADOOP_HOMEto the directory containingbin/winutils.exe.
Summary
- Install Java 8, 11, or 17 and set
JAVA_HOME— this resolves most SparkSession creation errors - Use
pip install pysparkfor the simplest installation (includes bundled Spark) - Check version compatibility between Spark, Java, and Python
- Always call
spark.stop()when done to release ports and resources - On Windows, install
winutils.exeand setHADOOP_HOME - Run the diagnostic script to quickly identify missing dependencies

