Trying new Swift features on Linux via VS Code dev containers

With every WWDC come new and exciting Swift features and we’re well trained to download the latest Xcode beta to try them all out. Sometimes, however, downloading Xcode isn’t enough, because features might have dependencies on the macOS version as well: you also need to be running on the latest macOS beta.

This of course poses a problem, because no one ever installs beta software on their devices, especially not just for playing around. 😬

What if there was a safer and faster way to get to the shinies?

There actually is: Linux via VS Code dev containers.

You need three bits of software to do this, some of which you likely already have:

Install the required software

Install Docker for Mac and VS Code via the links above. The Swift language extension is just a couple of clicks away after you launch VS Code:

Now we’re ready to set up a development container. This will allow us to launch the current project inside a docker container. There are more detailed instructions online , but here is the gist of it:

  • Set your folder up as a dev container
  • Open your project in a container
  • Edit your source file and run via the built-in terminal

Set up a dev container

Setting up a folder for use inside a dev container is quite simple. Open a folder in VS Code and then create a file .devcontainer/devcontainer.json with the following content :

{
    "name": "swift-nightly",
    "image": "swiftlang/swift:nightly-focal",
    "extensions": [
      "sswg.swift-lang",
    ],
    "settings": {
      "lldb.library": "/usr/lib/liblldb.so"
    }
}

Next, click on the green little button in bottom left corner of the window and then choose “Reopen in Container” from the command bar:

Depending on your network, it might take quite a while to download the Swift docker image and to finish reopening the window. However, unless you clear your docker image cache, this will be a one-time process.

Once the project has reopened in the container, you are running inside a Linux VM and VS Code’s terminal will give you a shell where you can run Swift commands as usual:

Any files you create in the project folder are on your Mac but also visible inside the container.

The Swift language extension has quite a few more tricks up its sleeve (running tests and launching the debugger for instance) and thanks to it, VS Code is quite a capable Swift IDE.

I hope this is helpful to getting started with Swift 5.7 on Linux and the exciting new things introduced at WWDC ’22.

If you have any questions or comments, please get in touch!