Tutorials‎ > ‎

Set up your Raspberry Pi

It is not essential to use a Raspberry Pi to write programs in Python ... but it's more fun that way! That's assuming that you can actually get your hands on one of these modern marvels.

Below I describe the steps that I took to set up my Raspberry Pi (henceforth RPi) so that I could use it to write Python programs.  This is not an exhaustive guide, and there are many other excellent guides on the web (for example, at elinux, h2g2, lifehacker, piprogramming or here).

You will need:
  • A Raspberry Pi (Model B) board.
  • Basic accessories: power supply (5V), USB keyboard, HDMI cable, TV/monitor, SD card and network cable.
  • Some free time and patience.

(1)  Install the operating system onto your SD card. For this step you will need a PC or Mac with an internet connection and an SD card writer.  (If you have bought a pre-loaded SD card (like this) then you can skip this step.)

First, download the latest version of the Raspbian operating system, available here, onto your PC or Mac. Extract the .img file from the .zip file.  Insert your SD card into the card writer.  Now, follow elinux's step-by-step instructions on how to write to ("flash") your SD card. Warning: If you use the "dd" command-line tool, then you must follow the instructions especially carefully to avoid the danger of overwriting your hard drive!

(2)  Boot and configure. 

Take a look at the Raspberry Pi Quick Start Guide, if you haven't already.

Insert the SD card into the RPi. Connect the RPi to your TV or monitor (with HDMI or DVI connector). Connect the power supply to the RPi, and switch it on at the wall.  A green light should appear on the Pi, and, after a moment, scrolling text should appear on your display.  If the SD card is correctly set up, then you will soon arrive at a configuration screen. Here you should choose your options, and I recommend choosing (at least):
  • expand_rootfs  :  to make use of the whole memory card
  • change_pass : to set a memorable password
  • ssh :  to enable SSH access (this will allow you to log in to your Pi from other devices)
  • boot_behaviour :  to select the "boot to desktop" option

Once you have finished, the RPi will boot to the desktop, so you should arrive at a screen featuring a picture of a large raspberry and some shortcut icons.

On my TV, the resolution is set too high for me to read the text on screen comfortably.  To change this, try:

  1. Open a terminal.
  2. At the prompt, type: 
    sudo nano /boot/config.txt
  3. This will open a simple program, called nano, which allows you to change the contents of the configuration file (config.txt) which is stored in the /boot directory.  We prefix this with "sudo" (short for: super-user do) so that we are granted permission to change this file. Enter your password if prompted.
  4. Uncomment the lines which specify the resolution. By which I mean, remove the # symbol from the following lines
  5. Press Ctrl-x to exit nano, and press "y" when asked, to save the file
  6. Reboot the Pi, with: 
    sudo reboot

You may wish to make other changes to the config file, at your preference.

(3)  Connect to Network and Update      

Connect your Pi to your home hub, using an ethernet cable.  Check the connection by, for example, by 'pinging' a well-known internet site, by typing at the terminal:

ping www.google.com
Now find the IP address that has been assigned to the Pi. To do this, enter

and check "inet addr" under the "eth0:" heading.  It should look something like 192.168.##.##  Note it down: we will use it in Step (5).

The software on your Raspberry Pi may be updated, upgraded and installed using a command line tool "apt-get". To update your Pi, enter

sudo apt-get update
sudo apt-get upgrade

(The second command may take quite a while to complete).

(4) Install Python Libraries

To make the most of Python's capabilities, we need to install some standard libraries.  On the RPi, new software can be installed via the command line with "sudo apt-get install <package-name>".  Many libraries are already installed in the standard distribution of Raspbian, but we need two more: scipy and matplotlib.  Install these with the following commands:

sudo apt-get install python-scipy
sudo apt-get install python-matplotlib

(5) Setup SSH access from PC/Mac

It is very convenient to be able to connect to your Raspberry Pi from another computer (for example, if someone else is watching TV!). On Linux and Mac systems this can be done using the ssh ('secure shell') command line tool. Open a terminal on Linux/Mac, and type:

ssh pi@<IP>
where here <IP> is the IP address you found in step (3).  (Presuming that you have not changed the default username 'pi').  If a connection is made, you will be prompted for your RPi password. Once successfully logged in, you will get a short message and a new prompt, something like:

  pi@raspberrypi ~ $

This indicates that, any commands you enter will now be run on the RPi, rather than on your PC/Mac. To log out again, just type exit.

With X-Window Forwarding, you can even use ssh to run programs that use their own windows. For example, you can run IDLE (the graphical user interface for Python) on your RPi, and get the windows to appear on the screen of your Mac/PC.

ssh -X pi@<IP>

where the -X option turns on X-Window Forwarding, and then, once logged in, type:

idle &

There are two simple steps you can take to make SSH access even easier:

(a) Use an environment variable to store the RPi's IP address. On your PC/Mac, you can edit the .bashrc file, by typing

cd ~
nano .bashrc

Somewhere towards the bottom of this file, add the lines
export RASPI
(where <IP> is your IP address) and save the file. Type source .bashrc

Now you can login to the RPi with the easy-to-remember command

ssh $RASPI

(b) Set up password-less login. Entering your RPi password each time you login using SSH quickly becomes a drag. If you are using a Mac or Linux machine, you can set up password-less login, using RSA encryption keys (an interesting piece of maths!). Try following these instructions.