Ready Your Raspberry Pi for Swift

I’m working on a series in conjunction with some talks I’m giving this spring/summer at the All Things Swift meetup here in San Diego. As one might expect, many Swift developers don’t have much linux experience. That’s perfectly fine – it doesn’t take a much to get started. I even come back to this setup process every time I set up new Raspberry Pis. Everything here assumes a Raspberry Pi 3 with Raspbian Jessie. Otherwise YMMV.

Before we get started, I’ll add this one note – Swift is under heavy development right now. Joe Bell has created a Swift-2.2 Debian package (below), and a number of people are working hard on Swift-3.0 for ARM devices. Development on Swift-2.2 has ceased in favor of bringing Swift-3.0 up to speed. Only recently has anyone been able to compile Swift on a somewhat consistent basis. There’s a good chance that within a few months many of the issues will be worked out.

Connecting

First, let’s make connecting our Pi a bit easier – install VNC and Bonjour (netatalk + avahi). The former enables screen sharing, and the latter allows you to connect to your Pi at an address such as
vnc://pi.local/ on your local network – much easier than remembering an IP address. Contributor fguillen posted a concise set of instructions on StackOverflow. Rather than re-hash his instructions, head over there. Remember to reboot the Pi when you’re finished (sometimes VNC doesn’t work properly without it).

If during setup, you did not change the default hostname, you can now connect to your Pi at
vnc://pi@raspberrypi . If you’d like to change it, as I did, you can run the raspi-config tool (via the GUI or command line). By running raspi-config, all the necessary config file changes are made for you.

At this point there’s little reason to keep the Pi connected to a monitor or TV. My next step is to stash it away somewhere out of the way so I can clear up some desk space. From now on, I’m connecting to the pi at ssh pi@pi.local .
.

Cut the Fat

If you plan on compiling Swift on your Pi (more on that another day), you’ll need at least a 16GB memory card. If you’re on the lower end, you may need to remove some extra cruft. Let’s start with LibreOffice, Mathematica, and Wolfram.

Shell

1

sudo apt-getautoremove mathematica*libreoffice*wolfram*

Swift via apt-get

The easiest way to get swift running on your RPi is to install Swift 2.2 via apt-get. There are several steps involved, but Joe Bell laid them out well. Follow them, but with a few important changes:

Go ahead and complete his instructions. When you’re finished, you’ll have a working Swift-2.2 installation.

There’s something important you need to know – the Swift REPL doesn’t work (yet). Neither does the Swift Package Manager. While you won’t be able to use any libraries that come via SwiftPM, you can still write some simple apps using Foundation and Glibc.

Let’s try a very simple Hello World…

Swift

1

print("Hello ?")

Compile and run…

Shell

1

swiftc helloWorld.swift&&./helloWorld

Finally! Doesn’t it feel great to see something familiar running outside the Walled Garden?

I mentioned compiling Swift from source above. That will come another time – certainly more involved than I intended for this post.