Installing Raspbian

These instructions will show you how to install Raspbian from scratch on your Raspberry Pi, allowing you to select only the components you want to produce your own customized installation.

Note – While these instructions will still work for the Raspberry Pi there is a newer unattended installer which will work on both the Raspberry Pi and Pi2.

I usually connect to my Raspberry Pi using a USB serial console cable which means I can use it headless without a screen, keyboard or mouse. Of course if you prefer to connect a monitor, keyboard and (optionally) mouse to your Raspberry Pi these instructions will work just as well if you skip the sections where I modify /boot/cmdline.txt and /etc/inittab to configure /dev/ttyAMA0 as the console device.

Note on Partitioning

Since the flash RAM on an SD card will only allow a finite number of disk writes I do not use the default partitioning scheme and use just three partitions, leaving out the swap partition. The process below will create a 64 MB FAT32 boot partition containing the configuration files and firmware, and a 2GB linux EXT4 partition for the operating system, which I find is sufficiently large for most uses, including MATE or XFCE which I use from a remote desktop. This leaves the rest of the space for use as extended linux partition for user data.

Partitioning the SD card

To repartition the SD card you need to be running as root.

$ su


$ sudo -i

Insert the SD card in a working linux system using an SD card reader, if there are any existing partitions on the SD card delete them. When you have done that the partition table should be empty as shown below.

Warning – The device name assigned you your SD card may not be the same on your system as it is on mine, if you repartition the wrong device using fdisk you may lose your data – permanently!

# fdisk /dev/sda

Command (m for help): p

Disk /dev/sda: 7948 MB, 7948206080 bytes
245 heads, 62 sectors/track, 1021 cylinders
Units = cylinders of 15190 * 512 = 7777280 bytes
Disk identifier: 0x000ee283

   Device Boot      Start         End      Blocks   Id  System

I find is easier to work directly with the number of sectors on the disk when trying to create disk partitions of a particular size.

Command (m for help): u
Changing display/entry units to sectors

First we need to create the FAT32 boot partition.

Note – This must start at sector 2048 or it won’t work.

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
Partition number (1-4): 1
First sector (62-15523839, default 62): 2048
Last sector 62-15523839, default 15523839): 128000

Command (m for help): t
Partition number (1-4): 1
Hex code (type L to list codes): b
Changed system type of partition 1 to b (W95 FAT32)

Command (m for help): a
Partition number (1-4): 1

Having created the FAT32 boot partition, we can then create a linux partition to hold the operating system later.

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
Partition number (1-4): 2
First sector (128001-15523839, default 62): 128001
Last sector  (128001-15523839, default 15523839): 4096000

Command (m for help): t
Partition number (1-4): 2
Hex code (type L to list codes): 83

The result should look like this:

Command (m for help): p

Disk /dev/sda: 7948 MB, 7948206080 bytes
245 heads, 62 sectors/track, 1021 cylinders, total 15523840 sectors
Units = sectors of 1 * 512 = 512 bytes
Disk identifier: 0x000ee283

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048      128000       63969+   b  W95 FAT32
/dev/sda2          128001     4096000     1984000   83  Linux

Note - I think you can ignore any messages warning you that the partition does not end on cylinder boundary. I have not had any major issues that I've noticed anyway.

Then commit the changes to disk.

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

Format the boot partition

If necessary install the tools needed to format a FAT32 volume.

# apt-get install dosfstools
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following NEW packages will be installed:
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 85.1kB of archives.
Setting up dosfstools (3.0.1-1) ...

Then format the boot partition

# mkfs -t vfat /dev/sda1

Configure the installer

First download the installer from here and save it in your current folder. Then create a temporary folder to hold the boot loader and other files before copying them to the boot partition.

# mkdir rpi_installer_08-19-12
# cd rpi_installer_08-19-12
# unzip ../
# mount /dev/sda1 /mnt
# mv * /mnt

To be able to use the serial console to install Raspbian you have to make some changes to the boot configuration highlighted below. On my system the USB serial console defaults to 9600 baud so I need to modify the baud rate as well as the console device. If you don't know what baud rate your machine is using leave it as the default and see if it works first.

# vi /mnt/cmdline.txt

dwc_otg.lpm_enable=0 console=ttyAMA0,9600 kgdboc=ttyAMA0,9600 
console=ttyAMA0,9600 root=/dev/mmcblk0p2 rootfstype=ext4 rootwait

Note - The above text should all be on one line.

# umount /mnt
# cd ..
# rmdir rpi_installer_08-19-12

Remove the SD card and insert it into your Raspberry Pi, then connect the serial console cable and power to start the install process.

Installing Raspbian

The following screen shots will walk you through the process of installing Raspbian from scratch using the installer, there are a few differences from the default Debian installation, mostly to do with the installation of the kernel but not many.

The first step is to select the default language to be used by the installer - there is not much of an option here so just select 'English'.
_Screenshot-raspbian-install-01Then select your country - this is use to select the default locale setting later on.
_Screenshot-raspbian-install-02As this is a network installer the next step is to try to detect the network, before configuring the interface using DHCP.
Note - You cannot install Raspbian usign a wireless network adapter using these instructions.
_Screenshot-raspbian-install-03_Screenshot-raspbian-install-04Pick a host name for your Raspberry Pi, the default is 'debian', but you should change this to a name that is unique to your own network but what you choose is up to you (you can change it later on after the installation has finished if you need to).

I've chosen the stunningly original 'pi' - what else!
_Screenshot-raspbian-install-06Unless your machine will actually be connecting to the internet directly, you can use '' as the domain name.
_Screenshot-raspbian-install-07The next step is to tell the installer where to find the raspbian package repository, which has to be configured manually.
_Screenshot-raspbian-install-08I'm using '' but which mirror site you use will depend on your location, for the fastest download speeds you should pick the mirror site closest to you.
_Screenshot-raspbian-install-09The directory is depends on the distribution, in this case 'raspbian'.
_Screenshot-raspbian-install-10Unless you are behind a proxy server you can leave the next entry blank, and if you are behind a proxy server you will know what to use!

_Screenshot-raspbian-install-11Do not worry about the following warning message, just select 'Yes' to continue.
_Screenshot-raspbian-install-13The installer will down load some additional components.
_Screenshot-raspbian-install-14Next you need to select a good strong password for the 'root' account and then create a default user account - the one you will normally use.
_Screenshot-raspbian-install-15_Screenshot-raspbian-install-16_Screenshot-raspbian-install-17_Screenshot-raspbian-install-18_Screenshot-raspbian-install-19The installer will then attempt to detect which disks (including SD cards) you have available, and then take you through the process of partitioning them. You can just select the default partition sizes if you want, however because usign an SD card for swap space is not a good idea due to the limited number of writes that a flash RAM device can be used for, I prefer to undo the changes made by the installer and use the partition sizes we created earlier.
To use the defaults just select 'Finish partitioning and write changes to disk', to use the partitions we have already created select 'Undo changes to partitions'.
_Screenshot-raspbian-install-23Select the first partition and tell the partitioner we want to use it as a FAT32 partition with a mount point of '/rpiboot'.
_Screenshot-raspbian-install-24_Screenshot-raspbian-install-25_Screenshot-raspbian-install-26When you have done that select 'Done setting up partition'.
_Screenshot-raspbian-install-27Repeat the process to set up the second partition as a ext3 or ext4 partition for the operating system.
_Screenshot-raspbian-install-28_Screenshot-raspbian-install-29_Screenshot-raspbian-install-30Check that when you are done the partitions look like this.
_Screenshot-raspbian-install-31When you have finished setting up the partitions select 'Finish partitioning and write changes to disk'.
_Screenshot-raspbian-install-32You can ignore the error message and select 'No' to continue.
_Screenshot-raspbian-install-33Confirm that you want to write the changes to disk by selection 'Yes'.
_Screenshot-raspbian-install-34The partitions will then be formatted, and the installation will continue.
_Screenshot-raspbian-install-35_Screenshot-raspbian-install-37Again we can ignore the following warning.
_Screenshot-raspbian-install-38_Screenshot-raspbian-install-39_Screenshot-raspbian-install-40_Screenshot-raspbian-install-41For a minimal installation I prefer to deselect the default software options and install the packages I want manually later, however you can just accept the defaults.

Note - Do NOT select the desktop environment.
_Screenshot-raspbian-install-43The selected packages will then downloaded and installed, this can take a while. Even for a minimal installation will take 15-20 minutes.
_Screenshot-raspbian-install-45You can ignore the following warning.
_Screenshot-raspbian-install-46Allow the installation to finish, and when the system starts to reboot power it off and remove the SD card.
_Screenshot-raspbian-install-48To finish configuring the installation we need to repeat the changes we made earlier to get the newly installed system to use the serial port as the default console, and the second partition as the root, (since they will have been overwritten by the installer).

# umount /dev/sda1
# mount /dev/sda1 /mnt
# vi /mnt/cmdline.txt

dwc_otg.lpm_enable=0 console=ttyAMA0,9600 kgdboc=ttyAMA0,9600
console=ttyAMA0,9600 root=/dev/mmcblk0p2 rootfstype=ext4 rootwait

Note - As before the above text should all be on one line. Remember that it will only be necessary to change the root device if like me you didn't use the default partitioning scheme.

Finally we need to modify inittab.

# vi /mnt/etc/inittab

# Example how to put a getty on a serial line (for a terminal)
T0:23:respawn:/sbin/getty -L ttyAMA0 9600 vt100
#T0:23:respawn:/sbin/getty -L ttyS0 9600 vt100 

Then unmount the SD card.

# umount /mnt

Reinsert the SC card into your Rasbperry Pi and power on - if all went well then it should boot (using the serial console if you configured it that way).

Raspbian GNU/Linux 7 pi ttyAMA0

pi login: 

If you plan on using the keyboard and mouse (or accessing your Pi remotely X Windows) or VNC) the next step is probably to install either MATE or XFCE.

Raspberry Pi is a trademark of the Raspberry Pi Foundation

This entry was posted in Linux, Raspbian and tagged . Bookmark the permalink.

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s