Using Swift 3 on RaspberryPi — Beginning

Vincent Saluzzo
vincentsaluzzo
Published in
6 min readFeb 12, 2017

I work on Swift since 1.0. Every people which have doing it know the pain it was. The version 2 give us a lot of maturity and the 3 is about to be very pleasant to use, even if we have already some disagreement like time compiling compared to Objective-C.

I’ve used C, C++, Java, Objective-C, PHP, JavaScript, Swift and some others language in my early developer life, but definitively, for me, there is two language that come out of the pack: JavaScript and Swift.

JavaScript because everything exist in JS since NodeJS come out. Swift because I’m in love of the syntax, the feature and of course, enjoy iOS development since his beginning.

iOS Development is cool, but I love JavaScript for all others development: server side, embedded (RaspberryPi), Desktop graphical software (with Electron).

Since Swift 3, it’s possible to use it on Linux, and as a server side language, and the community take this to purpose a lot of framework: Vapor, Zewo, Kitura, Perfect and many more.

It’s time for my self to discover the Swift as a server-side langage, or simply as another language than for iOS Development.

Because RaspberryPi is for me the good subject to learn this, I begin my series with a preparation of raspberry for Swift development/execution.

Prepare a Raspberry Pi for Swift

First of all, Swift are compatible with macOS and Ubuntu. No centOS, no Debian, etc. You can find a lot of tutorial on the internet for use Swift on this platform, but I’ve tryed some of them and nothing fully work.

In my case, I will have only a mac with macOS, so my tutorial will be for mac user, but Windows/Linux users could use this guide, with some adaptations of course.

Install Ubuntu on your RPi

I’ve a 1, 2 and 3 version of RPi, but I will only use (and write for) RaspberryPi 3.

First, download Ubuntu distribution adapted for RPi, go to Ubuntu Pi Flavor Maker download page and choose Ubuntu Server Minimal 16.04. You will download a torrent file, so use your favorite BitTorrent client to download it.

Next, go to your Terminal.app and go to the folder you have download the distribution image file. Be sure to have The Unarchiver macOS Application installed on your computer. Then tap this command

open ubuntu-minimal-16.04-server-armhf-raspberry-pi.img.xz -a /Application/The\ Unarchiver.app

This will use the app for extract the img to the same place you will download it.

Then, connect your SD card to your computer and identity them. The simple way to this is to use the terminal before and after plug in your SD Card.

ls -l /dev/disk*
# Insert here your SD Card
ls -l /dev/disk*

Identity the disk added in the second call to ls . Example for me:

$ ls -l /dev/disk*
brw-r----- 1 root operator 1, 0 10 fév 20:15 /dev/disk0
brw-r----- 1 root operator 1, 2 10 fév 20:15 /dev/disk0s1
brw-r----- 1 root operator 1, 3 10 fév 20:15 /dev/disk0s2
brw-r----- 1 root operator 1, 1 10 fév 20:15 /dev/disk0s3
brw-r----- 1 root operator 1, 4 10 fév 20:15 /dev/disk1
$ ls -l /dev/disk*
brw-r----- 1 root operator 1, 0 10 fév 20:15 /dev/disk0
brw-r----- 1 root operator 1, 2 10 fév 20:15 /dev/disk0s1
brw-r----- 1 root operator 1, 3 10 fév 20:15 /dev/disk0s2
brw-r----- 1 root operator 1, 1 10 fév 20:15 /dev/disk0s3
brw-r----- 1 root operator 1, 4 10 fév 20:15 /dev/disk1
brw-r----- 1 root operator 1, 5 12 fév 14:21 /dev/disk2
brw-r----- 1 root operator 1, 6 12 fév 14:21 /dev/disk2s1

In my case, the SD Card is representing by /dev/disk2 .

Next, use dd command to write the img on your SD Card.

dd if=ubuntu-minimal-16.04-server-armhf-raspberry-pi.img of=/dev/disk2

In my case, I write /dev/disk2 but you need to replace by your disk. This commande probably take a while (approximatively 5~10 minutes…)

When done, unplug your SD Card, put them in your RPi and power in it. Be sure to have an Ethernet cable plugged in your RPi with a local network access, the same of your computer.

Update and configure your RPi

Now you have a Ubuntu installed on your RPi, you need to determine it’s IP address on your LAN. For this I will recommand you to use LanScan macOS application, launch it and click on Lan your scan button. Normally, you will find a network connection with a network interface beginning with raspberry`

LanScan result for finding Raspberry Pi on LAN

In my case, my RPi was attributed by my DHCP server the IP 192.168.1.42 I will use it to connect with SSH, always using Terminal.app use ssh command to login with the username ubuntu and the password ubuntu .

ssh ubuntu@192.168.1.42

Next, we will update, upgrade and configurat Wi-Fi on the RPi. The last step isn’t mandatory, but less than my RPi was cabled, more I appreciate.

On your SSH session, write this :

sudo apt-get update
sudo apt-get upgrade -y
sudo apt-get install -y vim wpasupplicant

Next, we using VIM editor to create a Wi-Fi configuration file, execute sudo vim /etc/wpa_supplicant/wpa_supplicant.conf , go into insert mode ( just tap on i ) and paste this block of text :

network={
ssid="TO REPLACE BY YOUR SSID NETWORK"
psk="TO REPLACE BY YOUR PSK PASSWORD"
}

Assuming that your Wi-Fi network is securised by WPA-PSK2, replace each TO REPLACE BY <...> by the correct value for your network configuration, press esc button on your keyboard and write :wq then Enter keyboard button. This will save the file.

Next, edit the network interface file /etc/network/interfaces (also by using VIM) for adding this piece of text :

auto wlan0
iface wlan0 inet dhcp
wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf

Use the same logic for save the file (esc , :wq and Enter) and ifup/ifdown the new configured network interface.

sudo ifdown wlan0
sudo ifup wlan0

Unplug the Ethernet cable on your RPi, reboot them and redo a LAN scan to identify the new IP address of your RPi which use Wi-Fi than Ethernet.

Great, you have a fully configured RPi. Next step is to install Swift on them.

Optional: Change default password to prevent hacking

Because you probably don’t expose your RPi other than your home network, you not need to change the password of ubuntu user. But if you still want it, by security or by other choice, you can simply using passwd command to do this:

$ passwd
Changing password for ubuntu.
(current) UNIX password:
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully

Of course, you don’t see my previous and new password in this, but you need to write previous (or current) password once and new password twice.

Install Swift

Now, we will install Swift on your RPi, two choice: Compile it your self or instal pre-compiled bundle.

The first take more time than the second, but you could customize your swift distribution. We prefer to use a pre-compiled bundle here because some guys have already do the job before, why doing it again for nothing ?

A lot of people work on Swift for ARM. To simplify distribution, it create a Jenkins build server which distribute pre-compiled Swift-ARM archived.

In this guide, we will use the version compiled Dec 21, 2016. We will put download archive and swift install inside user folder

cd ~
mkdir swift
mkdir swift-3.0
cd swift
wget http://swift-arm.ddns.net/job/Swift-3.0-Pi3-ARM-Incremental/121/artifact/swift-3.0-2016-12-21-RPi23-ubuntu16.04.tar.gz
cd swift-3.0 && tar -zxvf ../swift-3.0-2016-12-21-RPi23-ubuntu16.04.tar.gz
export PATH=$HOME/swift/swift-3.0/usr/bin:$PATH
cd ~

That’s all folk!

For testing Swift install, create a simple Hello World file.

echo "print(\"HelloWorld\")" > helloWorld.swift
swift helloWorld.swift

You must see something like this :

$ swift helloWorld.swift
HelloWorld

That’s done, trying to compile this file into executable

$ swiftc helloWorld.swift
$ ./helloWorld

In case of swiftc fail with :

<unknown>:0: error: link command failed with exit code 127 (use -v to see invocation)

You must install some missing dependencies :

sudo apt-get install -y libicu-dev
sudo apt-get install -y clang-3.6
sudo update-alternatives --install /usr/bin/clang clang /usr/bin/clang-3.6 100
sudo update-alternatives --install /usr/bin/clang++ clang++ /usr/bin/clang++-3.6 100

Retry to compile your helloWorld, that’s should work now.

--

--

Vincent Saluzzo
vincentsaluzzo

Swift developer, Dad and countryside lover ! Work at @itelios as Lead Developper iOS & NodeJS