andersonmat
Archives About

Raspbian Setup on macOS

You’ve just got everyone’s favorite micro hobby computer and you’re itching to get started. While the Raspberry PI is nearly plug-and-play (especially if you purchased a pre-imaged SD card), there are a few things we’ll need to do in order to get started.

Raspbian

Perhaps most important, the Raspberry Pi needs an operating system. The preferred choice for many is Raspbian, a Debian-based distribution of Linux that’s maintained by the Raspberry Pi Foundation and specifically optimized for the Raspberry Pi. Raspbian is a good all-around choice for non-specialty projects.

To start we need to download a copy of Raspbian from the Raspberry Pi Foundation website. The Raspberry Pi Foundation offers a few variants to choose from. I’m partial to minimal installations so I will be providing commands below that download and use the “Lite” variant.

curl -L "https://downloads.raspberrypi.org/raspbian_lite_latest" -o raspbian.zip

This will download the most recently updated Raspbian Lite image from the Raspberry Pi Foundation website as raspbian.zip. Because they distribute images zipped, we will have to extract the image before moving forward.

unzip raspbian.zip

After extraction we will see that we have a single .img file alongside our raspbian.zip. At the time of writing the image was called 2018-11-13-raspbian-stretch-lite.img (presumably 2018/11/13 was when it was built). There’s the possibility your image is named differently, that is okay, just ensure that you use your image’s file name in any commands below.

SD Card

Now that we have a copy of Raspbian, our next task is to create a bootable SD card for the Raspberry Pi. After connecting your SD card to your Mac and before we can transfer the image itself to it, we need to do some exploratory work to make sure we run the right commands.

diskutil list

This command will give us a list of all available storage devices and what macOS is using to identify them. I’ve truncated the output to omit the macOS operating system drive, but the output for my 32GB SD card looks like this:

/dev/disk2 (external, physical):
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:     FDisk_partition_scheme                        *32.0 GB    disk2
   1:             Windows_FAT_32 NO NAME                 32.0 GB    disk2s1

The output of this command tells us that macOS is referring to the SD card as /dev/disk2. It is entirely possible that macOS is calling your drive something different – pay attention to this or you may permanently lose data on a drive you are not intending to overwrite!

Now that we’ve identified our target drive, we’ll go ahead and unmount it so we can operate on it. By unmounting but not ejecting the disk, the macOS will allow you to run commands on the drive that would otherwise be prevented.

diskutil unmountDisk /dev/disk2

Finally, we’ll copy the contents of the image file directly to the disk using the dd command. Note that the r preceding my disk2 is not a mistake. Due to how macOS exposes the drives, you must prefix with an r to have raw access to the device itself. Ensure both the image and disk name are correct before running this command.

sudo dd if=2018-11-13-raspbian-stretch-lite.img of=/dev/rdisk2 bs=4096

This process will likely take a minute or two. Go make yourself a cup of coffee, tea, etc. in the meantime.

When complete you should now see a “boot” volume present. If you just wanted a bootable SD card, congratulations, your journey ends here.

Niceties

There are a couple other niceties that I like to do before booting for the first time, this includes pre-configuring WiFi and enabling SSH access to the Raspberry Pi.

For WiFi, we’ll create a file called wpa_supplicant.conf in the mounted boot volume which will contain the SSID and PSK.

ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
network={
    ssid="YOUR SSID HERE"
    psk="YOUR WIFI PASSWORD HERE"
    key_mgmt=WPA-PSK
}

In order to enable SSH pre-first boot, we just have to create a file called ssh in the boot volume.

touch /Volumes/boot/ssh

That’s it. Safely eject your SD card, insert into the Raspberry Pi and you are good to go. You should see it join your WiFi network shortly and be able to SSH into it with the default pi/raspberry username and password.

Written on Dec 6, 2018.