Swift for Linux, on a Mac

This is what a computer linux hacker looks like
This is what a computer linux hacker looks like.

I’ve been working with Swift for Linux, as part of a bunch of teaching material, as well as some conference talks that we’re working on. It’s not super easy to figure out, from searching, the best way to install Swift and Linux, if you’re a Mac user who wants to have a go with it. Here’s what I’ve found.

As it turns out, after extensive research, my feeling is that the best way to run Swift on Linux (on a Mac) is using Vagrant and VirtualBox. I’ve looked at a variety of options, including setting it up manually in a VM, using Docker for Mac, and so on, but this turned out to be the easiest way to do it, and maintain it.

To get Swift on Linux running, on your Mac:

  1. Download and install VirtualBox.
  2. Download and install Vagrant.
  3. Make sure you have Git installed, and clone the following repository: https://github.com/IBM-Swift/vagrant-ubuntu-swift-dev.git
  4. Once you’ve cloned the repository, change directory into it: vagrant-ubuntu-swift-dev
  5. Run the command: vagrant up
  6. Wait. The vagrantfile included in the repository you cloned, which tells Vagrant what to do, downloads Ubuntu 15.10, the Swift prerequisites, the prerequisites for libdispatch, the Swift concurrency library, the Sphinx documentation system, and then clones the Swift repository and creates a script that allows you to build Swift. (This might take a while, and will download a few gigabytes of stuff.)
  7. Once Vagrant is done, you can run the following command to connect to the Linux installation: vagrant ssh
  8. Then, once in Linux, run the following script to build Swift: /vagrant/swift-dev/swift/utils/build-script (This might also take a while, depending on the speed/capabilities of your computer.)
  9. You can then run the following command to verify Swift is up and running: swift –version
  10. You can then create some .swift files, and compile them with the swiftc command.

Easy! Hope that was helpful to someone.