I recently started teaching myself Swift, the new Open Source Language from Apple that powers their latest iOS and MacOS apps. In this article, I’ll go over how I installed it on Ubuntu 14.04 and how I got Sublime Text 2 to recognize it’s syntax. Note that there may be better and more comprehensive ways of doing this, I’m just posting what worked for me. If you have any suggestions to improve this, please be sure leave a comment.
Directions adapted from this post.
Start by clicking here and downloading the latest snapshot for Linux. For me it was called Ubuntu 14.04 Swift Development Snapshot, posted on Feb 3, 2016.
Then you’ll want to unzip the tarball.
tar -xzf swift-DEVELOPMENT-SNAPSHOT-2016-02-03-a-ubuntu14.04.tar.gz
Inside you’ll find a usr directory; move that somewhere you’ll remember for later. I put it in my home directory. Update your PATH variable to include the usr/bin directory inside. Note the following is only a temporary PATH update for the current session on the terminal.
mv swift-DEVELOPMENT-SNAPSHOT-2016-02-03-a-ubuntu14.04/usr /home/jamil/swift
You should be able to run the following now.
Swift version 3.0-dev (LLVM 524cfeb800, Clang f66c5bb67b, Swift c33f2faad2)
Before you can compile any files, you’ll need to install clang.
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
Almost there. Now let’s take it for a spin. Create a helloworld.swift file somewhere using your favorite text editor. Enter the following line. Swift looks pretty simple doesn’t it?
Now let’s compile.
And finally run it!
With me so far? You can stop here if that’s all you plan to do. If you use Sublime Text 2, follow these steps to include Swift syntax highlighting. These steps will require restarting ST2 a few times after you run commands.
Follow the instructions here to install Package Control for Sublime Text if you don’t have it already. It’s just a copy’n’paste command line to the Sublime Text console.
Restart Sublime Text.
Then install the Swift-Sublime-Package with the simple instructions found here. All you have to do is search for the Swift package and hit Enter.
Restart Sublime Text.
When you create a new file in Sublime Text, go to View > Syntax and you should see Swift as one of the choices. Pretty cool, huh?