Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save dialupdev/8f74c76a641310462ab3 to your computer and use it in GitHub Desktop.
Save dialupdev/8f74c76a641310462ab3 to your computer and use it in GitHub Desktop.
Setting up headless Raspberry Pi OS on macOS

I recommend starting with Stretch for development projects. Stretch Lite doesn't include the build tools that most projects require. It's easier to just start with them in place rather than spend a lot of time installing things.

Download the latest Raspbian image from https://www.raspberrypi.org/downloads/raspbian/.

Insert your SD card and determine where it's mounted.

diskutil list

Find the one that matches your SD card and unmount it.

diskutil unmountDisk [path to disk, e.g. "/dev/disk2"]

Burn the image to your SD card. This can take several minutes.

sudo dd bs=1m if=[path to img file, e.g. "2017-08-16-raspbian-stretch.img"] of=[path to rdisk, e.g. "/dev/rdisk2"] conv=sync

Ensure the disk is mounted again, then enable SSH.

touch /Volumes/boot/ssh

If using Edimax EW-7811Un (rtl8188cus chipset) OR built-in WiFi on Raspberry Pi 3, add your Wifi configuration. Create the following file:

nano /Volumes/boot/wpa_supplicant.conf

And add this to it:

country=US
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1

network={
	ssid="[SSID]"
	psk="[password]"
}

Eject your SD card.

diskutil eject [path to disk]

Connect the Raspberry Pi to your Mac via an ethernet cable and turn on internet sharing to ethernet.

Copy your public SSH key the the Pi.

cat ~/.ssh/id_rsa.pub | ssh [email protected] "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"
# From https://www.digitalocean.com/community/tutorials/how-to-configure-ssh-key-based-authentication-on-a-linux-server

Then set up basic configuration.

ssh [email protected]

sudo raspi-config
Modified from https://www.hifiberry.com/guides/configuring-linux-3-18-x/.
# Configure device tree overlay file
# To do this, you must edit /boot/config.txt and add the following line
dtoverlay=hifiberry-dac
#Configure ALSA
#Create /etc/asound.conf with the following content:
pcm.softvol {
type softvol
slave.pcm "plughw:1"
control {
name "Master"
card 0
}
}
# Reboot
# Make sure device shows up:
aplay -l
# Look for "card { id }: sndrpihifiberry"
# Initialize mixer
speaker-test -D softvol -c 2 -t wav

Set shairport-sync to use the hifiberry-dac

sudo nano /etc/shairport-sync.conf Change this:

alsa = { output_device = "hw:1"; (or whatever number card "sndrpihifiberry" is above) interpolation = "soxr"; }

If audio is underflowing, set audio_backend_buffer_desired_length to 22050

Change any other settings you want (name, metadata on, etc.)

Restart Shairport-sync

sudo service shairport-sync restart

# In /usr/lib/, add one of these libraries (Use the armv6 ones for Rpi1, armv7 for Rpi2/3) https://github.com/sashahilton00/spotify-connect-resources/tree/master/libs
# Run this before Install section of spotify-connect-web
```sh
sudo apt-get install python-pip python-gevent python-alsaaudio
```
# Add this to /etc/shairport-sync.conf
sessioncontrol = {
run_this_before_play_begins = "/home/pi/stop_spotify_connect.sh";
run_this_after_play_ends = "/home/pi/start_spotify_connect.sh";
wait_for_completion = "yes";
};
```sh
chmod +x /home/pi/start_spotify_connect.sh
chmod +x /home/pi/stop_spotify_connect.sh
```
#!/bin/bash
LD_LIBRARY_PATH=/home/pi/spotify-connect-web python /home/pi/spotify-connect-web/main.py -u [username] -p [password] -k /home/pi/spotify_appkey.key -n [name] -b 320 --playback_device softvol --mixer Master &
#!/bin/bash
pgrep -f spotify-connect-web/main.py | xargs kill -9
@artburkart
Copy link

Awesome, thanks for sharing :)

@dialupdev
Copy link
Author

Glad you found it useful!

@johanndiedrick
Copy link

Thank you so much for this!

@spicymaya
Copy link

Really great. Thank you! Made the set up so quick and easy!

@theoctober19th
Copy link

I followed every steps, but cannot ssh to [email protected] because it asks for password. Tried raspberry but didn't work.

@dialupdev
Copy link
Author

Thanks for pointing this out @theoctober19th. In the latest version of Raspberry Pi OS (2022-04-04), the "pi" user has been removed. I've just updated the section about writing the image with instructions on how to create the "pi" user.

@arvindpant
Copy link

Thanks @celeryclub for making setup for easy:)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment