Getting Started with Swift on Linux

December 04, 2015
Written by

Getting Started With Swift On Linux

print("Hello, world!")

When I wrote my first line of Swift code I immediately had visions of being able to use this beautiful programming language for more than just iOS and OSX apps. Yesterday, Apple officially made Swift open source and my dreams came true. This blog post will help you quickly get started writing your first application using the open source version of Swift on Linux.

Here We Go!
The Linux implementation of Swift currently only runs on Ubuntu 14.04 or Ubuntu 15.10. For our application, I’ll be using Ubuntu 14.04.3. If you don’t have an Ubuntu server sitting around you can always spin one up on your hosting provider of choice (Looking for one? Check out DigitalOcean or Linode). The Swift GitHub page shows you how to build Swift manually but you may want to start writing code without having to wrestle with Linux. Fortunately Apple provides snapshots that you can download and get running with a quickness.

Grab the URL of the Ubuntu 14.04 snapshot and then pull it down to your server using wget:

wget https://swift.org/builds/ubuntu1404/swift-2.2-SNAPSHOT-2015-12-18-a/swift-2.2-SNAPSHOT-2015-12-18-a-ubuntu14.04.tar.gz

This may take a couple minutes to download. While you’re waiting, it’s a good time to watch Stephen Malkmus and the Jicks cover Taylor Swift’s “Blank Space”.

Once you have the snapshot, decompress it:

tar -zxvf swift-2.2-SNAPSHOT-2015-12-18-a-ubuntu14.04.tar.gz

In order to use Swift from the command line you need to update your path. Make sure to update this command to reference the path to where you downloaded the Swift snapshot:

export PATH=/path/to/swift-2.2-SNAPSHOT-2015-12-18-a-ubuntu14.04/usr/bin/:"${PATH}"

If you’re using a fresh Ubuntu server like I was you may be missing a few packages required for Swift to run correctly. To make sure you have everything in place you can run the following command:

apt-get update

Once that completes install all the required dependencies with this command:

sudo apt-get install git cmake ninja-build clang uuid-dev libicu-dev icu-devtools libbsd-dev libedit-dev libxml2-dev libsqlite3-dev swig libpython-dev libncurses5-dev pkg-config

More waiting. Perhaps time for an impromptu dance party?

Test that everything is running correctly by running swift --version. You should see a version number like this: Swift version 2.2-dev.

Let’s Sling some Code
Are you ready to write some code? I sure am! We’ll start by firing up the Swift REPL with the swift command. Once the REPL is running you can start throwing down some Swift. Here are some examples to try:

>1 print("Hello, Swift!")
>2 41 + 1
>3 import Glibc
>4 random()

When you’re ready to exit out of the REPL just type :q. Wow. Wonder where they got that idea?

The time has come to write our first app. First, we’ll create a new folder for our application to live. Within that folder we’ll create another folder called sources where we’ll write the code for our applications:

mkdir helloswift
cd helloswift
mkdir sources
cd sources

Within the sources folder create a file called main.swift. The name of this file is important, by naming it main.swift our application will automatically be built into an executable when we run swift build:

import Foundation
import Glibc

let player = ["rock", "paper", "scissors", "lizard", "spock"]

srandom(UInt32(NSDate().timeIntervalSince1970))
for count in 1...3 {
    print(count)
    sleep(1)
}

print(player[random() % player.count]);

Do you ever wish you could play “Rock, Paper, Scissors, Lizard, Spock” but no one is around? We just built a Swift application that will let you play against the computer whenever you want!

You can play your first game with the following command:

swift sources/main.swift

One thing I’m very excited about in the open source version of Swift is the Swift Package Manager because it makes it super easy to package and share code like our game with others. We won’t take advantage of some of the advanced features of the package manager in this post but we do need to create a Package.swift file to be able to build our application. The Swift Package Manager is case sensitive so make sure you have the capital P. We can keep this pretty basic for now:

import PackageDescription

let package = Package(
    name: "HelloSwift"
)

Now that we have everything in place we can build our application by running our build command:

swift build

The build command will produce an executable that we can run from the command line:

.build/debug/HelloSwift

What Will You Build?
You’ve built your first “Hello, Swift!” application using the open source version Swift on Linux. Looking for some inspiration? I’ve found Apple’s A Swift Tour extremely helpful as I’ve started learning the language. There’s a lot of good stuff to be found on Swift.org as well.

There’s a lot left to be added to the Linux version of Swift but I am looking forward to seeing it grow. Maybe you’ll even want to contribute to the project so we can all benefit from what you’ve learned.

I’d love to see what you build now that you have your environment set up!