When Apple first announced that Swift was going to be open sourced at WWDC last summer, one of my first hopes was that I could use it to write programs for Raspberry Pi. With Swift’s actual open sourcing earlier this month, I immediately set out to get the Swift compiler up and running on my Raspberry Pi 2. It turned out to be somewhat more difficult than I expected, and I ended up distracted with the holidays, etc. Thankfully, other people were working on it too.
About a week ago, William Dillon (@hpux735) announced that he had gotten the Swift compiler to successfully build for armv7 systems such as the BeagleBone, Raspberry Pi 2, etc. A while later, @iachievedit had packaged William’s work up and made it available via apt-get for Debian and Ubuntu. (Note that Swift is not yet working Raspberry Pi 1. It’s being worked on though.)
Yesterday, using their work I got Swift up and running on my own Raspberry Pi 2, and tweeted about it:
Thanks to @iachievedit, @hpux735, and others’ hard work, I have the Swift compiler running on my @Raspberry_Pi! pic.twitter.com/bjRgCvTGH6— Andrew Madsen (@armadsen) December 26, 2015
To my surprise, a lot of people were interested, and my tweet was retweeted widely, including by Chris Lattner. A number of people asked for instructions for getting up and running with Swift on Raspberry Pi, so I’m writing those up here.
A few disclaimers: I did very little to make this happen. I mostly just followed @iachievedit’s instructions for installing William Dillon’s Swift ARM packages. Still, there were a few hangups that I had to figure out, and I hope these instructions are useful. Also, Swift on ARM is still in an alpha state. The compiler itself works, but Foundation, and the Swift Package Manager are not working. Until these, especially Foundation, are up and running, it will be somewhat hard to use Swift for anything terribly useful. Anyway, instructions follow:
To start, we need to install Ubuntu 14.04 on an micro SD card (I tried to use Raspbian, and ran into a simple roadblock, but it may be possible too). The instructions for installing Ubuntu are mostly taken from this page and this page. First, download an image of Ubuntu 14.04 for Raspberry Pi 2 here. When the download finishes, unzip the downloaded file somewhere reasonable (I put it on my Desktop).
Connect the SD card to your Mac, then list all connected disks by running the following command in Terminal:
Find the SD card in the list, including its disk number. It will be identified as /dev/disk# where # is the disk number. You’ll need this for later steps.
Next, format the card using FAT32 like so:
diskutil eraseDisk FAT32 PiUbuntu MBRFormat /dev/disk#
(Replace # with the disk number you found above).
Now we’re ready to copy the Ubuntu image to the SD card:
sudo dd bs=1m if=~/Desktop/2015-04-06-ubuntu-trusty/2015-04-06-ubuntu-trusty.img of=/dev/rdisk#
Enter your Mac system password when prompted. (Again, replace # with the disk # found using
When the copy is done, eject the SD card from your Mac, and insert it into your Raspberry Pi 2, then power up the Pi. Assuming all went well, the Pi should boot to a command prompt. We need to finish some setup steps. First, delete the second partition on the disk, by running:
sudo fdisk /dev/mmcblk0
When prompted, enter ’d’ (for delete), then ‘2’. Then, recreate the partition by entering 'n’, then 'p’, then '2’, then pressing enter at the next two prompts.
Reboot by running
After the reboot, resize the partition’s file system by running:
sudo resize2fs /dev/mmcblk0p2
Setup a swap file by doing:
sudo apt-get install dphys-swapfile
If you want a GUI desktop, install Lubuntu (or Xubuntu or Kubuntu if you prefer):
sudo apt-get install lubuntu-desktop
Then reboot (
After all this, we’re ready to install the Swift compiler. Following @iachievedit’s instructions, do the following:
Install libicu-dev and clang-3.6:
sudo apt-get install libicu-dev clang-3.6
Use update-alternatives to provide /usr/bin links for clang and clang++:
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
Then, add @iachievedit’s repository key:
wget -qO- http://dev.iachieved.it/iachievedit.gpg.key | sudo apt-key add -
Add the appropriate repository to sources.list:
echo "deb [arch=armhf] http://iachievedit-repos.s3.amazonaws.com/ trusty main" | sudo tee --append /etc/apt/sources.list
Run apt-get update:
sudo apt-get update
Finally, install Swift!
sudo apt-get install swift-2.2
After the installation is complete, you’re ready to compile Swift programs!
Open your favorite text editor, write a program, and save it (e.g. to 'hello.swift’):
let device = "Raspberry Pi 2!" print("Hello from Swift on \(device)")
Then compile it:
and run it:
Hello from Swift on Raspberry Pi 2!
That’s it! Swift running on Raspberry Pi. In order for Swift to be terribly useful on Raspberry Pi 2, more work needs to be done. See the Swift on ARM issue on the Swift bug tracker, and its child cases here. Follow that issue, along with myself, @iachievedit, and @hpux735 to keep up with what’s going on.
One thing that is needed are libraries for using the Pi’s hardware peripherals, including the GPIO pins, i2c, SPI, serial, etc. I’m working now on porting my serial port library, ORSSerialPort to Swift, in preparation for adding Linux support. I’d also like to write a library specifically for Raspberry Pi I/O in Swift, but have not yet started on it. Stay tuned for more.