Introduction to Open Source Swift on Linux

Learn how to use open source Swift to run a Hello, World app on Linux – and how to use the Swift package manager! By Alexis Gallagher.

Leave a rating/review
Save for later
Share

Note: This tutorial has been updated to work with the latest versions of Ubuntu 14.04 and the Swift 3.0 development snapshots as of April 20, 2016. As a result, some dates and version numbers in this tutorial have been updated from the time of the initial relase of Swift on Linux.

Less than a week ago, the Swift world woke up to an early Christmas present — open source Swift — that you can run on Linux!

And there was even a present inside the present: Apple announced new projects and tools intended to make Swift an incredibly practical choice for Linux development. This creates exciting possibilities, such as running Swift on platforms from Linux servers right down to five-dollar Raspberry Pi Zeros, or in the kajillions of other environments that host Linux.

In this tutorial, you’ll set up a Linux environment on your Mac, install Swift, and compile and run some basic Swift examples on Linux. Then you’ll take stock of the new bits offered by Apple, and finish off with a look into into the misty crystal ball of time, and guess what this all means for the future.

This tutorial does not require any previous knowledge of Linux and you don’t even need Linux installed yet – but if you also have Ubuntu running, all the better! All you need is a Mac running at least OS X El Capitan and an interest in open source Swift. Let’s get started!

Installing Swift on Linux

If you’re already running Ubuntu 14.04 LTS or Ubuntu 15.10, either directly or in a VM on Mac or Windows, you can simply follow the instructions on swift.org to download and install Swift for Linux and skip to the next section.

If you’re not running one of the above versions of Ubuntu, follow the instructions below to install Virtualbox and Ubuntu on your Mac. This will use less than 1 GB of disk space and confine Linux to a virtual machine — without affecting your current OS X installation.

Installing VirtualBox

VirtualBox is a free, open-source application that lets you run other operating systems as virtual machines in parallel with OS X.

Go to the VirtualBox downloads page, and download the disk image for VirtualBox 5.0.16 or later. Double-click the downloaded DMG and run the VirtualBox.pkg package installer:

swift-on-linux.VirtualBoxdDiskImage

Installing and Using Vagrant

Vagrant is a command-line interface to VirtualBox; it can download virtual machine images for you, run provisioning scripts, and coordinate the interaction between OS X and the machine by setting up things like networking and shared folders.

Go to the Vagrant downloads page and download Vagrant for Mac OS X. Double-click the downloaded DMG disk image and run the Vagrant.pkg package installer:

swift-on-linux.VagrantDiskImage

Now you’ll need a Vagrantfile to tell Vagrant exactly which version of Linux you want to use and how to install Swift on that version of Linux.

Create an empty directory in your Documents folder (or wherever makes sense for your workflow) and name it vagrant-swift. Inside that directory, create a file named Vagrantfile and add to it the lines below:

Vagrant.configure(2) do |config|
  ## 1
  config.vm.box = "https://cloud-images.ubuntu.com/vagrant/trusty/current/trusty-server-cloudimg-amd64-vagrant-disk1.box"
 
config.vm.provision "shell", inline: <<-SHELL
    ## 2
    sudo apt-get --assume-yes install clang libicu-dev
    ## 3
    curl -O https://swift.org/builds/development/ubuntu1404/swift-DEVELOPMENT-SNAPSHOT-2016-04-12-a/swift-DEVELOPMENT-SNAPSHOT-2016-04-12-a-ubuntu14.04.tar.gz
    ## 4
    tar zxf swift-DEVELOPMENT-SNAPSHOT-2016-04-12-a-ubuntu14.04.tar.gz
    ## 5 
    sudo chown -R vagrant:vagrant swift-*
    ## 6
    echo "export PATH=/home/vagrant/swift-DEVELOPMENT-SNAPSHOT-2016-04-12-a-ubuntu14.04/usr/bin:\"${PATH}\"" >> .profile
    echo "Swift has successfully installed on Linux"
  SHELL
end

Now to run your Vagrantfile. Open Terminal on your Mac (found in the /Applications/Utilities folder), change to the vagrant-swift directory that contains the Vagrant file above, then enter the following command to run your script:

vagrant up

Sit back and wait while Vagrant performs the following steps based on your Vagrantfile:

  1. Downloads a disk image of Ubuntu LTS 14.04 (the latest version as published by Canonical, the Ubuntu company, whenever you are running the script). It will only perform this download once and then cache the image for later use. You will likely see an “Attempting to find and install…” message for the Box file, followed by a message about “Adding it directly…”.
  2. Runs the downloaded disk image in virtualization.
  3. Installs Apple’s C compiler clang and the internationalization library libicu, both components which the Swift compiler requires.
  4. Downloads Swift (as distributed by swift.org on April 12th, 2016) and uncompresses it.
  5. Adjust some file permissions needed to use the Swift Package Manager.
  6. Configures the default user’s home PATH variable, so it can run the swift binary, REPL, and other tools.

If this completes successfully, the final message will say that “Swift has successfully installed on Linux”.

Next, you’ll need to connect to your Linux virtual machine. In the same vagrant-swift directory, execute the following command:

vagrant ssh

You should now find yourself at an Ubuntu prompt as a user called “vagrant”. To verify that you can access Swift and it’s functioning properly, execute the following command:

swift --version

You should see something like the following:

Swift version 3.0-dev (LLVM 752e1430fc, Clang 3987718dae, Swift 36739f7b57)
Target: x86_64-unknown-linux-gnu

Tada! Swift on Linux. It’s a Christmas miracle! :]

We’re pretty sure Tiny Tim would have appreciated open source Swift.

We're pretty sure Tiny Tim would have appreciated open source Swift.

We’re pretty sure Tiny Tim would have appreciated open source Swift.

Compiling a Program

You’ve installed Swift on Linux. Now it’s time to compile and run something.

Of course, hallowed coding tradition dictates that your first program do nothing but say “Hello, world”. And in true developer form, you’ll do it the easiest way possible. :]

Switch to the Linux shell prompt and execute the following command:

cat > helloworld.swift

Now enter the following single line of Swift code:

print("Hello, world")

Press Enter once followed by Ctrl-D to create the file:

Execute ls in the shell to show the directory listing and confirm that you have created a file named helloworld.swift.

Now execute the following command at the shell prompt to invoke the swift compiler and compile your program:

swiftc helloworld.swift

Thanks to @phildev for pointing this out!

Note: If you get an error while compiling, you may need to update 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

Thanks to @phildev for pointing this out!

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

Execute ls -l h* at the shell prompt to see detailed information on all files beginning with h:

-rwxrwxr-x 1 vagrant vagrant 13684 Dec  4 17:55 helloworld
-rw-rw-r-- 1 vagrant vagrant    22 Dec  4 17:55 helloworld.swift

helloworld has x’s in the first column as it’s an executable file. Execute the following command to run helloworld:

./helloworld

You’ll see the following output:

Hello, world

Lightning and thunderbolts! You’ve run Swift on Linux. So easy! :]

But what does this all mean? What can you really do with Swift on Linux? To answer these questions, put the brakes on this runaway freight train of cross-platform, bleeding-edge and technological euphoria, and take a moment to review what it is that Apple has actually released.