Getting started with Swift and Raspi V.2

While progress with Swift on ARM has been very encouraging, there have been a few reports of people having trouble with it.  This post is intended to be a step-by-step, starting with a brand-new Raspberry Pi version 2 and a freshly-made Raspian image (note that other distributions might work, I’ve only verified raspian).

Setup the raspberry pi

I expect that you can ssh to your raspberry pi (hereto referred to as raspi), and that you’ve run the raspi-config utility.

First, you need to expand the range of debian repositories in order to access the required prerequisites.  To do that, edit /etc/apt/sources.list:

sudo nano /etc/apt/sources.list

and uncomment the last line in the file, which should look like this:

deb-src jessie main contrib non-free rpi

Then, you’ll need to update your repositories.

sudo apt-get update

Install prerequisite software

Once that’s complete, install clang

sudo apt-get install clang

Option 1: install from tarball

A tar of the swift installation (usually more up to date) is available from my website. You can install this in either your system root, or from another directory. I do all of my testing with it installed in the system root, and therefore things are more likely to work. However, you’re putting your system at more risk by doing that. Keep in mind, though, that you’re playing around with alpha-level software on your <$100 computer… YOLO.

cd /
sudo wget
sudo tar -xzpf swift-armv7.tar.gz
rm swift-armv7.tar.gz

Option 2: install with Joe Bell’s repo

Joe @iachievedit did a fantastic job creating a debian repository for the swift compiler and tools, and he’s hosting it in an amazon aws instance.  Taking this route has some huge advantages.  First among them is that they’re only updated when things are stable-ish.  The version hosted at my website can change frequently, and it might even be completely broken!


Now, assuming that everything went according to plan, you should be able to compile programs written in swift. Also, you should be able to use glibc and Foundation.

pi@raspberrypi:~ $ cat hello.swift 
import Glibc
import Foundation

let now = NSDate()

print("Hello world at \(now)")
pi@raspberrypi:~ $ swiftc hello.swift 
pi@raspberrypi:~ $ ./hello 
Hello world at 2016-01-09 05:36:31 +0000
pi@raspberrypi:~ $ uname -a
Linux raspberrypi 4.1.13-v7+ #826 SMP PREEMPT Fri Nov 13 20:19:03 GMT 2015 armv7l GNU/Linux

Not so fast

Ok, so there are some caveats. For one, it seems like the REPL is kinda broken on the Raspi v.2.  Joe noticed this, too.  I’m not sure what the root cause is for this one.  It works on the original raspberry pi (armv6), and it works on the Beaglebone Black and Nvidia Tegra (armv7).

Also, things are very alpha-level, so expect the unexpected.

  1. #1 by davidatoakside on January 22, 2016 - 12:45 am

    This all works for me with Ubuntu Mate Rpi2 (using Option 1) And REPL seems OK, but perhaps that’s been resolved?

  2. #2 by hpux735 on January 22, 2016 - 7:41 am

    I’m not certain it’s totally resolved. I think the problem they were having was related to software versions in their install of linux. The bug it’s a duplicate of had someone make really excellent progress.

  3. #3 by davidatoakside on January 22, 2016 - 9:33 am

    That could explain my experience. My first attempt had failed. I was then using an updated and developed version of an Ubuntu image on a usb stick. I replaced that by an original of the same image on a micro sd and all was then well. So that could certainly be inconsistency in the software versions of components.

  4. #4 by hpux735 on January 22, 2016 - 9:36 am

    Definitely. It also explains why I couldn’t reproduce it. I kept starting with a fresh download of ubuntu.

(will not be published)

Please complete this capcha. I get almost 1000 spam comments a day! * Time limit is exhausted. Please reload CAPTCHA.