Get a free year on Tuts+ this month when you purchase a Siteground hosting plan from $3.95/mo
The Raspberry Pi is a versatile little computer that provides the perfect sandbox to start creating some fun and interesting projects. One popular project is as an AirPlay receiver, allowing us to stream audio from an iOS device or computer using iTunes to our Raspberry Pi that's connected to a set of speakers.
In this tutorial, I'll show you how to set up a Raspberry Pi to be used as an AirPlay receiver so you can stream audio from any iOS device, iTunes or compatible AirPlay software such as AirFoil.
We'll need a few bits and pieces before we get started, all of which I've listed below:
- Raspberry Pi
- SD Card that's 4GB or larger
- Micro USB cable and power source (or Micro USB power adapter)
- HDMI cable and compatible display
- USB keyboard
- Some speakers
- Ethernet cable
I'm going to be using the Raspbian operating system. Raspbian is a Linux distribution that has been tweaked specifically for the Raspberry Pi. It's lightweight and easy to use, with all of the built-in hardware already configured with drivers and ready to go.
An AirPlay receiver does not necessarily need to be wireless and the Raspberry Pi I'll be configuring will make use of the Ethernet port and be connected via a cable to my home's network.
Flashing Your SD Card
Before we can set up our Raspberry Pi, we must download the Raspbian operating system and flash it to a suitable SD card. We've previously published a step-by-step guide on exactly this in our previous tutorial "How to Flash an SD Card for Raspberry Pi". Check it out and follow the instructions to flash your SD card and return here once completed.
Booting Your Raspberry Pi
Now that your SD card has been flashed, it's time to start up your Raspberry Pi.
- Connect your Raspberry Pi to your chosen display using a HDMI cable.
- Connect your USB keyboard.
- Insert your SD card.
- Power up the Raspberry Pi using the Micro USB port.
Tip: You can use a HDMI-DVI cable if your preferred display does not have an HDMI connection.
Your Raspberry Pi will begin the boot sequence and you'll be presented with the Raspberry Pi Configuration Tool. There are a few options we need to alter here before we can continue.
By default, a flashed SD card will only occupy up to about 2GB of space, with most of it being used. Even flashing an 8GB card, only 2GB will show up. To correct this, we must expand the filesystem to fit the entire card.
Doing this is very simple and is actually the first option within the configuration tool. Simply press enter on the first option and it will automatically expand the filesystem to fill the entire card. For an 8GB card, this provides an additional 6GB of free space.
Change User Password
This is an optional process but is one that is still worth performing so that your Raspberry Pi can remain secure. The default user is "pi" and the password is "raspberry" so changing it, even for something as inconsequential as this, is still recommended. You can select the second option using the cursor keys and pressing enter, where you'll then be prompted to enter a new password and confirm it.
Those are all the settings that we need to configure and you can go ahead and select Finish. This will cause your Raspberry Pi to restart.
Once it's restarted, you'll be presented with a simple command line asking you to log in.
Enter the username "pi" with the password you recently set and you'll then be logged in and ready to start.
Where Are All The Icons?
All of the following instructions will actually be performed within a command-line interface and as such, don't require any form of desktop environment. A graphical interface requires a fair amount of system resources so if it can be avoided, it makes the resources it would have used available for other processes.
As our Raspberry Pi is going to be nothing more than a receiver for network information, it doesn't require a graphical interface. We do, however, need a display just so we can see what we're entering into it.
Just like with a normal computer, one of the first things to do is check for any software updates. To do this, enter the following into the command line:
$ sudo apt-get update && apt-get upgrade
We're actually running two commands here, one after the other. The first,
apt-get update, checks to see if there are any new packages (software) that is available, in the same way we would run Software Update on a Mac or Windows Update on a Windows PC to see if there are any new updates.
The second command,
apt-get upgrade, downloads and installs the software updates that are available. Again, if we were using a Mac running Software Update, this is when we actually click "Download & Install".
This process can take a little time depending on the updates available and the speed of your internet connection so be patient while the process is running.
Now that our Raspberry Pi is up to date, we need to install some additional software that isn't included as standard. The software we're installing is:
That seems like an awful lot of software! Most of these packages are actually related in some way and you'll notice the same term cropping up in a few of the filenames. Explaining the purpose of all these packages would go far beyond the requirements of this tutorial (and likely be of little interest to some readers) but, sufficed to say, you require all of these in order to continue.
Tip: If you'd like to know what you're installing, all of these are public packages and more information can be found on the Debian Package Wiki
To install these, we can do so in one (giant) command. Deep breath!
$ sudo apt-get install avahi-utils build-essential chkconfig git libao-dev libavahi-client-dev libcrypt-openssl-rsa-perl libio-socket-inet6-perl libssl-dev libwww-perl pkg-config
Enter your password, if prompted, and Raspbian will get to work and download all of these files for you. Just like the updates, this can take some time to do.
Phew! If you've made it this far then congratulations are in order. The next step is to download a utility called ShairPort, this will enable the core AirPlay functionality that is needed and will allow us to stream audio to our Raspberry Pi.
At the command prompt, let's make sure we're where we need to be. Simply type
cd and we'll be placed back into our home directory. It's unlikely we've moved from there but, just in case you've been exploring your Raspberry Pi, it will make sure we know exactly where we are.
We're going to use our newly-installed
git command to download ShairPort from GitHub.
$ git clone -b 1.0-dev git://github.com/abrasive/shairport.git
The command tells Raspbian to download the ShairPort utility from GitHub. Again, going into detail about how GitHub works would go way beyond the scope of this tutorial.
We're now going to "make" our software which will install it for us to use.
First of all, let's navigate to the shairport folder we just created by downloading it.
$ cd shairport
Next, we're going to prepare it for installation and then install it. We'll string this command together into one so that we're not needing to enter it separately.
$ sudo ./configure && sudo make && sudo make install
After a few minutes, ShairPort is now installed and ready to go.
To start ShairPort, enter the following command:
$ shairport -a 'Raspberry JAM'
This starts ShairPort and names our new AirPlay device as, rather fittingly Raspberry JAM. Our Raspberry Pi will then state that it's "listening for connections" which means it's ready to go.
On my iPad, I've opened the AirPlay menu and can now see our new speaker up and running. To test it out, simply start streaming some audio to it and plug in your speakers or headphones.
You should, by now, have some audio playing through your Raspberry Pi but its probably a little on the quiet side and increasing speaker volume merely distorts it. The audio output of the Raspberry Pi isn't the best quality and, by default, the volume is set very low.
We can tweak this by using the command alsamixer. To run it, we first need to stop ShairPort. Pressing Ctrl-C will stop ShairPort running completely and allow us to enter the following commands:
Using the up and down cursor keys, adjust the level to be around 75-80, any more and you risk distorting the audio considerably.
Press Esc to save the changes and quit, then re-run the previous command to start ShairPort. To save you a little time, simply use the up and down cursor key again on your keyboard and you can scroll though your previous commands.
Once you've restarted it, use your iOS device (or whatever you're streaming from) to stream audio back to the Raspberry Pi and the audio should be much louder without having to turn the speakers up.
There are some drawbacks with the setup so far. There's no Wi-Fi connectivity so our Raspberry Pi, and therefore our speakers, are under a physical restriction. Additionally, the audio quality from the Raspberry Pi's built-in audio is mediocre at best. These are limitations that have relatively simple workarounds and they'll be ones that we look at as our tutorials continue.
No matter what, by the end of this tutorial you'll have been able to configure a Raspberry Pi from scratch as an AirPlay receiver, letting you stream audio from any compatible device or software, just as you could with an AirPort Express or even an Apple TV.