Raspberry Pi ASAP Setup Guide

A fast way to setup Raspberry Pi with Raspbian. This guide is a combination of information found on the internet and experience. Feel free to contribute via GitHub.

1. Prepare yourself

Hardware

Software

Know-how

Internet connection

Yep ... you will need it for setting-up everything, and installing software on your Pi once it is ready.

2. Installing Raspbian

Raspbian is the operating system that we are going to install on the Raspberry Pi. You can download the most recent version here and unarchive it. The archive contains only one file - a full disk image of the Raspbian system.

Formatting the SD card

Before installing Raspbian, you must prepare your SD card by formatting it to the FAT32 file system. Some may say that it is useless as we will rewrite the whole file system in the next steps, but it is recomended to do it if your SD card will be used for the first time.

Format the SD card to FAT32 file system using SDFormatter or Mac Disk Utility.

Use GParted to format your SD card to FAT32 file system.

Determining which device is my SD card

First, eject the SD card.

Then, open your Terminal, and run diskutil list to see what devices are connected to your computer. The output in the Terminal window will look similar to the following:

/dev/disk0
     #:                       TYPE NAME                    SIZE       IDENTIFIER
     0:      GUID_partition_scheme                        *256.1 GB   disk0
     1:                        EFI EFI                     209.7 MB   disk0s1
     2:                  Apple_HFS Macintosh HD            255.2 GB   disk0s2
     3:                 Apple_Boot Recovery HD             650.0 MB   disk0s3

Next - insert your SD card and run the diskutil list command again. You should get output similar to this:

/dev/disk0
     #:                       TYPE NAME                    SIZE       IDENTIFIER
     0:      GUID_partition_scheme                        *256.1 GB   disk0
     1:                        EFI EFI                     209.7 MB   disk0s1
     2:                  Apple_HFS Macintosh HD            255.2 GB   disk0s2
     3:                 Apple_Boot Recovery HD             650.0 MB   disk0s3
  /dev/disk1
     #:                       TYPE NAME                    SIZE       IDENTIFIER
     0:     FDisk_partition_scheme                        *3.9 GB     disk1
     1:                 DOS_FAT_32 EMPTY                   3.9 GB     disk1s1
  

As you see, there is a new device - /dev/disk1. On your computer, the device <ID> might be different (e.g. if you have more devices connected). Try to find the ID that was not there before you inserted the SD card.

Burning Raspbian on the SD card

In your Terminal, navigate to the directory where your Raspbian image was extracted. Mine was extracted in my home directory /Users/me/, so I need to run cd /Users/me/ in the Terminal.

Let's check that the image is there by running ls. A list of all files and sub-folders in the current directory should be printed, and you should see the extracted Raspbian image in the list. For example, mine is called 2014-01-07-wheezy-raspbian.img. This is our <image> - we will use later in code.

Now let's burn this. Unmount the SD card by running the diskutil unmount /dev/<ID> command. Of course, replace <ID> with the device ID you found in the previous step.

Wait till the disk is unmounted and then continue with the most important part - overwritting the contents of the SD card with the image.

WARNING: be very careful with this as you can accidentaly damage your computer's system drive if you enter the wrong device ID.

sudo dd if=<image> of=/dev/<ID> bs=1m

if is the input, of is the output and bs is the size of chunk of data that is being written to the output. Replace <image> by the name of your image file, and <ID> by the device ID of your SD card. For example, with the ID I found in previous step and the image file I have dowloaded, the command looks like the following : sudo dd if=2014-01-07-wheezy-raspbian.img of=/dev/disk1 bs=1m. But be careful, as - once again - your device ID and image file might be different from the one here.

Once you have double checked the ID, and image file name, execute the command. This process will take some time. So go make a coffee. And a sandwich. Don't worry if it seems to be idle. It isn't.

Wait till the Terminal has returned to it's normal state. You can tell that by being able to issue simple commands like ls and cd.

First, eject your SD card from the card reader if it is there. Open the My Computer section/directory so you can see all the storage devices connected to your computer. You should see one or more devices with letters C, D, E...

Insert your SD card into the SD card reader of your computer. A new device should appear. Windows also takes care of automatically assigning a drive letter for it. Memorize that letter because we are going to need it in the next step.

Burning the Raspbian image

On Windows you need a new program for almost every new task you want to do. It is true in this case as well. To burn the Raspbian disk image to the SD card, we have to download a program called Win32 Disk Imager. It's main purpose is just that: to burn disk image files to actual disks, USB sticks, flash drives and SD cards.

Download and install Win32 Disk Imager. Open it and choose the previously downloaded Raspbian image file (2014-01-07-wheezy-raspbian.img) as the input and the disk that represents your SD card. Press Burn and wait till the process finishes.

Linux instructions comming soon...

Don't eject the SD card yet.

3. Launching the Pi

Assigning IP addresses

In order to connect for the first time, we need to give the Pi and our laptop static IP addresses that are in the same network.

To do that, insert the SD card in the reader, and with the file browser of your operating system browse the contents of the card. There should be a file named cmdline.txt. This file contains options that are passed to the Linux kernel as it boots up. Open the file in your favorite text editor. We need to add the following at the end of the existing content:

ip=192.168.1.200::192.168.1.1:255.255.255.0:rpi:eth0:off

The cmdline.txt file should look like this now:

dwc_otg.lpm_enable=0 console=ttyAMA0,115200 kgdboc=ttyAMA0,115200 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline rootwait ip=192.168.1.200::192.168.1.1:255.255.255.0:rpi:eth0:off

Notice that there should be just a space (' ') between the existing instructions and the line just added. Save and close the file. Eject the SD card before removal.

We will be able to access the Pi through network with the IP address 192.168.1.200.

Now let's assign our laptop a static IP.

In Mac, open network preferences, choose "ethernet", and in the "Configure IPv4" field, choose "Manually".

Go to Network Settings and edit the TCP/IP part, set the IP address to "manual".

Open the network manager, select your Ethernet connection and choose "static".

Set the IP address to 192.168.1.201, netmask to 255.255.255.0 and the gateway (router) address to 192.168.1.1. You might need to disconnect from any wireless networks to make this work.

Booting up - the first time

Connect your Raspberry Pi to the computer with an ethernet cable, eject the SD card from your computer, and put it in the Pi, then plug-in the Pi's power adapter.

First, let's test that we can reach the Pi with the IP address we gave it.

For this, we will use the command ping. Run ping 192.168.1.200 in the Terminal. There should be response:

64 bytes from 192.168.1.200: icmp_seq=3 ttl=64 time=52.941 ms

If the Raspberry Pi is not reachable:

Request timeout for icmp_seq 0

For this, we will use the command ping. Run ping 192.168.1.200 in the Command Prompt. To open the Command Prompt, go to Start and type cmd in the input text field and press enter. A window with black background and white text will open. There should be response:

64 bytes from 192.168.1.200: icmp_seq=3 ttl=64 time=52.941 ms

If the Raspberry Pi is not reachable:

Request timeout for icmp_seq 0

It means that for some reason, the Pi cannot be reached. In that case, double check your network parameters. You might also have to wait as the Pi takes about 20 seconds to boot.

Now that we made sure the Pi can be reached, let's connect to it. Run ssh pi@192.168.1.200 in the Terminal. Enter raspberry once prompted for a password.

Use MobaXterm or Putty to connect to the Raspberry Pi by using IP address 192.168.1.200, username pi and password raspberry.

The SSH connection just opens a remote shell on the Raspberry Pi. Basically, you can run commands, just like in your own computer's terminal, but they are executed on the Raspberry Pi.

4. Sharing net with the Pi

Now that your Pi is connected, we can safely share our laptop's Internet connection with it. Having an Internet connection will allow you to install new software on your Pi. For this step, don't disconnect from your Pi before being told to do so.

How to share the Internet connection is different with each operating system, but the baseline is the same.

Firstly, you must configure your laptop to share its Internet connection with the port to which the Pi is connected. The way your computer achieves this is by creating a separate network which has an Internet access.

Secondly, we will connect the Pi to that network, by assigning it a static IP address. Therefore, we also need to discover what is the address of that network.

Enabling Internet Sharing

Go to System Preferences, click on Sharing. Select the Internet Sharing checkbox and choose Wi-Fi form the Share your connection from dropdown. Check the Ethernet checkbox in the To computers using field.

Now your Mac computer automatically assigns an IP address to whatever is connected to the computer's ethernet port unless a static IP address on the connected device is set.

With our Pi we will need to do just that - set a static IP address, but to do that we need to know a bit more about the network that the Mac computer is setting up to share the internet connection.

First go to system settings then Network. In order to be able to modify the options for the Ethernet port, you need it to be active. So for example, connect your Raspberry Pi to the port, and power it up. Then, open the wired tab, click on options, then IPv4 settings and change the Method to "Shared to other computers", then save the changes. That's it! Your Internet is shared.

Click the Start button. Click the Control Panel item. In the search box type adapter and then, under Network and Sharing Center, click View network connections.

Choose the connection that you want to share, right-click it and click Properties.

Click Sharing tab and select the Allow other network users to connect through this computer’s Internet connection check box. Also, select in the drop down menu the connection to which you want to give Internet, basically the port to which your Pi is connected.

When you click on "Apply", you should see a message box giving you the address of the network on which the Internet is shared. Write that address down carefully.

Finally, shutdown the Pi by running sudo shutdown -h now in the Pi's terminal.

Determining internet sharing subnet settings

Open Terminal and run ifconfig. A list of enabled network devices and their properties should fill the screen. Search for the one that has bridge in front. For some Mac's the connection is called something like en0.

bridge100: flags=8863 mtu 1500
options=23
ether 02:25:4b:3d:a7:64 
inet 192.168.2.1 netmask 0xffffff00 broadcast 192.168.2.255
...

We need to keep in mind the values that come after the inet keyword. 192.168.2.1 is the router (gateway) address and in this case it's your Mac. 0xffffff00 is the netmask that usually is expressed in numbers like this: 255.255.255.0. To make things easier, here is a table with the most common hex values for netmasks.

If our gateway address is 192.168.2.1, our static IP address can vary between 192.168.2.2 to 192.168.2.254. If your gateway address is different, leave the first 3 numbers the same and add the last one in the range from 2 to 254.

Open your terminal and run ifconfig. It will print a list of your network interfaces, including the Ethernet port on which Internet sharing is set-up. The IP address of that interface is the gateway address. This is what I get when executing the command :

  eth0      Link encap:Ethernet  HWaddr 90:f6:21:7d:7a:66  
            inet addr:10.42.0.1  Bcast:10.42.0.255  Mask:255.255.255.0
            inet6 addr: fe80::92e6:baff:fe7d:be66/64 Scope:Link
            UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
            RX packets:3107 errors:0 dropped:0 overruns:0 frame:0
            TX packets:4570 errors:0 dropped:0 overruns:0 carrier:0
            collisions:0 txqueuelen:1000 
            RX bytes:383440 (383.4 KB)  TX bytes:662042 (662.0 KB)
            Interrupt:43 Base address:0x6000 

  lo        Link encap:Local Loopback  
            inet addr:127.0.0.1  Mask:255.0.0.0
            inet6 addr: ::1/128 Scope:Host
            UP LOOPBACK RUNNING  MTU:16436  Metric:1
            RX packets:7198 errors:0 dropped:0 overruns:0 frame:0
            TX packets:7198 errors:0 dropped:0 overruns:0 carrier:0
            collisions:0 txqueuelen:0 
            RX bytes:771195 (771.1 KB)  TX bytes:771195 (771.1 KB)

  wlan0     Link encap:Ethernet  HWaddr 0a:99:d3:b5:1b:5b  
            inet addr:192.168.0.102  Bcast:192.168.0.255  Mask:255.255.255.0
            inet6 addr: fe80::225:d3ff:febd:1b5b/64 Scope:Link
            UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
            RX packets:84839 errors:0 dropped:0 overruns:0 frame:0
            TX packets:36230 errors:0 dropped:0 overruns:0 carrier:0
            collisions:0 txqueuelen:1000 
            RX bytes:45789161 (45.7 MB)  TX bytes:8132670 (8.1 MB)
        

In my case it is easy to find the right interface, as there is only 2 physical interfaces and loopback. You can forget about loopback, this is not the interface you are looking for. In my case, interfaces are named conveniently so that it is easy to find which one is the wifi (wlan0), which one is the Ethernet port (eth0).

If on the other hand you cannot figure out by their name, you can run the command sudo lshw -class network. This will give you more detailed information about all the interfaces and help you to find the id of your Wifi and Ethernet interfaces.

When you have figured out which is your ethernet port, the address you are looking for is the numbers just after inet addr: as emphasized in the example output above.

Connecting with a new static IP

Now that you have discovered the address of the network to which Internet is shared, it is time to give your Pi a new static IP. Unplug your Pi, remove the SD card and in cmdline.txt replace the previous static IP and gateway IP by the ones you discovered in the previous step.

When this is done, put back the card in the Pi, power it up, and connect to it using its new address

Once connected, try to download a web page using the command wget. For example wget www.cc4av.info . If everything goes well, you should get an answer like the following :

...lots of text...
Saving to: `index.html'

    [ <=> ] 16,025      --.-K/s   in 0.06s   

2014-03-23 09:59:37 (263 KB/s) - `index.html' saved [16025]
      

If this failed, you might need to change your Pi's DNS settings. For this edit the file etc/resolv.conf. It should contain one line nameserver 192.168.0.1. Simply replace 192.168.0.1 by 8.8.8.8. Then save the file and run sudo reboot to reboot the Pi. Then try again to download a web page.

Connecting the Pi to a router

If you've tried everything and cannot share Internet with your Pi, but you have the possibility to connect the Pi to the router, and you can connect to the admin interface of the router there is still hope.

First you will need to restore the defaults. Remove all the network configuration that you might have added in cmdlint.txt or in /etc/network/interfaces.

Then connect your Pi to the router with an Ethernet wire. If everything works fine, the router should now assign an IP address to your Pi automatically. The only issue is that you don't know what that IP address is! By chance, in most of the routers, you can see a list of connected devices and their IP addresses by connecting to the admin interface of the router. So let's do this.

First, you need to discover the IP address of your router. It is normally your own IP address, but replacing the last number by 1. For example if your IP address is 192.168.1.123, your router's IP address is 192.168.1.1. Then, open a web browser and type the IP address of the router in the address bar. You should then be prompted for a password and user name. If you don't know what they are, ask the administrator of the network. If it is your own router and you never changed the password, there should be an indication of what is the default password in your router's user manual.

Once you are connected to your router, dig in the menu, looking for the list of connected clients. Interfaces for all vendors are completely different. So the only advice I can give you is to look for a DHCP menu. When you have found the address of your Pi, you can just connect to it through SSH. The Pi should now have Internet connection.

If the local network of the router is not big (you have your and maybe one more device connected), it is possible to define a static IP address for your Pi right away. You just need to check your computer IP address, gateway and netmask and replace the last number of the IP address with a number between 2 and 255. Assign the IP address by editing the cmdline.txt file on the boot partition of your Raspberry Pi SD card.

5. Configuring the Pi further

Network settings

Ok, let's change the real network settings of the Raspberry Pi system the way it should be done normally with a Linux computer.

To do that open the /etc/network/interfaces file in an text editor like the commandline nano. Type sudo nano /etc/network/interfaces in to the TerminalPutty window after you have successfully connected to the Pi via SSH.

sudo means that you are executing a command as administrator (or "root"), so basically you have every right. Be careful though, as it also means you can potentially harm your system.

Add the following content there:

auto lo

iface lo inet loopback
iface eth0 inet static

address 192.168.2.200
netmask 255.255.255.0
gateway 192.168.2.1
auto lo

iface lo inet loopback
iface eth0 inet static

address 192.168.1.200
netmask 255.255.255.0
gateway 192.168.1.1
auto lo

iface lo inet loopback
iface eth0 inet static

address 192.168.1.200
netmask 255.255.255.0
gateway 192.168.1.1

Save the file and exit.

Now we have to remove the extra line from cmdline.txt that we added earlier. To do that, open the file in nano by running sudo nano /boot/cmdline.txt in TerminalPutty.

Delete the ip=192.168.2.200::192.168.2.1:255.255.255.0:rpi:eth0:offip=192.168.1.200::192.168.1.1:255.255.255.0:rpi:eth0:offip=192.168.1.200::192.168.1.1:255.255.255.0:rpi:eth0:off part, then save the file and exit.

Now, let's reboot our Pi by using the sudo reboot command.

If all is well, in approximately 15 to 30 seconds you will be able to connect to the Pi via SSH by using IP address 192.168.2.200192.168.1.200192.168.1.200 (if you have not changed it for something else), username pi and password raspberry.

Expanding filesystem

If your whole installation is on the SD card, the easiest way to go is to use the raspi-config tool. Run sudo raspi-config in the Raspberry Pi console to open the tool's interface.

Select the first menu item 1 Expand Filesystem and press enter. You will have to reboot the Pi after this step.

Configure keyboard

It is useful to configure the layout of your keyboard as for many following tasks you will need to type specific symbols that are usually hard to find on the keyboard if the layout is not the way it is on your main computer. Type the following in the Pi terminal to access keyboard configuration:

sudo raspi-config

Go to 4 Internationalization Options. From there choose I3 Change Keyboard Layout and hit enter.

Set up timezone

Assigning the right timezone might be also usefull, especially for debugging. There might be situations where knowing what happened at what time can be critical. So execute sudo raspi-config again and go to 4 Internationalization Options, then I2 Change Timezone.

Auto Login

To make your Pi log in automatically, run the following line in the shell:

sudo nano /etc/inittab

Comment (by using #) the line starting the terminal tty1 ("1:2345:respawn:/sbin/getty 115200 tty1"), and just bellow it add:

1:2345:respawn:/bin/login -f pi tty1 /dev/tty1 2>&1

TIP: Use keyboard arrow keys to navigate the text file. You won't see scrollbars in this kind of text editors and adjusting the cursor position with mouse won't do any good.

Close the file by pressing Ctrl + X. Nano will ask if you want to save the file: press Y to say yes. Next, Nano will ask where to save the file: just press Enter to overwrite the existing file.

Preventing the screen from going blank

If you are using your Pi through SSH without an actual keyboard connected, the screen might go to sleep after some time as your Pi does not detect any activity. To prevent that, edit the rc.local file by opening it in the nano editor: sudo nano /etc/rc.local. Press Enter to execute the command.

Add the following line just before the exit 0 line:

sh -c 'setterm -blank 0 -powersave off -powerdown 0 < /dev/console > /dev/console 2>&1'

Save the file and exit (Ctrl + X, Y and Enter). Reboot your Pi: sudo reboot, press Enter to execute the command. Now the screen will stay awake after the Pi reboots.

Startup scripts

Starting a program on boot

On the Pi go to the /home/pi directory by typing the following in the terminal:

cd /home/pi

Press Enter. We are going to create a text file (a script) that will contain instructions for the Pi - what to do when it boot's up. To create the file, type touch startup.sh in the Pi Terminal and press Enter.

Next, we are going to make it executable (launchable). Write chmod a+x startup.sh in the Pi Terminal and press Enter again.

Then open .bashrc by typing sudo nano ~/.bashrc in the Pi Terminal and pressing Enter.

Use arrow keys to navigate, add the following line at the end of the file:

~/startup.sh

Save the file by pressing Ctrl + X, Y and Enter and exit.

I think it is a good idea not to run everything when you log in via SSH next time. Edit the startup.sh file to distinguish between direct login and SSH access. Open the file in text editor:

sudo nano ~/startup.sh

Don'f forget to press Enter. Replace the contents of the file with the following:

#!/bin/bash

echo "Checking SSH connection...";
if [ -z "$SSH_CONNECTION" ]; then
	echo "No SSH connection, launching scripts.";
	# Your scripts here
else
	echo "SSH connection, nothing to be launched.";
fi

You can add your own scripts just after the # Your scripts here line. When you are done, save the file and exit by pressing Ctrl + X, Y and Enter.

Starting graphical user interface

Yes, there is a possibility to use the Raspberry Pi as a regular desktop computer with keyboard and mouse, open windows and browse web-pages as normal people do. Sometimes it might help you get your text-editing or file-moving tasks done faster.

If you are using your Pi directly - without SSH connection, with a screen, mouse and keyboard connected, you can issue the startx command in the Pi shell to start the desktop mode. It is possible to configure your Pi to enter this mode automatically by using the sudo raspi-config tool. Choose the 3 Enable Boot to Desktop/Scratch option.

6. Useful tips

Getting a GUI for Pi programs

Even when connecting through SSH, it is still possible to get graphical user interface (also called "GUI") for many programs. This is achieved by using a technique known as "X forwarding". The program runs on the Pi, but forwards its graphical output to your own laptop. On your laptop you need to have a server that interprets those commands.

On Linux, X is the normal way to display GUIs. Therefore you have nothing extra to install.

First, you should install XQuartz, which is a X11 server for mac OS.

Connect to your Pi through SSH, but add the -X option. For example ssh -X pi@192.168.2.200

There are many programs that allow you to connect through SSH with X forwarding. We recommend you to use MobaXterm. It is a good all-in-one solution for working through SSH. You can connect, save sessions, transfer files very easily, and also enable X forwarding to get GUI of programs running on the Pi.

To connect, click on "session", then go to "SSH" and type the IP address of your Pi. That's all there is to it. X forwarding should be enabled by default.

Once you have successfuly opened a connection, you can test that X forwarding works by opening a program that has a GUI on your Pi. For example, try to open "leafpad" by running leafpad in the terminal. Leafpad is a simple text editor, and if everything works fine, you should see it open on your laptop.