Prepare a Raspberry Pi from scratch

Our step-by-step guide to prepare a lean yet versatile SD card image for the Pi.

Prepare a reelyActive SD card image for Raspberry Pi

The TL;DR (Too Long; Didn't Read)

Learn how we at reelyActive prepare a SD card image for a Raspberry Pi.

Our original Pi tutorial is/was one of our most popular pages. This tutorial is a friendlier reference on each step of the prep.


What will this accomplish?
A clean installation of the latest Raspbian Lite build with everything to support reelyActive's open source software suite.
Is there an easier way?
Yes, you can simply flash a ready-made disk image to a SD card.
So why would I read this?
Perhaps you're curious about the process. Perhaps you'll find one or more steps useful for a completely different project.

Flashing Raspbian Lite to a SD Card   Step 1 of 5

Download the latest version of Raspbian Lite and flash it to an SD card with Etcher.


Why Raspbian?
As the default operating system for the Pi, it is accessible to the broadest audience.
Why Lite?
It's good enough and keeps the disk image small.
Flashing Raspbian to SD card using Etcher

Prerequisites

We'll flash the SD card with Etcher, a friendly open source tool that you can download here.

Download and unzip the latest Raspbian Lite Part 1

From the computer with which you intend to flash the SD card:

  1. Browse to www.raspberrypi.org/downloads/raspbian/
  2. Scroll down to find the Lite version
  3. Download the ZIP or Torrent, as you prefer
  4. Unzip the downloaded file

You should now have a file entitled 20xx-xx-xx-raspbian-stretch-lite.img on your computer . Let's flash!

Flash the Raspbian Lite image to the SD card Part 2

We recommend using micro SD cards from reputable vendors of at least 4GB size. From the computer with which you intend to flash the SD card:

  1. Insert the SD card (via adapter if required) and observe that it is recognised/mounted by the operating system
  2. Run Etcher (see Prerequisites above) by double-clicking its executable AppImage or desktop icon
  3. In the Etcher interface, select the image file
  4. In the Etcher interface, confirm that the SD card is automatically selected, or select manually
  5. In the Etcher interface, click Flash!

Within a few minutes the SD card should be flashed and verified. Unmount (if necessary) and eject the card .

Pre-configuring Raspbian Lite for the Pi   Step 2 of 5

Configure Raspbian before the first boot on the Pi, ensuring a painless initial connection.


Why more pre-config?
Unless you have a monitor and keyboard for the Pi, good luck with the first connection otherwise!
Why so difficult to connect?
By default SSH is disabled (for security) so even if you can reach the Pi over a network, it will refuse to connect!
Predictable network names and eth0 fallback

Re-insert the SD card into the computer . The computer should now mount two partitions: boot and rootfs. We'll make minor changes to both partitions as follows.

Enable SSH and predictable network interface names Part 1

Open a terminal and browse to the root of the boot partition, then:

  1. Enable SSH with the command sudo touch ./ssh
  2. Open the file cmdline.txt and append the following to the first line: net.ifnames=0

This will allow us to SSH into the Pi on the first boot, and will use predictable network interface names (ex: eth0, wlan0) which we'll need for Part 2.

Fallback to static Ethernet IP address Part 2

Open a terminal and browse to the root of the rootfs partition. Edit the /etc/dhcpcd.conf file to include the following (simply append or uncomment and edit):

# Define static profile
profile static_eth0
static ip_address=10.0.50.100/24
static routers=10.0.50.1
static domain_name_servers=10.0.50.1

# Fallback to static profile on eth0
interface eth0
fallback static_eth0
          

This will ensure that the Pi boots with the static Ethernet address of 10.0.50.100 in the absence of DHCP, as will be the case, and will prove helpful on any occasion where it is necessary to directly connect to the Pi over Ethernet, as in the following step.

Unmount both partitions and eject the SD card which is now ready to find its way into the Pi.

First boot and raspi-config   Step 3 of 5

Boot the Pi for the first time and SSH in via Ethernet to run raspi-config.


Why connect over Ethernet?
Because we know the Pi's static IP address, making this the easiest way to connect at first.
Why use SSH?
Unless you have a display and keyboard connected to the Pi, the only way to remotely execute commands is over SSH!
raspi-config

The micro SD card should now be inserted into the Raspberry Pi .

Boot the Pi and connect via SSH over Ethernet Part 1

With the micro SD card inserted into the Pi , complete the following:

  1. Apply power to the Raspberry Pi
  2. Connect the computer with the Pi using a network (Ethernet) cable
  3. Set the computer to use the static Ethernet IP address 10.0.50.101
  4. Open a terminal and SSH into the Pi with the command ssh pi@10.0.50.100 and, when prompted, enter the default password raspberry

Now that you're logged in to the Pi, it is possible to execute commands such as raspi-config.

Complete the basic configuration with the raspi-config tool Part 2

From the command prompt on the Pi , enter the command sudo raspi-config which will open a text-based menu. Update the configuration as per the following table where the settings are indicated in [ ].

# Option Action(s)
1 Change User Password Set to berryinsecure
2 Network Options N2 Wi-fi (adjust the defaults as per your local WiFi network)
  • Set country [CA]
  • Please enter SSID [reelyActive]
  • Please enter passphrase [owl-in-one]
4 Localisation Options I2 Change Timezone [America | Montreal]
5 Interfacing Options P1 Serial
  • Would you like a login shell to be accessible over serial? [No]
  • Would you like the serial port hardware to be enabled? [Yes]
7 Advanced Options A1 Expand Filesystem [Select]

Upon completing the above, select <Finish> and select <Yes> when prompted to reboot. If you entered valid WiFi settings, the Pi should connect to this network automatically, which will be required for the next step.

Second boot and DHCP server config   Step 4 of 5

SSH into the Pi over WiFi and configure its Ethernet DHCP server to facilitate future direct connections.


Why reconnect over WiFi?
The Pi will require an Internet connection in subsequent steps. A local area network (LAN) connection, such as over WiFi, affords both a local SSH login and Internet access for the Pi.
Why a DHCP server?
Configuring a static IP address on a computer can be a pain (remember Step 3?). A DHCP server enables the Pi to automatically assign an IP address to any computer with which it is directly connected.
Raspberry Pi DHCP either way

The Pi should have rebooted from the previous step and connected to both the local WiFi network and again to the computer connected directly via Ethernet .

SSH to the Pi over WiFi after determining its IP address Part 1

With the Pi still connected to the computer via Ethernet , and from the same terminal window as before:

  1. SSH back into the Pi with the command ssh pi@10.0.50.100, using the new password berryinsecure
  2. Determine the Pi's WiFi IP address with the command ifconfig. It will be the inet value under wlan0.
  3. Exit the current SSH session with the command exit
  4. SSH into the Pi over WiFi with the command ssh pi@xxx.xxx.xxx.xxx, replacing the x values with the Pi's WiFi IP address.
  5. Disconnect the network cable between the Pi and the computer.

The computer is now connected to the Pi over WiFi .

Be sure to disconnect the network cable between the two, else they may ignore their Internet connection over WiFi, instead selecting Ethernet as their default gateway.

Install and configure the DHCP server using dnsmasq Part 2

From the same terminal connected to the Pi via SSH over WiFi :

  1. Install dnsmasq with the command sudo apt-get install dnsmasq
  2. Save a copy of the default configuration with the command sudo mv /etc/dnsmasq.conf /etc/dnsmasq.default
  3. Create the file /etc/dnsmasq.conf, paste in the following two lines and save:
    interface=eth0
    dhcp-range=10.0.50.2,10.0.50.99,255.255.255.0,12h
  4. Restart the service with the command sudo service dnsmasq restart

The Pi will now behave as follows on Ethernet connection:

External DHCP? Behaviour of the Raspberry Pi
Yes The Pi will accept the DHCP-assigned IP address
No The Pi will fallback to static IP address 10.0.50.100 and it will assign IP addresses in the range 10.0.50.2-99 to any connected devices

The core configuration of the Raspberry Pi is now complete. On to the software!

Install Node.js, npm and git   Step 5 of 5

Install all the prerequisites for the reelyActive open source software.


Why install Node.js?
The reelyActive open source software runs on Node.js.
Why install git?
Because nothing says "I ♥ Open Source Software" quite like git.
Node.js, npm and git

Prerequisites

Visit nodejs.org and note the latest LTS version number available (ex: 10.15.3). Substitute this version number in all of the commands below.

Download and install Node.js and npm Part 1

From the same terminal connected to the Pi via SSH over WiFi :

  1. Change to the home folder with the command cd
  2. Download the Node.js tarball with the command wget https://nodejs.org/dist/v10.15.3/node-v10.15.3-linux-armv7l.tar.xz
  3. Unpack with the command tar -xf node-v10.15.3-linux-armv7l.tar.xz
  4. Move the unpacked folder to /usr/local/node with the command sudo mv node-v10.15.3-linux-armv7l /usr/local/node
  5. Remove the original download with the command rm node-v10.15.3-linux-armv7l.tar.xz
  6. Change to the /usr/local/bin folder with the command cd /usr/local/bin
  7. Create here a symbolic link to node with the command sudo ln -s /usr/local/node/bin/node node
  8. Create here a symbolic link to npm with the command sudo ln -s /usr/local/node/bin/npm npm

Confirm that node and npm are successfully installed with the commands node --version and npm --version respectively.

Update/upgrade all packages and install git Part 2

Since the latest release of Raspbian, there are inevitably newer versions of some of the packages available. From the same terminal connected to the Pi via SSH over WiFi :

  1. Update the list of available packages and their versions with the command sudo apt-get update
  2. Upgrade existing packages to the latest version with the command sudo apt-get upgrade
  3. Install git with the command sudo apt-get install git

All software packages are now installed and at the latest version. The Pi is prepared for installation of the reelyActive open source software suite, which is the subject of the next tutorial:

Next tutorial

Do you like this document template?

It's based on Bootstrap 4   MIT and Font Awesome 5 (Free)   MIT. Our Web Style Guide   MIT has all the details. Sharing and feedback are encouraged!

  reelyActive's Web Style Guide

Where to next?

Continue exploring our open architecture and all its applications.