Update! If you have the Swift compiler on your Linux box already, head on over to our tutorial on building Swift applications to run on Linux. If you don’t have one, come and grab it with apt-get
from here.
On December 3, 2015 Apple made good on its promise to open source Swift, their new language for development on Apple platforms. Swift has quickly risen in popularity and folks are developing Mac, iOS, WatchOS, and now tvOS apps with it.
While I enjoy developing in the Apple ecosystem and writing iOS apps, my first love will always be writing software for Unix systems (please don’t post a comment reminding me that OS X is a Unix). So while I’m excited about the fact that Swift is now open source, I’m more excited about the ramifications of that fact, primarily inroads Swift can make into the server room, operations environments, and IoT devices.
Swift on Ubuntu
Apple has made available all of the Swift source code available on Github, and even included instructions for building it on Ubuntu 14.04 LTS. I tried it, and while I did get it to compile, it isn’t quite polished yet in terms of “easy installation.” Fortunately if you search around you will eventually find this page for easier installation.
So, here we go, the iAchieved.it quick start guide for Swift on Linux, though its fairer to call it Swift on Ubuntu for now.
Step 1. Get the Package
Go to the download page and grab the version appropriate for your Ubuntu version, either 14.04 or 15.10. I’m going to get the 14.04 version and save it to my /usr/local/archive
directory where I keep tarballs.
sudo mkdir /usr/local/archive cd /usr/local/archive sudo wget https://swift.org/builds/ubuntu1404/swift-2.2-SNAPSHOT-2015-12-01-b/swift-2.2-SNAPSHOT-2015-12-01-b-ubuntu14.04.tar.gz
December 13 Update: Copy/paste is great, but there are new builds coming out every other week from Apple. Double-check the latest development snapshots before your wget
.
Step 2. Unpack
Like /usr/local/archive
is a convention I use, I am also picky about where vendor software is installed and typically use the /opt/provider
convention, which I’ll do here.
cd /opt sudo mkdir apple cd apple sudo tar -xzf /usr/local/archive/swift-2.2-SNAPSHOT-2015-12-01-b-ubuntu14.04.tar.gz
This will leave us with a directory named swift-2.2-SNAPSHOT-2015-12-01-b-ubuntu14.04
in /opt/apple
.
Step 3. Add it to your PATH
The directory structure Apple chose within is a typical usr
structure with the Swift compiler in usr/bin
. Let’s update our path now to pick up the compiler. In your shell’s rc-file update your PATH
environment variable to include /opt/apple/swift-2.2-SNAPSHOT-2015-12-01-b-ubuntu14.04
, like this:
1 2 |
# User configuration export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/opt/apple/swift-2.2-SNAPSHOT-2015-12-01-b-ubuntu14.04/usr/bin" |
Reload your rc file (I use . ~/.zshrc
) and type which swift
. You should see something like this:
# which swift /opt/apple/swift-2.2-SNAPSHOT-2015-12-01-b-ubuntu14.04/usr/bin/swift
Step 4. Optional! Emacs Goodness
When developing on the Mac I like Xcode. On a Linux box, emacs is the only editor for me. Since there is swift-mode available out there I naturally wanted to use it, but it turns out it only supports Emacs 24.4 and higher, so we need to install it on Trusty (14.04). Luckily someone out there has a PPA available for it:
1 2 3 4 |
sudo apt-get remove -y emacs24 sudo apt-add-repository ppa:andreas-h/emacs sudo apt-get update sudo apt-get install -y emacs24 |
Edit your ~/.emacs.d/init.el
Emacs configuration file and add something like
1 2 3 |
(require 'package) (package-initialize) (add-to-list 'package-archives '("melpa" . "https://melpa.org/packages/")) |
Launch emacs and using M-x package-refresh-contents
to pull in the packages from the MELPA repository, and then M-x package-install
to install the swift-mode
package.
Step 5. Try it out!
We’re not going to use the REPL function right now, but will compile everything together. First, you may need to install some dependencies if you don’t already have them:
sudo apt-get install clang libicu-dev
Warning! After an hour or two browsing around I discovered that the current version of Foundation released with Swift for Linux doesn’t have a lot of items implemented. For example, the status page currently states (as of December 5, 2015) that NSURLSession
isn’t implemented and that NSURL
is “mostly” implemented. Mostly doesn’t include using init(URL:)
apparently!
fatal error: init(URL:) is not yet implemented: file Foundation/NSObjCRuntime.swift, line 64
If you take a look at the new Swift Package Manager being developed, you can see in the source code where low-level code for routines like popen
is being developed to communicate with Github for downloading repositories. Even NSTask
isn’t implemented!
Until these Foundation classes are implemented it might be worth waiting a bit before trying to really do much with Swift on Linux. Our sample code just shows a basic example of using main.swift
and a class file to calculate the area of a rectangle. Boring stuff!
In a file called main.swift
(this name is important as it defines to Swift the entry point of your application, like main()
in C):
1 2 |
let s = Rectangle(base:5, height:6) print("The area of the rectangle is \(s.area()).") |
Now in a file called rectangle.swift
:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
class Rectangle { var base:Int var height:Int init(base:Int, height:Int) { self.base = base self.height = height } func area() -> Int { return self.base * self.height } } |
Compile it with the swiftc
compiler, which should now be in your path.
swiftc main.swift rectangle.swift
And of course, run it!
# ./main The area of the rectangle is 30.
Conclusion
There’s not a lot one can do right now with Swift on a Linux box. This will rapidly change as Foundation gets built out and others come in and begin developing packages. One should continue to monitor the download page to keep track of updates and keep an eye on the status of Foundation. As classes such as NSTask
and NSURLSession
are added we should see folks exploring using Swift as a “server language”. For me, I can’t wait!
Excellent post, thank you.
For those using bash shell:
– Change export:
export PATH=/opt/apple/swift-2.2-SNAPSHOT-2015-12-01-b-ubuntu14.04/usr/bin:”${PATH}”
– Add the export to .bashrc in ~ directory
– Reload shell:
. ~/.bashrc
How to install in centos?
As of today the open source Swift community is only “officially” supporting Ubuntu. I’ve compiled the source successfully for Debian Jessie, but have not attempted any Red Hat variants. To get started with a “port” you need to get Clang, LLVM, and the other prerequisite tools installed with
yum
and then build the repositories that are on Github.