Editor’s Note: Swift 3.0 for Raspberry Pi 2 and Pi 3 is now available! Head on over to our Swift 3.0 for Raspberry Pi post for instructions on how to download and install it.
The following is similar instructions to that provided for other ARM devices like BeagleBoneBlack and BeagleBoard X15. There are some slight differences to get clang-3.6
properly installed.
If you’ve already installed the swift-2.2
package with the instructions below and just want to upgrade to the latest version, use sudo apt-get update && sudo apt-get upgrade
.
Disclaimer! Swift on ARM systems is very much at an alpha stage at the moment! The package below does not contain a working
- Swift package manager
Foundation implementation- REPL
12/27/15 Update: The REPL is now somewhat functional on BeagleBone and BeagleBoard, but there is a memory corruption error on the Raspberry Pi 2.
1/4/16 Update: Foundation is now working! Try some of the examples from this post.
You can track the progress of the port of Swift to ARM platforms at bugs.swift.org, and follow @iachievedit on Twitter for updates when new packages are released. To help out, create an account at bugs.swift.org
and when you find broken stuff, file bugs and tag them with ‘arm’.
Prerequisites
Both 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++
. To get clang-3.6
on a Raspberry Pi 2 you can use repositories provided by Robert C. Nelson.
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.deb sudo dpkg -i rcn-ee-archive-keyring_2015.10.22~bpo90+20151022+1_all.deb echo "deb [arch=armhf] http://repos.rcn-ee.com/debian/ jessie main" | sudo tee --append /etc/apt/sources.list sudo apt-get update
Then, install the dependencies libicu-dev
and clang-3.6
:
sudo apt-get install libicu-dev sudo apt-get install 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
Note: You must use update-alternatives
to link /usr/bin/clang
and clang++
.
Installation
At the moment there are only Raspberry Pi 2 packages for Debian Jessie. I have tested the basic installation on a Raspberry Pi 2.
1. Add the repository key
wget -qO- http://dev.iachieved.it/iachievedit.gpg.key | sudo apt-key add -
2. 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
3. Run apt-get update
sudo apt-get update
4. Install swift-2.2!
sudo apt-get install swift-2.2
Trying it Out
Again, a warning! There is no Swift package manager in this build yet and don’t expect to be able to use swift
to run it in REPL mode. You can however compile basic applications, even those using Glibc
and Foundation
.
root@beaglebone:~# more helloWorld.swift print("Hello, world!") for i in 1...10 { print(i) } root@beaglebone:~# swiftc helloWorld.swift root@beaglebone:~# ./helloWorld Hello, world! 1 2 3 4 5 6 7 8 9 10
or, try out swiftcat.swift
which uses Glibc
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import Glibc guard Process.arguments.count == 2 else { print("Usage: swiftcat FILENAME") exit(-1) } let filename = Process.arguments[1] let BUFSIZE = 1024 let pp = popen("cat " + filename, "r") var buf = [CChar](count:BUFSIZE, repeatedValue:CChar(0)) while fgets(&buf, Int32(BUFSIZE), pp) != nil { print(String.fromCString(buf)!, terminator:"") } exit(0) |
With the latest 0ubuntu11
release, Foundation
is supported too!
countdown.swift
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import Foundation import Glibc let thread = NSThread(){ print("Entering thread") for i in (1...10).reverse() { print("\(i)...", terminator:"") fflush(stdout) sleep(1) } print("\nExiting thread") print("Done") exit(0) } thread.start() select(0, nil, nil, nil, nil) |
Getting this on BBB. Is there a way it can be fixed?
After this operation, 0 B of additional disk space will be used.
# apt-get install swift-2.2
Reading package lists… Done
Building dependency tree
Reading state information… Done
The following NEW packages will be installed:
swift-2.2
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 63.0 MB of archives.
After this operation, 0 B of additional disk space will be used.
The
0 B of additional disk space will be used
is now fixed:“var pp” would be better as “let pp”. Trivial, but it’s swift.
Indeed. Interesting that the compiler didn’t catch that one, it usually emits a suggestion.
Do you know if there are news about REPL and PackageManager?
I’ve been in touch with the folks that are doing the hard work of getting Swift and all of the other complementary components built. The REPL (powered by
lldb
) and PackageManager (a separate project) have been the lower priority items. A few builds have been done but there were issues with SwiftPM working completely. I would say we are still a month or more away from having a fully-built suite for ARM-based systems at this time. But definitely watch the @iachievedit Twitter feed, when it is available it will be broadcasted there.Still no news about repl and pm :'(
Would it work on pi 3?
Thanks.
Carl, I don’t have a Pi 3 myself, but I may know some that do. I’ll ask and get back to you.
Thank you so much.
I have a pi 3 and it works as well. Just follow the instructions above. I’m only wondering if Swift 3.0 is also available for the pi?
Hi, thanks for your great effort in the Swift open source world. I have a Raspberry Pi 3 with Swift 2.2 installed via one of your tutorials, working great except that the swift-build binary is not included. Is there a way to solve this? I’ve read things about the Release/Development branch from Swift, the Development branch should have swift-build included.
Current version of raspbian only has clang-3.7, not clang-3.6. Any ideas about how to fix?
This might be a long shot, but what could be a viable route to getting a GUI up on the Raspberry Pi Touch Display using Swift?