How to Code a Raspberry Pi: Setup to First Program
In this article
Within about an hour you can have a Raspberry Pi running code you wrote yourself: flash the operating system, boot the board, and run your first Python script. The Pi is designed to be programmed directly, and Python is the language most of its software and examples use. Everything below works on any recent Raspberry Pi board with a standard setup.
What Do You Need Before You Start?
Get these together first so you are not stopping halfway through:
- A Raspberry Pi board (any recent model) with its power supply.
- A microSD card of at least 16 GB, plus a card reader for your main computer.
- A keyboard, mouse and monitor with an HDMI cable — or you can set it up "headless" over your network instead.
- A second computer to write the SD card, with the Raspberry Pi Imager tool downloaded from the official Raspberry Pi site.
- A Wi-Fi network or an ethernet cable for internet access during setup.
Menu names and labels can differ slightly between versions of the software, so treat the names below as a guide rather than exact wording. The official Imager tool is free and works on Windows, macOS and Linux.
How Do You Set Up the Pi for Coding?
Flash the operating system
- Insert the microSD card into your card reader and open Raspberry Pi Imager.
- Choose the standard Raspberry Pi OS as the operating system and select your SD card as the destination.
- Before writing, open the settings gear (or the customisation option) and set a hostname, a username and a password — this saves you a whole configuration step later.
- Click Write and wait about 5-10 minutes for the card to be written and verified, then safely eject it.
First boot
- Insert the SD card into the Pi, connect keyboard, mouse and monitor, then plug in the power last — the Pi starts as soon as it gets power.
- Wait 2-3 minutes for the first boot to finish; the screen may show a rainbow splash first, which is normal.
- Follow the on-screen prompts to connect to Wi-Fi and set your region and keyboard layout.
- When the desktop appears, open a terminal window so you can type commands.
Power note: always shut the Pi down through the menu before pulling the power cable, because cutting power mid-write can corrupt the SD card.
Which Language Should You Write First?
Start with Python unless you already have a reason not to. It comes pre-installed on Raspberry Pi OS, the official examples use it, and you can control the Pi's hardware pins with it. Here is how the main options compare:
| Language | Best for | Already installed? |
|---|---|---|
| Python | First programs, hardware control, scripts | Yes |
| Scratch | Young beginners, drag-and-drop logic | Yes |
| C / C++ | Speed-critical projects, learning low-level code | Compiler yes, editor optional |
| JavaScript / HTML | Web projects served from the Pi | Browser yes, server tools optional |
Decision rule: if you have never coded, choose Scratch for a child or Python for anyone else. If you already know a language, use it — the Pi supports most of them.
How Do You Write and Run Your First Program?
- Open a terminal and type
python3 --versionthen press Enter; a version number confirms Python is ready. - Create your program by typing
nano hello.py— this opens a simple text editor inside the terminal. - Type one line:
print("Hello from my Raspberry Pi") - Save and exit the editor with Ctrl+O, Enter, then Ctrl+X.
- Run the program with
python3 hello.pyand watch your sentence appear on screen. - For a second program that uses hardware, create a new file and use the built-in GPIO library: import it at the top of the file, set a pin as an output, then blink an LED attached to that pin with a small pause between on and off. The official Raspberry Pi documentation has the exact wiring diagram for your board model.
You can also write code in the Thonny editor that comes with the desktop — open it from the programming menu, and it has a Run button so you skip the terminal entirely. Either way, the file you save is the same file you run.
What Goes Wrong for Beginners and How to Fix It?
The Pi will not boot. Re-flash the SD card with the Imager; a corrupted card is the most common cause. Check the power supply is the correct one for your board, since an underpowered Pi fails in odd ways.
"command not found" in the terminal. You have a typo or the tool is not installed. Install software with sudo apt install followed by the package name, after updating with sudo apt update.
Permission denied on a file. Run the command with sudo in front of it only when you are sure you trust the command, since it acts with full administrator rights.
Python says a module is missing. Install it with pip3 install followed by the module name, then re-run your script.
Where Do You Go After the First Script?
Once hello.py runs, the natural next steps are reading a sensor, controlling an LED or button through the GPIO pins, or scheduling your script to run at startup. The official Raspberry Pi projects site walks through each of these with copy-paste code. Keep each project in its own file with a clear name, and back up your SD card image through the Imager's read function once a project works — that gives you a restore point if a later experiment breaks the system.