In this post we will see how to install Swift, the programming language created by Apple, on Linux, concretely in Debian‘s currently stable release(named Jessie).
Swift is intended to be used to develop software for the Apple platforms (iOS,tvOS, watchOS, etc), which means that serious developing is to be, at this time, done in a Mac, and using Mac tools like XCode.
Still Swift is open source, and the binaries for Linux can be downloaded from the open source Swift community site. Which means that we can install the Swift core in a Linux machine, and the Swift compiler: we won’t be able to develop an iPhone app in our Linux box, but we will be able to run simple/small programs intended mainly to learn the basics of Swift programming.
1. Create a directory for the Swift binaries
mkdir /opt/swift cd /opt/swift/
2. Download the binaries from the Swift.org site
Notice that Swift.org does not provide binaries for all the Linux distributions out there, but merely for Ubuntu. Given that Ubuntu is a Debian based distribution, we should be able to use those binaries, too, in our Debian Jessie system (and presumably in the rest of Debian based distributions).
You may also notice that I am downloading the Swift 2 version. At the time of writing this Swift 3 can be downloaded from the same site. Why I am sticking to an older release? The reason is that Swift 3 requires a newer version of some libraries (like libstdc++6) than the ones available for Debian Jessie. So if we want to run Swift 3 in Linux, we will need either to run the unstable Debian development release, or run a distro based on the testing/unstable release of Debian: such as Ubuntu.
3. Untar the files
tar -xzf swift-2.2-RELEASE-ubuntu14.04.tar.gz
4. Add Swift to your path
And that’s it.
Write, compile, and run a Swift program
Use your favorite text editor to write a short swift program, for example a “Hello world” application like this one:
var language = "Swift" print("Hello World. I am a \(language) application")
save it as swiftHelloWorld.swift, and compile it using the following command:
This will produce a swiftHelloWorld file, that can be executed from the command line, and that as expected will print the Hello world message on screen:
swiftHelloWorld Hello World. I am a Swift application
For a more complete example on Swift code running on Linux, you can check this post on how to make a REST Call in Swift from Linux to an Azure Backend.
If you liked this post, you can subscribe to my blog (click at the follow button at the bottom), and you will be notified when new post are written.