How to set up Raspberry Pi

by Liam Fraser

Liam Fraser, creator of the popular Raspberry Pi Tutorials YouTube series and System Administrator for the Raspberry Pi Foundation, shows us how to set up the Raspberry Pi and write our first Python ‘Hello World’ application…

The Raspberry Pi is an exciting device that will hopefully bring computer programming (and Linux) to the masses. The low-priced, single-board computer features 256MB of RAM, a 700MHz ARM processor and a powerful GPU capable of 1080p video playback at 30fps and 3D graphics performance comparable to that of the original Xbox games console.

The idea behind the Raspberry Pi is to provide a computing environment where children can have the freedom to program without having to worry about the consequences of possibly breaking the family computer.

The device has also gained a lot of attention from the hacker community. Want a low-powered, low-cost web server, media centre, or SSH client? Then this could be exactly what you’re looking for. Throw in a network switch and you could have a cluster! The possibilities are virtually endless.

As you might imagine, the Raspberry Pi is a specialised hardware platform, similar to that of a smartphone. For this reason, operating systems are provided as images that are flashed to an SD card. There are currently three Linux flavours for the device: Debian, Fedora and Arch Linux. Debian is our recommended distribution out of the three on offer, and will be the one used in these tutorials.

By this point, you’re probably dying to rip the Pi out of the box and get started, so let’s do it!

01 Download the latest Debian image

Head over to the Downloads section of RaspberryPi.org and download the latest Debian root file system. The image can be downloaded via HTTP download or torrent. The Debian image is roughly 800MB and about 2GB when extracted, so make sure you have enough space free available.

02 Download the SHA-1 checksum

We want to verify the image we just downloaded before we carry on. You need to select the direct download option on the download page to get to the load balancer page. Once you’re there, right click on the SHA‑1 checksum hyperlink and select the option to save the link. Save the file in the same location you saved the Debian ZIP file.

03 Start your terminals

Check your current location using the pwd command. To list the files in the current directory, use the ls command. Use the cd command to navigate to where you saved the image. You can use cd.. to go back a directory. Once you’re in the correct directory, use ls to list the files and check they are both present.

04 Verifying the image

Use the sha1sum command with the following syntax: ‘sha1sum –check [sha1 file]’. Our file was debian6-17-02-2012.zip.sha1. The command may take a few minutes to run and you’ll get a result when it’s finished. If the result is okay, you can carry on. If not, go back to step 1!

05 Extract the Debian image

We now need to extract the image from the ZIP file. To do this, we’ll be using a tool called unzip. You may need to use your package manager to install it if it’s not already on your system. To extract the image, use the unzip command with the following syntax: ‘unzip [zip file]’. In our case, the command was ‘unzip debian6-17-02-2012.zip’. Once the command has finished, use ls to verify that a new folder has been created. This folder will contain the image file.

06 Connect your card reader

Insert your SD card into your card reader and connect it up to your PC if you’re using a USB reader rather than an internal reader. Decline any offers to view the contents of the SD card as we’re still working from the terminal.

07 Locate your SD card

We now need to find the path to your SD card because it could be disastrous if dd was used on the wrong device. We’ll be using the command ‘fdisk -l’. Fdisk requires root privileges so you’ll either need to use the command ‘sudo fdisk -l’ or switch to root using the su command before running ‘fdisk -l’. The ‘fdisk -l’ command simply lists each disk attached to your computer. The SD card we used was 8GB in capacity. There is a disk in our ‘fdisk –l’ output with a capacity of 8068MB, so that must be our SD card. The path to our SD card is /dev/sdb. Make note of yours as you’ll be using it in the next couple of steps.

08 Writing the image to your SD card

Start by using the cd command to change into the directory where the Debian image was extracted. This next part can break your system if you write to the wrong storage device with dd, so check and check again! As with fdisk, the dd command requires root privileges so you either need to start the following commands with sudo or switch to root using su before running them. Use the dd command with the syntax:

dd bs=1M if=[debian .img file] of=[path to your SD card]

In our case, the command was: ‘dd bs=1M if=debian6-17-02-2012.zip of=/dev/sdb’. dd doesn’t display a progress bar but no news is good news in this case. You’ll see a summary of the transfer once the command is finished.

09 Disconnect and reconnect

Before continuing, disconnect and then reconnect your SD card reader to ensure that the kernel has reloaded the newly flashed partition structure.

10 Start up GParted

The Debian image currently only takes up 2GB of space on the SD card, so we need to use GParted to resize the partitions so that we can make use of the full capacity on the card. You may need to install GParted using your package manager if you don’t already have it on your system. Start GParted and make sure that your SD card is selected in the combo box on the top right of the window.

11 Resize your partitions

Use the Resize/Move tool to move the linux-swap partition to the end of the SD card and then resize the main ext4 partition to fill up the space all the way up to the linux-swap partition. You should have two pending operations once you have done this.

12 Apply the changes

Once you’re happy with the new partition layout (making sure you have not moved the FAT32 partition) you can go ahead and click the apply button, which will write the changes to your SD card. The command may take a couple of minutes to complete, so be patient.

13 Connect it up!

Start by inserting your SD card; the gold connectors should be facing upwards as you insert it with the board’s processor facing you. Then connect your USB keyboard and mouse. Connect your display via HDMI or composite. Finally, connect your Ethernet cable; you can use the Pi without one but you’ll have no internet connection to install more packages, and your time and date will have to be set manually (as the device has no battery-powered clock). There’s no power button, so power it on by connecting the micro-USB cable. Notice the cardboard that the Pi is placed on; we wouldn’t want it sitting on any conductive metals now, would we?

14 Log in

The username and password for the image are on the download page. In this case, the credentials are pi/suse. Once you’re logged in, you’ll want to type ‘startx’ to get into LXDE.

15 Set the date & time

This step is only necessary if you’re not using Ethernet, or NTP isn’t getting the correct time for you. Start LXTerminal by going to Menu>Accessories>LXTerminal. Use the date command with a similar syntax to:

sudo date --set “23 APR 2012 16:01”

16 GPU memory allocation

The GPU on the Raspberry Pi shares RAM with the device. There are three different firmware files on the FAT32 partition, each giving a different amount of RAM to the GPU. The allocation you choose depends on what task you want to do on the device. The default for this image is that the GPU uses 64 out of 256MB. There are also options for 32MB and 128MB out of 256MB. The FAT32 partition is mounted in the /boot folder on the Raspberry Pi. To change the GPU’s memory usage, copy the appropriate firmware file with the name start.elf, overwriting the file that is currently there. You’ll have to restart the device for the changes to take effect.

17 Introducing Python

We’re going to finish off by creating a small ‘Hello World’ application in Python. Start LXTerminal again and use the touch command to create a new file called hello_world.py. Mark the file as executable using the command:

chmod +x hello_world.py

18 Hello, World

Open hello_world.py in the nano editor by typing nano. Start by typing the shebang symbol (#!), followed by the path to Python. Use the Return key to add blank line and then add a comment explaining what the application will do:

#! /usr/bin/python
# Our first Hello Word on the Raspberry Pi!

19 Sleeping

We want the application to sleep so we can admire our work. We need to import the time library to do this. Use the print function to output some Hello World text, enclosing the text in quotation marks. Don’t forget to use comments! Using print with empty quotation marks simply outputs a blank line.

20 Sleeping continued

Use the print function to output that the program is going to sleep for five seconds. Then use the time.sleep function, specifying the time in seconds, to make the program sleep.

print ("Sleeping for 5 seconds")
time.sleep (5)

21 Finishing off

Finish your first program off by printing a new line and then printing that the program will exit. The program will simply end when it reaches the last command, which in this case will be ‘print(“Exiting…”)’:

#Print another new line
print("")
print("Exiting...")

22 Saving our work

We save files in nano using Ctrl+O. Press Enter after pressing Ctrl+O to save the output into the hello_world.py file we created earlier. Then you can use Ctrl+X to exit nano.

23 Running our first program

Type ‘./hello_world’ into the terminal and watch your first program run. The ./ directive attempts to execute anything that comes after it. Once you’re finished, close the terminal by typing ‘exit’.

24 Powering off

We still have to turn the Raspberry Pi off nicely, regardless of it having a power button or not. Yanking the power cable could cause data corruption and we don’t want that. Go the LXDE menu and select ‘logout’. Wait for the system to halt fully before removing the power cable.

Why do we need a MS Windows file system (VFAT32) to start up a Linux system (Debian)?

Can’t Debian boot the system?

John Yost

A patented MS Windows file system!
give me a break.
VFAT32

Holden

Hello, great tutorial Liam, thanks. I think I spotted a small error though: at point 08, you want to write the extracted Debian image to your sd card with dd, not the .zip file containing the image. So the right command example should be: ‘dd bs=1M if=debian6-17-02-2012.img of=/dev/sdb’

Cronin

This is an ok post, but totally misses the Raspberry Pi targeted audience… little kids. I understand this is posted to linuxuser. and the majority of readers are linux users, however, we need to include extremely detailed instructions in every operating system and provide the tools.

It’s like the opposite of the phrase the blind leading the blind…. this is the seeing leading the seeing. What is the point? I haven’t looked at Liam’s other stuff but if you read this Liam, I hope in posts other than linuxuser you make it so easy that an 8 year old or an 80 year old could do it. Someone who has never used linux in their life should be able to do it. If you say the word “terminal” in the instructions then you’ve gone too far. That’s something that comes later.

For current UNIX/Linux users who know what the terminal is but are unfamiliar with the command line then sure it’s is ok, For the 99 percent of the rest of the population this is inadequate and can turn people off of the product should they find this article before finding a better one. Remember setting it up should be easy as ‘pi’… using it is where the learning begins.

gus3

You forgot the most important step: brag to all your geeky friends about getting your RPi. I prefer to start my brag thus: “I am holding in my hand…” (dramatic pause for effect) “… the future.” A little explanation about what it is (not a PC-compatible), what it does (everything required to be Turing-complete), what it costs (followed by the *thud* of jaws meeting the floor), and the purpose of the Raspberry Pi Foundation then ensues.

Leroy Scandal

Sending a USR1 signal to a running `dd’ process makes it print I/O
statistics to standard error and then resume copying.

Cronin, Id agree with you but the first Pi’s are for developers so they can busy creating and modifying code for the Pi. Its not for joe user with no computer knowledge although at that price, its better to learn on the Pi than your 600$ machine.

The educational part of the project hasnt been lauched yet so Ill presume you arent aware of that and not call you a troll.

Every time Ive heard Eben speak he REITERATES that their goal isnt world dominination but rather getting kids interested into programming.
THAT IS THE GOAL.

I am not sure however how come some schools have already received the Pi if the educational component isnt ready yet.

Taking in consideration the plans for the plan and where we are located in that timeline, it is VERY important to remind developers and others of the things you mention: many of the users of the Pi will NOT have geeks for dads who can show them these things (and I dont think geeks know how to teach what they know well). Its the ones with parents who dont know tech (not toys like many dads think technology is) and have only the internet, forums and youtube to guide them.

If Bob User reads about the Pi and decides to buy it for his niece and nephews (its the same price as a starter Chemistry set) but knows nothing about Linux or computers, then the material and tutorials HAVE to be able to talk to children in the 4-8, 9-12, 13-18yr old categories at their level of understanding.

So yes, you are perfectly right about that. Its one of the things I am looking forwards to seeing the most (ive done arduinos and beagleboards before, I like the Pi price and educational goal) in this project.

By the time all the geeks have gotten their hands on the Pi, I expect that this whole 25 step processs, will be whittled down to Download This, Copy it on SD card, start computer. Or even easier, someone starts selling SD cards with the OS already on it.

THIS tutorial above is NOT at that stage yet.

how it gets to there will be just as important as how it communicates the knowledge to children.

My grade school kids know how to install Linux because its so simple. DL the distro, burn on CD, play the Live CD and the install (press the Enter key a dozen times). the challenge will be to make it even simpler for the Pi.

Ilams videos are great for us to get an idea but in no way are they to be used by young newbiesl it would discourage them right away. tahts why he will need another channel like Pi for Kids Tutorials or similar to differentiate between it and his regular tutorials meant more for devs.

Ive seen hundreds of soccer and judo coaches in my life but every time I see one that knows how to connect with kids and make them do more than youd think possible, I realize how hard it is to do and how important reaching out to the kids with the Pi will be.

At 03 start your terminals , it is suggested that we use the cd command to navigate to where we saved the image.
We are working on a virtualbox whereas the files were saved under Windows XP (in my case) and I can’t see how I can thus find them.
I am missing something but can’t figure out what exactly.
Can you help please?

Smith

ian, you need to copy the image into your virtual machine. You can do this by creating a shared folder with your virtual machine (look into getting the virtualmachine attached to your network for this, it will make it easier), or copying to CD or USB before attaching it to the virtual machine.

Also, on a note, if youre learning something other than straight programming, say building a small server, then learning to verify and install linux images is a very important step. Its still part of the learning process. I would say this tutorial is *nearly* spot on, except that someone who has never used linux wouldnt know where to start. They should probably start in virtualbox over purchasing a raspberry pi, however cheap it is. In the future though, when the education part of the program starts, the process should be much simpler and most likely the educators would have pre-installed an image.

you’re in reality a excellent webmaster. The website loading speed is amazing. It sort of feels that you’re doing
any distinctive trick. Moreover, The contents are masterpiece.
you’ve performed a magnificent job in this matter!