How To: Prep A Raspberry Pi 2 For Compiling Swift
Recently I set up a couple of Raspberry Pi 2s with Raspbian Jessie Lite to compile Swift. While I happened to be compiling Swift, these are basically the same steps I would follow for any project that requires setting up a Pi to run as a headless1 server.
Time Required: About 20 minutes if you’ve done it before:-)
Step 1 (On a PC): Download and Burn Raspbian Jessie Lite
- Download here.
- Image burning instructions for Linux, Mac OS, and Windows are here. Writing the actual 1.46 GB uncompressed image takes about 62 seconds on my system.
Step 2: SSH to the Pi
- Network scanning tools for iOS like Scany and Fing are an easy way to find your Pi’s IPv4 address if you don’t want to bother checking on your router. Other options for finding your Pi’s IPv4 address.
- Prompt 2 is an excellent SSH client for iOS.
- The default username is ‘pi’ with password ‘raspberry’.
Step 3: Initial Setup
Run the included setup utility with sudo raspi-config
. A few things I usually do:
- Expand the filesystem to fill the microSD card.
- Change the user password. Choose something very long and very random.2
- Change the hostname (under Advanced Options)
This is also a good time to take care of some optional setup tasks:
- Give yourself some generous swap space: For compiling Swift, I usually set
CONF_SWAPSIZE
to 2048 in /etc/dphys-swapfile .3 - Allocate a little less memory to the GPU: Since we’re running this as a headless system, we can reduce the amount of memory set aside for the GPU to the minimum by adding
gpu_mem=16
to/boot/config.txt
(documentation).4 - Disable HDMI to reduce power usage just a bit: Add
/usr/bin/tvservice -o
to/etc/rc.local
.
I don’t typically reboot at this point, as I want to avoid ever having to enter that long random password by setting up SSH key-based authentication first.
Step 4: Setup SSH Key-Based Authentication
- Add the public key(s) of the systems you want to login from to
~/.ssh/authorized_keys
. This is both simpler and easier than using a password every time. Digital Ocean has a good guide to setting up ssh key-based authentication with more details here. - If you have an IPv6 capable ISP (and I hope you do!), take a note of your Pi’s own unique globally addressable address with
ifconfig eth0
.5 You can use this address to access your Pi remotely from IPv6 capable networks without the hassle of getting around IPv4 NAT! - Try logging in with SSH over IPv6. For example,
ssh -6 pi@_2001:e42:102:1508:160:16:64:205_
(but please use your own IP).6
Step 5: Run System Updates and Setup Automatic Updates
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install unattended-upgrades
- Enable automatic updates by uncommenting the appropriate lines in the config file (
sudo vi /etc/apt/apt.conf.d/50unattended-upgrades
) and runningsudo dpkg-reconfigure -plow unattended-upgrades
to create/etc/apt/apt.conf.d/20auto-upgrades
. (For details see the Debian wiki page on unattended upgrades.)
Step 6: Add Some Convenience Utilities
These are a few I usually start with:
sudo apt-get install htop
A nice utility for watching CPU usage on individual cores.sudo apt-get install vim
Something a bit more full-featured than vim-tiny. How I set up code highlighting and indentation for Swift in Vim.sudo apt-get install screen
So we can keep our compilation going even when our connection to the Pi drops. Don’t forget toecho "termcapinfo xterm* ti@:te@" >> ~/.screenrc
so that screen does something sane with scrolling.
And you’re ready to start compiling Swift!
The next post in this series: How to Compile Swift on a Raspberry Pi 2
- No monitor, no mouse, no keyboard. [return]
- I sometimes use
openssl rand -base64 32 | head -c${1:-32}; echo
to generate a quick random password on the command line. Depending on your needs, this may not be a sufficiently random method of generating a password. This site lists some other options, however note any methods based on a hash of a date or time would generally not be considered secure. (If the system clock is accurate, the resulting hashes would fall within a known and relatively small range; if the system clock has not yet been set, that range could be even smaller.) Or, remove all password-based access to the account withsudo usermod -p '*' pi
and only allow SSH key-based authentication.[3. Since there’s really no need for password-based authentication after the initial set up, you might consider disabling it entire by addingPasswordAuthentication no
to/etc/ssh/sshd_config
. [return] - This change will take effect on your next reboot or when you restart the
dphys-swapfile
service:sudo /etc/init.d/dphys-swapfile restart
. Note that creating a 2 GB swap file can take several minutes. [return] - You can also disable audio by modifying
dtparam=audio
at the same time, but note that the kernel modules that handle sound are very small to begin with—around 161 KB in total according tolsmod
. [return] - The IPv6 address that you’re looking for is the address that appears after
inet6 addr:
that ends withScope:Global
. The address beginning withfe80:
and ending inScope:Link
is your ‘link local’ address. As the name implies, this is a local (that is, not globally accessible) address for the specific link (say, the network cable connecting the Pi to your router). Unless you’re on the other end of that specific network segment you won’t have much luck trying to log in remotely using the link local address. [return] - I typically add my Pi’s IPv6 address to
/etc/hosts
, so I can just type something likessh pi@newswiftpi
. If you don’t want to modify your hosts file, you could just as easily keep the Pi’s IP address in a file (ssh pi@$(cat newswiftpiIP)
) or in an environmental variable (ssh pi@$NEWSWIFTPIIP
). [return]