Run Swift 3 with Docker for Mac


One of the safest and easiest ways to experience Swift 3 on Linux is to use the new Docker for Mac. It includes everything we need to run Linux images on Macs, and Docker also makes it easy to experiment and save checkpoints.

After you’ve signed into the Docker web site and installed Docker for Mac, you can follow these steps to start trying Swift 3 on Linux.


First, go to the terminal and use docker run to start running an image. We’ll use Ubuntu 14.04 and add --privileged=true to adjust a Docker permissions setting that breaks Swift.

% docker run -t -i --privileged=true ubuntu:14.04 /bin/bash

Then run the following to install some required Ubuntu packages.

apt-get update
apt-get install wget -y
apt-get install clang libicu-dev libedit-dev python-dev libxml2-dev -y

Now we can install a Swift 3.0 preview (look here for the latest).

wget -qO- https://swift.org/builds/swift-3.0-preview-1/ubuntu1404/swift-3.0-preview-1/swift-3.0-preview-1-ubuntu14.04.tar.gz | tar xz

That unpacks the Swift distribution into a directory at the top of your filesystem (Note that you are running inside the Docker image as root).

All that’s left is to set your path, and then you can run Swift!

export SWIFT=swift-3.0-preview-1-ubuntu14.04
export PATH=/$SWIFT/usr/bin:$PATH
swift

Welcome to Swift version 3.0 (swift-3.0-PREVIEW-1). Type :help for assistance.
  1> 2+2
$R0: Int = 4
  2> ^D

Use Control-D to exit Swift, and then enter Control-D again to exit the Docker image. Now you’re back at your Mac’s terminal prompt.

Where did your image go? You can see your running instance with docker ps:

$ docker ps -a
CONTAINER ID        IMAGE                        COMMAND             CREATED             STATUS                      PORTS               NAMES
9944aeb00a5d        ubuntu:14.04                 "/bin/bash"         3 minutes ago       Exited (0) 12 seconds ago                       backstabbing_borg

If you see more than one, look for the one that was created most recently. To save a version of that that you can restart anytime, use docker commit.

% docker commit 9944aeb00a5d swift

After that, you’ll be able to list your local Docker images:

$ docker images
REPOSITORY                   TAG                 IMAGE ID            CREATED             SIZE
swift                        latest              a3f41fe84cbc        3 seconds ago       925.8 MB
... others ...

And now you can run your image anytime with docker run.

% docker run -t -i --privileged=true swift /bin/bash

For more information about Swift on Linux, see the Swift Download Docs.