Get Spark and Python running on your computer
You'll want a Spark and Python notebook to follow along with the book. Installing Spark has a lot of steps, so grab someone to help you with this part β a grown-up, a big sibling, a friend. Two people staring at the same confusing error message is much better than one.
First things first: you need an actual computer for this page. π₯οΈ A Mac, a Windows PC, or a Linux machine β something with a keyboard, a screen, and a terminal you can type commands into. Everything on this page happens by typing commands, so a phone or tablet is not going to work.
- iPad or iPhone? Spark can't run here. There's no Java for iOS, and Apple doesn't allow the kind of program that would install it. That is not a you problem, it's just how those devices work. Head to the cloud notebook page instead β it runs in a web browser, so an iPad with a keyboard is fine.
- Android phone or tablet? Newer Android versions do hide a Linux Terminal in Settings β System β Developer options β "Linux development environment", and a determined grown-up can get Spark going in there. But it's slow and awkward and not a good first adventure. The cloud notebook page is a much better use of your afternoon.
- Chromebook? Most can do it, but you have to switch on the Linux feature first β Settings β About ChromeOS β Developers β Linux development environment β Set up. Then open the Terminal app it gives you and follow the Linux instructions below. Two catches: school-managed Chromebooks often have this switched off and only an administrator can turn it back on, and small or older Chromebooks will struggle. In either case, the cloud will be much happier.
Helpers: getting stuck here is normal. π Installing Spark has an unreasonable number of steps, and people who do this for a living still hit strange errors on a fresh laptop. It's a good part of why companies pay somebody else to run Spark for them. Getting stuck is the ordinary outcome, not a sign you did anything wrong.
If this page starts to feel like a fight, just stop and use a cloud notebook instead. That takes about ten minutes, and the code in the book is identical either way.
What are we installing, and why? π§©
Spark is the program that splits a big job up across lots of computers β the same trick as getting every classroom to count their own results instead of one poor person counting the whole school district. To run it on one computer we need three things:
- Python π β the language we write our programs in. We'll install it with conda, which keeps this project's Python in its own tidy box, separate from anything else on the computer.
- Java β β Spark itself is written in Java and Scala, so even though we type Python, Java has to be there underneath. This is the step people forget most often, and it causes the most confusing error messages.
- PySpark and JupyterLab β PySpark is the Python flavour of Spark, and JupyterLab is the notebook where we type code and see answers appear. We use PySpark 4.2 throughout the book, so that's the version we ask for.
Plan for about 20β40 minutes, most of it waiting for downloads. You'll need a Mac, Windows, or Linux computer with roughly 5 GB of free disk space and an internet connection.
Worth knowing before you start π§ Spark on one computer is slower than plain Python, and that won't be a mistake in your setup. Spark spends real effort planning work and splitting it up, and that effort only pays for itself once the data is bigger than one machine can hold. We install it here to learn the ideas, not to go fast. If you remember one thing about Spark, make it this one: the overhead is the price of being able to add more computers later.
Pick your computer π
Then everybody meets back up at testing that it worked and starting the notebook.
How to read a terminal (60 seconds, worth it) π₯οΈ Everything below happens in a terminal β a window where you type commands instead of clicking. A few things nobody ever tells you:
- The prompt is the little bit of text sitting at the left edge,
waiting for you. It might look like
yourname@laptop ~ %or just$. If you can see the prompt, the terminal is ready for your next command. If it's gone, something is still running β wait for it to come back. - "Run this" means: type the line, then press Enter. One line at a time, Enter after each one. Nothing happens until you press Enter.
- You can copy and paste instead of typing, which is much safer for
long commands. Paste is
β+V on a Mac, Ctrl+Shift+V in most Linux terminals, and Ctrl+V or a right-click on Windows. - Terminals print a lot of text, and some of it is red or says
WARNING. That's normal. Red text is only a problem if the thing you asked for didn't happen. - Nothing on this page can break the computer. The worst that happens is an error message, and an error message is just the computer telling you what it needs.
π Instructions for Mac
Open the Terminal app: press β+Space, type "Terminal",
press Enter. Commands go in that black-and-white window.
Step 1: install conda
We recommend Miniforge: a small, community-run conda that's free for everybody, including at work. (The better-known Anaconda installer now needs a paid licence at larger organisations. Miniforge skips that entire conversation.)
If you already have Homebrew (type brew --version to find out),
it's two lines β the second one wires conda into your shell, which the first one doesn't
do on its own:
brew install --cask miniforge
conda init zsh
Then close Terminal and open it again. You'll know it worked when the
prompt has (base) at the front, like (base) yourname@laptop ~ % β
and you can jump straight to Step 2.
If you don't have Homebrew, that's fine β you don't need it. Run these two lines instead. They work out which Mac you have on their own, so there's nothing to choose:
curl -L -O "https://github.com/conda-forge/miniforge/releases/latest/download/Miniforge3-$(uname)-$(uname -m).sh"
bash Miniforge3-$(uname)-$(uname -m).sh
That second line starts an installer which asks you three things, and this is where everybody gets stuck:
- "Please, press ENTER to continue" β press Enter.
- A huge wall of legal text scrolls past. Tap the space bar to page
through it, or press
qto jump straight to the end. It is very long. It's supposed to be. - "Do you accept the license terms?" β type the whole word
yesand press Enter. Justywon't do for this one. Then it asks whether to initialize conda:yesagain, Enter.
When it finishes, close Terminal and open it again β conda isn't
switched on until you do. You'll know it worked when the prompt has (base) at
the front, like (base) yourname@laptop ~ %.
Prefer clicking to typing? π±οΈ
Miniforge also ships a normal double-click installer for macOS β grab the .pkg
file for your Mac from
conda-forge.org/download.
Afterwards, run conda init zsh in Terminal once so conda switches itself on,
then close and reopen Terminal.
Step 2: make a home for the project
This creates a little sandbox called dc4k with its own Python
and its own Java, so nothing we do here can break anything else on the
computer.
conda create -n dc4k -c conda-forge python=3.12 openjdk=21
conda activate dc4k
It lists a screenful of packages and then asks Proceed ([y]/n)? β type the
letter y and press Enter. (Anything in a terminal offering you
[y]/n works this way: one letter, then Enter. The one in square brackets is what
you get if you just press Enter without typing anything.) Downloading takes a few minutes.
When your prompt comes back it should say (dc4k) at the front instead of
(base).
Remember this one line π
conda activate dc4k β every new Terminal window starts out forgetful, so
you have to type that before running Spark. It's the single most common reason things
"suddenly stop working" tomorrow.
Step 3: install PySpark and the notebook
pip install --upgrade "pyspark[sql]==4.2.*" jupyterlab pandas
That installs PySpark 4.2. It's a biggish download β a few hundred megabytes β so this is
an excellent moment to go get a snack. πͺ It scrolls a lot of text, and it may sit completely
still for a minute at a time. That's normal, so don't press anything. You'll know it finished
when you see a line starting Successfully installed and your prompt comes
back.
Now skip down to testing that it worked.
Homebrew-only alternative πΊ
If you'd rather not use conda at all, brew install openjdk@21 plus
brew install python also works. Two things to know. First, link the Java
install so Spark can find it β Homebrew prints the exact sudo ln -sfn β¦
command to run when it finishes, so copy that. Second, you can't
pip install into Homebrew's Python directly, so make a virtual environment
first:
python3 -m venv ~/dc4k
source ~/dc4k/bin/activate
pip install --upgrade "pyspark[sql]==4.2.*" jupyterlab pandas
We still suggest conda, because it keeps Python and Java together, tells Spark where Java is without you having to, and doesn't touch the rest of your Mac.
π§ Instructions for Linux
Open a terminal β on most systems Ctrl+Alt+T does it. If that does nothing, search your applications for "Terminal" or "Console".
Step 1: install conda
curl -L -O "https://github.com/conda-forge/miniforge/releases/latest/download/Miniforge3-$(uname)-$(uname -m).sh"
bash Miniforge3-$(uname)-$(uname -m).sh
Press Enter at the first prompt, then space-bar through the licence (or press
q to skip to the end), then type the whole word yes to accept it
and yes again when it offers to initialize conda. Then close the
terminal and open a new one. The prompt should now start with
(base).
Step 2: make a home for the project
conda create -n dc4k -c conda-forge python=3.12 openjdk=21
conda activate dc4k
Type y and press Enter when it asks Proceed ([y]/n)?. When the
prompt comes back it changes from (base) to (dc4k). You'll need to
type conda activate dc4k in every new terminal window from now on.
Step 3: install PySpark and the notebook
pip install --upgrade "pyspark[sql]==4.2.*" jupyterlab pandas
Wait for Successfully installed and your prompt to come back.
Prefer your own package manager? π¦
That works too. These commands start with sudo, which means "do this as the
boss of the computer" β it's installing software for everyone who uses the machine, so it
asks for your password first. Your password will not appear as you type it, not
even as dots. That's deliberate, not a broken keyboard: type it and press Enter.
(JDK is just the long name for Java.)
Install a JDK and pip with
sudo apt install openjdk-21-jdk python3-pip python3-venv (Debian/Ubuntu),
sudo dnf install java-21-openjdk-devel python3-pip (Fedora β it dropped its
Java 17 packages in Fedora 42, so 21 is the one to ask for), or
sudo pacman -S jdk21-openjdk python-pip (Arch). If your system says it can't
find Java 21, you're on an older release β ask for openjdk-17-jdk instead, which
Spark 4.2 is equally happy with. Then use a plain virtual environment:
python3 -m venv ~/dc4k
source ~/dc4k/bin/activate
pip install --upgrade "pyspark[sql]==4.2.*" jupyterlab pandas
Just don't pip install straight into the system Python β newer
distributions block that on purpose, and they're right to.
πͺ Instructions for Windows
A shortcut worth knowing about π§
Spark was born on Linux and is happiest there, so the smoothest path on Windows 10 or 11 is
often the Windows Subsystem for Linux. Right-click the Start button, choose
Terminal (Admin), run wsl --install, and let it work β it prints a
lot and then wants you to restart the computer. After the restart, open the "Ubuntu" app
from the Start menu and follow the Linux instructions inside it.
This route also skips step 4 below entirely, which is the part of Windows Spark that nobody enjoys. If it sounds like too much, the native Windows steps work fine.
Step 1: install conda
Download the Miniforge installer for Windows from
conda-forge.org/download
and run it. Accept the defaults, and install it "Just Me" into a path with
no spaces in it β the default,
C:\Users\yourname\miniforge3, is perfect. Spaces in paths confuse Spark.
When it's done, open Miniforge Prompt from the Start menu. That's the window you'll use for everything below β not the regular Command Prompt or PowerShell, because conda is only switched on inside this one.
Step 2: make a home for the project
conda create -n dc4k -c conda-forge python=3.12 openjdk=21
conda activate dc4k
Type y and press Enter when it asks Proceed ([y]/n)?. The
prompt should change to say (dc4k).
Step 3: install PySpark and the notebook
pip install --upgrade "pyspark[sql]==4.2.*" jupyterlab pandas
Wait for Successfully installed and your prompt to come back.
Step 4: the awkward Windows bit (winutils)
On Windows, Spark wants two little Hadoop helper files that it doesn't ship with, and nobody has ever fixed this properly β so we do it by hand. Without them Spark usually still starts, then prints a wall of red and refuses to save anything. Sorry. Here's how to give it what it wants:
- Make a folder called
C:\hadoop\bin. - Download
winutils.exeandhadoop.dllfrom the cdarlint/winutils collection on GitHub. Pick the folder with the highest version number available, open itsbinfolder, then click each file and press the Download raw file button β the small downward arrow near the top right. GitHub shows you a preview page first, so don't worry when the first click doesn't seem to download anything. Windows may also warn you about downloading an.exe; choose Keep. - Put both files in
C:\hadoop\bin. - Tell Windows where they are. This step sounds alarming because Windows calls it
"system settings", but all you're doing is writing down a folder name so Spark can find it
β you can delete it again later and nothing will mind. Press Start, type "environment
variables", and choose Edit the system environment variables β
Environment Variables. Under "User variables" click New and add a
variable named
HADOOP_HOMEwith the valueC:\hadoop. Then findPathin that same list, click Edit β New, and addC:\hadoop\bin. - Close the Miniforge Prompt and open a new one so it notices the change. Don't forget
conda activate dc4kagain.
Two honest warnings β οΈ Only download these files from a source you trust, and never run an installer for them that asks for administrator rights β these are just two small files that get copied into a folder, nothing more.
Also: Spark 4.2 actually ships Hadoop 3.5, and nobody publishes matching Windows
binaries for that yet, so the newest files you can get are a little older than Spark
expects. It normally works anyway. If you still get errors mentioning
UnsatisfiedLinkError after doing all of the above, that mismatch is why β and
the WSL route at the top of this section is the reliable fix.
Testing that it worked (everyone) π§ͺ
In your terminal, with (dc4k) showing in the prompt, run:
pyspark --version
You should see the word Spark drawn out big using slashes and underscores
(that's Spark showing off), a version number starting with 4.2, and a line
naming the Java it found. If you see that, Java and Spark have found each other and the hard
part is over. π
Version 4.2, or: 42 with a dot in it π
Which means we are working with the Spark release that answers life, the universe, and
everything. We pin the book to 4.2 on purpose, so the version on your screen matches the
version in the chapters instead of drifting every few months. If you ever need to check
later, pyspark --version is how.
Now let's actually compute something. Start the interactive shell:
pyspark
Wait for the >>> prompt. It can take 20 seconds and print some
warnings along the way β warnings are fine, that's just Spark talking to itself. Just
above the prompt it should say SparkSession available as 'spark', which is
Spark handing you a ready-made variable called spark. Then run:
spark.range(1000000).filter("id % 7 == 0").count()
It should think for a moment and answer 142858.
Where does that number come from? There are 142,857 multiples of seven under a million β
and 142857 Γ 7 = 999999, which is one of the tidiest facts in arithmetic β and
then zero counts too, because zero divides by everything. So: 142,858.
You just did distributed computing! π
That was Spark splitting a million numbers across all the cores in your computer and
adding up the answers β the exact same trick it uses to split work across a thousand
computers. Your gnomes would be proud. Type exit() to quit.
Starting the notebook (everyone) π
The book uses notebooks, which are much nicer than the shell: you can edit, re-run, and keep your work. In the terminal:
conda activate dc4k
jupyter lab
Two things happen. First, the terminal fills with text and then stops giving you a
prompt back. It isn't frozen β that's JupyterLab running, and it has to keep running the
whole time you use the notebook, so leave that window open and alone. Second, your web
browser opens with JupyterLab in it. (If no browser opens, find the line in the terminal
starting http://localhost:8888/lab?token= and copy that whole line into your
browser's address bar.)
Click Python 3 under "Notebook" to make a new one. A notebook is a stack of boxes called cells: you type code in a cell, then press Shift+Enter (hold Shift down, press Enter) to run it, and the answer appears underneath. Click the first cell, type this, and press Shift+Enter:
from pyspark.sql import SparkSession
spark = SparkSession.builder.appName("dc4k").getOrCreate()
spark.range(1000000).filter("id % 7 == 0").count()
You should get 142858 again, this time inside the notebook. That's the whole
setup done β the rest of the book is the fun part.
Secret bonus: watch Spark work π While a notebook is running, open localhost:4040 in another browser tab to see Spark's own dashboard. Engineers call this the Spark UI, and it's what they're staring at when they say a job is stuck on a stage. It shows the work being chopped into pieces in real time, and it is genuinely fun to watch.
To stop the notebook, go back to the terminal and press Ctrl+C twice. (On Windows, once is enough.)
When things go wrong π§
Almost everyone hits at least one of these. Nothing on this list means the computer is broken.
conda: command not found(or "not recognized")- The installer finished but the terminal hasn't noticed yet. Close every terminal window and open a fresh one. On Windows, use Miniforge Prompt from the Start menu, not Command Prompt or PowerShell.
No such file or directoryright after downloading Miniforge- The file isn't named what the command expected. Run
ls Miniforge3*(Mac and Linux) to see its real name, then use that name. If you downloaded it with a browser instead of the terminal, it's probably in your Downloads folder, so tryls ~/Downloads/Miniforge3*. JAVA_HOME is not set, orUnable to locate a Java Runtime- Java isn't installed, or Spark can't see it. Inside your conda environment, run
conda install -c conda-forge openjdk=21and try again. Check what you've got withjava -versionβ Spark 4.2 runs on Java 17, 21, or 25, and we install 21. UnsupportedClassVersionError, or a complaint about "class file version"- The Java that's installed is too old for this version of Spark. Install a
newer one:
conda install -c conda-forge openjdk=21. TypeError: 'JavaPackage' object is not callable- Almost always the same problem wearing a disguise β Python found PySpark but not a usable Java. Fix Java as above.
error: externally-managed-environmentfrom pip- You're trying to install into the computer's own Python, which modern systems protect
on purpose. Activate your conda environment first
(
conda activate dc4k), or make a virtual environment as described in the Linux section. - A very long red
Py4JJavaErrorwall of text - Don't panic, and don't read all of it. Scroll to the top of the message: the first line or two is the real cause, and everything below it is Spark explaining where it was standing when things went wrong.
- macOS: "Do you want the application to accept incoming network connections?"
- Click Allow. Spark's pieces talk to each other over the network even when they're all on the one computer. Nothing is leaving your machine.
- Windows: errors mentioning
winutils,NativeIO, orUnsatisfiedLinkError - That's step 4. Check that both files really are in
C:\hadoop\bin, thatHADOOP_HOMEis set toC:\hadoop(with no\binon the end), and that you opened a brand new prompt afterwards. If it still complains, it's the Hadoop version mismatch described in step 4, and WSL is the way out. Address already in use, or port 4040 is busy- An older Spark is still running somewhere. Close other notebooks and terminals β or just ignore it, because Spark will quietly move itself to port 4041.
- The notebook can't find PySpark, even though you installed it
- The notebook is probably running from a different Python. Quit it, run
conda activate dc4k, and startjupyter labagain from that same window. Inside a notebook,import sys; print(sys.executable)should show a path withdc4kin it. - A complaint that
PyArrowmust be installed - Something in the book asked Spark to hand data to pandas, which needs an extra piece.
Run
pip install --upgrade "pyspark[sql]==4.2.*"β the[sql]part is what brings it along. - Everything is justβ¦ slow
- On one computer, Spark spends a lot of effort organizing work that a plain Python loop would finish instantly. That's expected, and it's the whole point of the note near the top of this page. Spark's superpower only shows up when the data is bigger than one machine.
- Nothing on this list helps
- Then stop. Local Spark installs go wrong in ways nobody has catalogued yet, and losing a whole evening to one is a bad trade. Use a cloud notebook instead β the code in the book is identical either way, and it's about ten minutes of clicking.
Starting up next time βοΈ
Good news: once it's installed, getting going again is just two lines.
conda activate dc4k
jupyter lab