How to compile Swift on Raspberry Pi

Mission Kao
4 min readApr 24, 2016

--

last updated : April 25, 2016

After Swift was open sourced last year, there are lots of extension applications such as web framework (IBM Kitura) or some other backend services were developed in Swift, not just only for iOS/OS X App.

Thanks for Andrew Madsen, iAchieved.it and William Dillon, now we can compile and run Swift on Raspberry Pi or other armv7 systems. Recently, I followed Andrew Madsen’s blog and tried to build a Swift compiler on my Raspberry Pi 2 and I succeed. Therefore, I am going to introduce how to make it in this article.

My Devices :

  • Raspberry Pi 2 Model B with 1GB RAM (40pin extended GPIO)
  • 8 GB Sandisk microSD card
  • EDIMAX USB wireless adapter

Operation System :

Getting Started

  • Insert the microSD card to Mac and format it to FAT32. (followed by Michael Crump)
  • Follow the raspberrypi.org’s instructions to copy the Raspbian Image on microSD card. (If you would like to install Ubuntu and run swift on Raspberry Pi, you can refer to Andrew Madsen’s blog)
  • Insert the microSD card to our Raspberry Pi and power up. Raspberry Pi would start to boot. For raspbian, the account/pwd is pi/raspberry.
  • We need to resize the Raspbian 2GB image partition to make full use of a 8GB microSD card by deleting the second partition on the disk. (refer to this page)
sudo fdisk /dev/mmcblk0
d, 2 to delete the main partition
n p 2 to create a new primary partition
  • Reboot the Raspberry Pi and resize our filesystem.
sudo reboot
sudo resize2fs /dev/mmcblk0p2
  • Expand Filesystem Ensures that all of the SD card storage is available to the OS by running: (refer to this page)
sudo raspi-config
  • Setup the Internet enviroment and make Raspberry Pi be able to connect the Internet by ping google.com for testing. Running
sudo vi /etc/wpa_supplicant/wpa_supplicant.conf

Copy the follwing text and paste to wpa_supplicant.conf.
ssid is your AP’s name and psk is AP’s password. (You can use your mobile phone to be the AP)

ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdevupdate_config=1network={    ssid=”Mission”    psk=”00000000"    proto=RSN    key_mgmt=WPA-PSK    pairwise=CCMP    auth_alg=OPEN}

Running the following cml to turn up the wlan

sudo ifdown wlan0sudo ifup wlan0sudo kill -9 $(ps -ef | grep wpa | awk ‘{print $2}’)sudo wpa_supplicant -B -i wlan0 -c /etc/wpa_supplicant/wpa_supplicant.conf sudo dhclient wlan0

Ping 8.8.8.8 or google.com to ensure that Raspberry Pi has connected to the Internet.

ping 8.8.8.8ping google.com
  • The clang-3.6 and libicu-dev packages need to be installed. In addition, you should use update-alternatives to provide /usr/bin links for clang and clang++. (refer to this page)

Run the following commands to add the rcn-ee repository:

wget http://repos.rcn-ee.com/debian/pool/main/r/rcn-ee-archive-keyring/rcn-ee-archive-keyring_2015.10.22~bpo90+20151022+1_all.debsudo dpkg -i rcn-ee-archive-keyring_2015.10.22~bpo90+20151022+1_all.debecho "deb [arch=armhf] http://repos.rcn-ee.com/debian/ jessie main" | sudo tee --append /etc/apt/sources.listsudo apt-get update

Then, install the dependencies libicu-dev and clang-3.6:

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

Install Swift

  • Add the @iachievedit’s repository key
wget -qO- http://dev.iachieved.it/iachievedit.gpg.key | sudo apt-key add -
  • Add the repository to sources.list:
echo "deb [arch=armhf] http://iachievedit-repos.s3.amazonaws.com/ jessie main" | sudo tee --append /etc/apt/sources.list
  • Run apt-get update
sudo apt-get update
  • Install Swift 2.2
sudo apt-get install swift-2.2
  • After the installation is complete, we are able to compile Swift on our Raspberry Pi now by testing some swift code. I recommend using vim to be our text editor instead of using vi because I cannot type correctly on vi by Mac.
print("Hello World!!")
  • Then compile it and run it:
swiftc hello.swift./hello

SwiftGPIO

In addition to compile and run swift on Raspberry Pi, we can also use Swift library to interact with Linux GPIOs/SPI by just following this repository on the github.

  • Manually download Sources/SwiftyGPIO.swift:
wget https://raw.githubusercontent.com/uraimo/SwiftyGPIO/master/Sources/SwiftyGPIO.swift
  • Create a main.swift file and make our Raspberry Pi to lighten up a LED. (the file name should be main.swift)
  • When your code is ready, compile it with:
swiftc SwiftyGPIO.swift main.swift
  • Run it:
sudo ./main

For more examples or some awesome projects, you can refer to SwiftGPIO.

--

--

Mission Kao

i am from Taiwan. A beginner of iOS development. Now i am working in a startup called iCHEF which provides an iPad based POS (point-of-sale) system.