Running PySpark locally & in notebooks
Apache Spark · Spark Fundamentals
Theory
You can learn and test Spark on a laptop. Install Java 17 or 21 and then pip install pyspark, which bundles the Spark engine itself, so no separate download or cluster is needed. In your script or Jupyter notebook you create a SparkSession with master('local[*]'), which runs the driver and executor threads inside one local process and uses all CPU cores (local[2] would use two).
The same code later runs on a cluster: you only change the master or let spark-submit, Databricks, EMR or Kubernetes provide it. Useful local settings are a small spark.sql.shuffle.partitions (the default 200 is meant for clusters) and the Spark UI at http://localhost:4040 to watch jobs. For example, Priya runs her orders notebook in Jupyter with local[*] on a 1 GB sample, then submits the same file with spark-submit --master yarn on the full data.
Why we use it
A local setup gives fast feedback for learning, unit tests and debugging without paying for a cluster. Because the API is identical, code you test locally behaves the same on a real cluster.
When to use it
Use local mode for learning, development, CI tests and small samples. Use a real cluster (or Databricks, EMR, Dataproc, Kubernetes) when data is large or jobs must run on a schedule. In notebooks, create the session once and reuse it.
How it works
- Install Java 17 or 21
- pip install pyspark (and jupyter if needed)
- Create SparkSession with master('local[*]')
- Develop and test on a small sample
- Ready for full data? Yes: spark-submit to YARN, Kubernetes or a managed platform. No: Keep iterating locally.
- Call spark.stop() when finished
Syntax
pip install pyspark # needs Java 17 or 21 on PATH / JAVA_HOME
pyspark # interactive shell with 'spark' ready
spark-submit --master local[4] job.py
SparkSession.builder.master("local[*]").appName("dev").getOrCreate()Example
from pyspark.sql import SparkSession, functions as F
spark = (SparkSession.builder.master("local[1]")
.config("spark.ui.enabled", "false")
.getOrCreate())
orders = spark.createDataFrame([
(101, 1, "North", "Laptop", 1, 55000.0, "2026-01-05"),
(102, 2, "West", "Phone", 2, 30000.0, "2026-01-06"),
(103, 1, "North", "Mouse", 3, 1500.0, "2026-01-06"),
(104, 3, "South", "Laptop", 1, 52000.0, "2026-01-07"),
(105, 4, "West", "Tablet", 1, 25000.0, "2026-01-08"),
(106, 2, "East", "Phone", 1, 15000.0, "2026-01-08"),
(107, 5, "North", "Tablet", 2, 48000.0, "2026-01-09"),
(108, 3, "South", "Mouse", 4, 2000.0, "2026-01-10"),
], "order_id INT, customer_id INT, region STRING, product STRING, "
"qty INT, amount DOUBLE, order_date STRING"
).withColumn("order_date", F.to_date("order_date"))
# On a laptop you would usually write .master("local[*]") to use all cores.
spark.conf.set("spark.sql.shuffle.partitions", "4") # small for local work
print("master:", spark.sparkContext.master)
print("default parallelism:", spark.sparkContext.defaultParallelism)
print("shuffle partitions:", spark.conf.get("spark.sql.shuffle.partitions"))
orders.groupBy("region").count().orderBy("region").show()
spark.stop() # free resources when the script ends
# Output:
# master: local[1]
# default parallelism: 1
# shuffle partitions: 4
# +------+-----+
# |region|count|
# +------+-----+
# | East| 1|
# | North| 3|
# | South| 2|
# | West| 2|
# +------+-----+
Key points
- pip install pyspark includes Spark itself; you only need Java 17 or 21
- local[*] uses all cores, local[n] uses n cores, local is a single thread
- getOrCreate() returns the existing session if one is running, so settings may not change
- Lower spark.sql.shuffle.partitions for small local data (default is 200)
- The Spark UI is at http://localhost:4040 while the session is alive