Developing software in the Real World

Compiling Swift on Linux

Swift is open source, which means that we can build it ourselves. This isn't too hard to do, but takes some time.

Set up the dependencies and grab the code

Firstly, you need a lot of memory and as it takes ages, give your VM plenty of CPUs! My Macbook Pro has a quad core process, so I tell Virtualbox that it can use all 4 using this configuration in my Vagrantfile:

You then need all the dependencies inside the VM:

If you want to build the docs, then you also need Sphinx:

Now grab the code from GitHub:

In addition to the core swift repository, you also need a number of other repositories and fortunately, there's a script to do this for us:

Alternatively, if you don't want to do all this manually, you can just grab this Vagrantfile from IBM.

At a later date, if you want to collect the latest changes to the source files just run update-checkout with no arguments:

Introducing build_script

To compile Swift we use a script to do it all for us. This one is called build-script which takes a number of different parameters. Use -h to see what it provides.

In order to successfully build in 8GB of RAM, we need to build without the debug symbols, so we use the -R switch for that. To also run the tests, we need -t and if you want to build Foundation and XCTest, you need to add the switches --xctest and --foundation.

The simplest build command, however, is:

This is a good time to get a cup of tea. On my computer, it takes 55 minutes to build… Once built, the binaries are in build/Ninja-ReleaseAssert/swift-linux-x86_64/bin.

Compiling for usage

When you build with -R, you're building for contributing to the compiler and associated libraries. If you want to build so that you can then use the tool chain for writing Swift applications, then the easiest way is to use preset which creates an installable package:

Again, this takes a while to do!!

Once done, you have a fully working swift package at ~/swift-install.

Test using:

One thought on “Compiling Swift on Linux

  1. Thank you for this. Please also note that rsync is required as well for compiling for usage (used by swiftpm/Utilities/bootstrap).

Leave a Reply

Your email address will not be published. Required fields are marked *