Build a Raspberry Pi Cluster with MPICH and MPI4PY
- Published
- Author
- Patrick Fromaget
- Reads
- 806
Connect Raspberry Pi nodes with MPICH and MPI4PY to run parallel Python work, from shared system setup to network configuration and testing.

A Raspberry Pi cluster is useful for learning how multiple computers coordinate on one workload. Its value depends on the software being run: adding boards doesn’t automatically make every program faster. The setup described here uses MPICH to launch work across nodes and MPI4PY to let Python programs exchange data between processes.

The example uses a Raspberry Pi 5 as the master and a Raspberry Pi 4B as a second node. You can begin with two boards and add more later. Each node needs the same operating system, cluster software, and copies of the scripts you intend to run. The boards also need to share a network and be configured so the master can connect to them over SSH.
What a cluster does
A cluster groups multiple computers so they can work on parts of a shared task. In parallel computing, software divides work among processes, which can run on different CPU cores and machines. If the task can be divided effectively, those processes may finish sooner than a single process running on one computer.
That arrangement differs from load balancing. A load balancer distributes separate requests or jobs among machines to spread demand. In a cluster used for parallel computing, processes cooperate on one job. MPI, or Message Passing Interface, supplies a way for those processes to communicate.
Clusters have been used since the early days of computer networking. The source describes ARCNET as an early commercial cluster approach, linking groups of Datapoint 2200 computers. At the other end of the scale, it cites IBM Summit at Oak Ridge National Laboratory as an example of a large supercomputer, with more than 2 million CPU cores and 3,000 TB of RAM at the time of writing. Those systems operate at a very different scale, but the basic idea—multiple computers coordinating work—also makes a small Raspberry Pi cluster a useful learning platform.
Raspberry Pi boards offer relatively low-cost hardware for experimenting with that model. A cluster of four boards might provide more total CPU capacity for suitable parallel tasks than one board, but performance depends on how well a workload can be split and on the communication it requires. Work that cannot be divided, or that spends much of its time exchanging data, won’t necessarily benefit from adding nodes.

Choose the hardware and layout
For the example, the Pi 5 acts as the master: it prepares and launches work. A Pi 4B serves as the second node. The master’s role here is administrative; it doesn’t mean every calculation happens there. MPI starts processes across the machines listed in the cluster’s host file.
The basic hardware requirements are:
- At least two Raspberry Pi boards. The example uses a Pi 5 and a Pi 4B; other models can also be used.
- One SD card for each board.
- A network connection for every board. Ethernet is preferred for this setup, though Wi-Fi can be configured.
- Power supplies or a multiport power bank with enough connections for the boards.
- A network switch, such as a five-port gigabit switch, if you want to connect the boards by Ethernet.
A case that stacks the boards can make cabling easier to manage and help keep the system organized. The source recommends a cluster case for a setup that will run frequently. Choose a case and cooling arrangement that suit the boards and their operating conditions; the article doesn’t specify a temperature limit or cooling performance.
Plan the SD card images before copying them. The master image must fit on the smallest card you intend to use. The source recommends creating the master on the smallest SD card, because a full image from a larger card won’t fit on a smaller one. Cards used as image destinations need to be at least as large as the source card.
Prepare the master system
Install Raspberry Pi OS Lite on the master using Raspberry Pi Imager. A desktop environment isn’t needed for this command-line setup. Boot the board and complete the initial configuration, then open the Raspberry Pi Software Configuration Tool:

sudo raspi-config
Enable SSH under Interface Options so the master can be managed remotely. Under System Options, set a clear hostname such as master. Update the package lists and installed software, then reboot to apply the changes:
sudo apt update
sudo apt upgrade
sudo reboot
Once the system has restarted, install MPICH from the Raspberry Pi OS package repository:
sudo apt install mpich

MPICH is a free implementation of the MPI standard. It provides the commands and runtime needed to start processes under MPI. The article notes that MPICH can also be downloaded and compiled from its project site, but installing the repository package is the simpler route for this setup.
Check that MPI can launch a command locally:
mpiexec -n 1 date
The command should print the current date. That verifies a basic MPI launch on the master, but it doesn’t yet demonstrate communication between separate Raspberry Pi boards.
Check local MPI launches
Before configuring the second node, make a small Python file on the master. In the home directory, create test.py and enter:

print("Hello")
Run it directly first:
python test.py
The terminal should display Hello. Then ask MPI to start four copies:
mpiexec -n 4 python test.py

You should see four lines of output. This demonstrates that MPI can launch multiple processes on the master. It doesn’t yet use multiple Raspberry Pis, and launching the script repeatedly is different from writing a program that divides work and shares results. That distinction matters when evaluating what a cluster can do for a real workload.
Add Python support with MPI4PY
MPI can be used directly by programs written in languages such as C and Fortran. For Python workloads, install MPI4PY, a Python library that exposes MPI features to Python scripts.
First install the packages listed as prerequisites in the source:
sudo apt install python3-pip python3-dev libopenmpi-dev
Then install the Python package:

sudo apt install python3-mpi4py
Package installation time can vary with the Raspberry Pi model. Once it finishes, the master has the software needed to run the example Python code using MPI. The same environment needs to be present on the other nodes, which is one reason the setup copies the prepared SD card image.
Clone the master image
With the operating system and packages configured, make an image of the master’s SD card. This avoids repeating the installation for every node. If you’re building only a two-node cluster, setting up the second board separately may be quicker than creating and flashing an image; for a larger group, cloning can save repeated preparation.
On Windows, the source uses Win32 Disk Imager to read the master card. Select a destination filename such as cluster_master.img, choose the drive corresponding to the SD card, and use the program’s read function to create the image. The source reports that this took about 15 minutes on its computer; the time will depend on the card and computer. Eject and keep the master card once the image has been created.
On Linux, the source gives this example for reading a card into an image:

sudo dd if=/dev/mmcblk0 > cluster_master.img
Confirm that /dev/mmcblk0 refers to the SD card before running the command. The device name may differ between systems, and selecting the wrong device can read the wrong storage.
Write the image to one SD card for each additional node. Win32 Disk Imager can write the saved image to another card; Etcher is another option. Linux and macOS users can also use dd, as described in the source. The destination card must be at least as large as the card used to create the image.
Each cloned card starts with the same system configuration, including the master hostname. That makes it important to give every node a distinct hostname before using the cluster. Keep the original master card safe as a copy of the prepared system.
Connect and identify the nodes
Insert a prepared card into each Raspberry Pi and connect all the boards to the same network. Ethernet is the preferred connection in the example. For a board that needs Wi-Fi, connect a screen and keyboard, run sudo raspi-config, and use System Options to configure the wireless network, including its country, network name, and passphrase.

Find the master’s IP address by running:
ip a
Look for the address shown after inet on the active network interface. The source’s example address is 192.168.1.69; your network will likely use a different address.
To find the other boards, install Nmap on the master:
sudo apt install nmap

Before changing the cloned boards’ hostnames, they share the name master. The example searches the local subnet for devices advertising that name:
nmap -sP 192.168.1.* | grep master
Change the subnet pattern to match your network. The source identifies a second node at 192.168.1.18 in its example. Record each board’s IP address in a file on the master, one address per line:
cd ~
nano nodes_ips
A sample file might contain:

192.168.1.15
192.168.1.16
192.168.1.17
192.168.1.18
Use the addresses actually assigned to your nodes. MPI will use this host file to identify machines for a distributed launch.
Set unique hostnames and SSH access
Connect from the master to a node using its IP address and your account name:
After accepting the first connection prompt and signing in, run sudo raspi-config. In System Options, change the hostname to a unique name, such as node1, and reboot when prompted. Exit the SSH session and repeat the process for each node, assigning a distinct hostname to each one.

Next, configure passwordless SSH from the master. Generate an RSA key pair on the master:
ssh-keygen -t rsa
Accept the default file location when prompted. The example also accepts an empty passphrase. The private key, id_rsa, stays on the master. The public key, id_rsa.pub, is the file to copy to the nodes.
For each node, copy the public key using its address and the relevant username. The source’s example is:
scp /home/pat/.ssh/id_rsa.pub [email protected]:/home/pat/master.pub

Replace pat with your account name and change the IP address for each node. Then connect to the node and append the public key to its authorized keys file:
ssh [email protected]
cat master.pub >> .ssh/authorized_keys
exit
If the .ssh directory doesn’t exist, create it before appending the key:
mkdir .ssh
Repeat the key transfer and installation for every node. Test a connection from the master with ssh [email protected]. A successful connection should no longer ask for the node account’s password. This lets MPI start processes remotely using the master’s SSH access.

Run commands across the cluster
Once SSH access is configured and nodes_ips contains the nodes’ addresses, try running a command across the cluster:
mpiexec -hostfile nodes_ips -n 8 hostname
The host file supplies the node addresses, while hostname is the command each process runs. In the example, both the Pi 5 and Pi 4B have four cores, so the launch requests eight processes in total. The output should include hostnames from the participating machines, showing where the processes ran.
The value after -n is the number of processes to start, not a setting that automatically measures the available cores. Adjust it to match the process count you want to run. The command is a useful connectivity check, though it doesn’t measure application speed or show that every workload will scale with more processes.
To launch the test Python file on the nodes, use:
mpiexec -hostfile nodes_ips -n 8 python test.py
In the two-node example, the script prints Hello from the processes launched on each machine. MPI runs the program on each node, but it doesn’t transfer the script automatically. Every node must have the same file at the same path.
Create and check the script on the master, then copy it to each node with scp. For example:
scp /home/pat/test.py [email protected]:/home/pat/
Use your own username and each node’s address. Repeat for all nodes before launching the program. This file-copy step also applies to later scripts unless you arrange another way to distribute them.
Use MPI4PY to share data
The point of MPI4PY is to let a Python program use MPI operations, including communication between processes. The example below initializes the MPI communicator, assigns each process a rank, and broadcasts a dictionary from rank 0 to the other processes:
#!/usr/bin/env python
from mpi4py import MPI
comm = MPI.COMM_WORLD
rank = comm.rank
if rank == 0:
data = {'a':1,'b':2,'c':3}
else:
data = None
data = comm.bcast(data,root=0)
print ('rank',rank,data)Only rank 0 starts with the dictionary. The call to comm.bcast distributes that value to the other ranks in the communicator, after which each process prints its rank and the received data. It’s a compact demonstration of coordination; a useful parallel application would need to divide a real workload and decide what information its processes must exchange.
The source gives this launch command for the example:
mpirun.openmpi -np <threads> -machinefile nodes_ips python test.py
Replace <threads> with the number of processes you want to start, and ensure the program file exists at the expected path on each node. The source describes the output as showing the same message for all ranks. More examples of MPI4PY operations are available in its tutorial documentation.
Adding nodes and keeping addresses stable
You can extend the cluster after the initial setup. Prepare an SD card for the new board, boot it on the same network, assign it a unique hostname, configure SSH access from the master, and add its IP address to nodes_ips. Make sure the software and scripts needed for your jobs are present on the new node as well.
IP addresses assigned automatically may change, which can leave the host file pointing to the wrong machine. The source suggests reserving addresses in the router’s DHCP configuration or assigning static addresses on the Raspberry Pi. Whichever method you use, update nodes_ips so that it continues to match the addresses the cluster uses.
This setup demonstrates the mechanics of a small MPI cluster: shared software, network discovery, unique machine names, SSH access, and process launches through a host file. Its practical payoff depends on the program. A script that simply prints a line confirms that the nodes can run code; an application must use MPI to divide work or exchange data before the extra boards can contribute to a shared computation.