This guide walks you through setting up a lightweight Large Language Model (LLM) on a Raspberry Pi Zero 2 W. We’ll use Raspberry Pi OS (Legacy, 64-bit) Lite, optimize the Pi for better performance, and install the Ollama application to run the model.
- Raspberry Pi Zero 2 W
- MicroSD card (32GB or larger recommended)
- Micro USB power supply
- USB-to-Ethernet adapter or Wi-Fi connection
- Raspberry Pi Imager
- Raspberry Pi OS (Legacy, 64-bit) Lite (Released: 2024-10-22)
- Download Raspberry Pi Imager from the official website.
- Insert your MicroSD card into your computer.
- Open Raspberry Pi Imager and select:
- Operating System: Raspberry Pi OS (Legacy, 64-bit) Lite
- Storage: Your MicroSD card
- Configure advanced options:
- Enable SSH
- Set a username and password
- Configure Wi-Fi (if needed)
- Click Write and wait for the process to complete.
- Insert the MicroSD card into the Raspberry Pi and power it on.
- Find the Pi’s IP address:
- Run
arp -afrom your computer. - Look for a device labeled
raspberrypi.
- Run
- SSH into the Raspberry Pi:
ssh pi@raspberrypi.local
- If you encounter an SSH error, clear the old host key:
Reattempt the connection and accept the new host key.
ssh-keygen -R raspberrypi.local
Run the following commands to ensure the system is up to date:
sudo apt update && sudo apt full-upgrade -y
sudo rpi-update- Open the configuration file:
sudo nano /boot/config.txt
- Add these lines at the end of the file:
over_voltage=6 arm_freq=1000 force_turbo=1
Warning: Overclocking can void your warranty and potentially damage your hardware.
- Save and exit (Ctrl+O, Enter, Ctrl+X).
- Turn off the current swap file:
sudo dphys-swapfile swapoff
- Edit the swap file configuration:
sudo nano /etc/dphys-swapfile
- Set the swap size to 1GB or higher:
CONF_SWAPSIZE=1024
- Apply the changes:
sudo dphys-swapfile setup sudo dphys-swapfile swapon
Install the necessary Python libraries:
pip install transformers optimum
pip install torch torchvision --extra-index-url https://download.pytorch.org/whl/arm- Download and install Ollama:
curl -fsSL https://ollama.com/install.sh | sh - Verify the installation:
ollama --version
Install and run the smollm2:135m-instruct-q4_K_S model:
ollama run smollm2:135m-instruct-q4_K_SThis command will download the model and start interacting with it.
- The Raspberry Pi Zero 2 W is a resource-constrained device. For better performance, consider upgrading to a Raspberry Pi 4.
- Always monitor the temperature during overclocking to avoid overheating.
Feel free to share your experience or ask questions in the comments!