Getting Started with Swift on Linux
Time to read: 4 minutes
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
:
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:
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:
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:
Once that completes install all the required dependencies with this command:
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:
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:
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
:
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:
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:
Now that we have everything in place we can build our application by running our build command:
The build command will produce an executable that we can run from the command line:
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!
Related Posts
Related Resources
Twilio Docs
From APIs to SDKs to sample apps
API reference documentation, SDKs, helper libraries, quickstarts, and tutorials for your language and platform.
Resource Center
The latest ebooks, industry reports, and webinars
Learn from customer engagement experts to improve your own communication.
Ahoy
Twilio's developer community hub
Best practices, code samples, and inspiration to build communications and digital engagement experiences.