Swift Development with Visual Studio Code — On Linux!

Peter Zignego
Dec 21, 2018 · 3 min read

*Last updated on May 15, 2019*

After reading Mattt’s excellent NSHipster article on building the fledgling Swift Language Server Protocol project, I scrolled back up and stared at Step 0: Install Xcode. It didn’t seem strictly necessary, based on what followed. And as I’m sure you’ve deduced by the title and existence of this post, it’s not. Below is a short step-by-step guide to building and running the Swift Language Server Protocol on linux¹. The following instructions assume either a basic understanding of a unix-style command line and/or a willingness to copy and paste superuser commands from the internet into your terminal program of choice.

  1. Install VS Code by following these instructions: https://code.visualstudio.com/docs/setup/linux
  2. Make sure that you have all the necessary dependencies installed:
$ sudo apt install curl git clang libsqlite3-dev libblocksruntime-dev libncurses5-dev

3. Download the recommended Swift development toolchain (Trunk development master as of this writing) from swift.org or via the command line:

$ curl https://swift.org/builds/development/ubuntu1804/swift-DEVELOPMENT-SNAPSHOT-2019-05-12-a/swift-DEVELOPMENT-SNAPSHOT-2019-05-12-a-ubuntu18.04.tar.gz > ~/swift-DEVELOPMENT-SNAPSHOT-2019-05-12-a-ubuntu18.04.tar.gz

4. Extract the toolchain and move it to /usr/local/bin:

$ sudo tar xzf swift-DEVELOPMENT-SNAPSHOT-2019-05-12-a-ubuntu18.04.tar.gz -C /usr/local/bin

5. Add Swift to your command line path:

$ export PATH=/usr/local/bin/swift-DEVELOPMENT-SNAPSHOT-2019-05-12-a-ubuntu18.04/usr/bin:"${PATH}"

6. Run swift --version to make sure Swift was downloaded and installed correctly. You should see this output (or similar, depending on the version of the toolchain you installed):

Swift version 5.0-dev (LLVM 082dec2e22, Swift 87e1efd1a2)
Target: x86_64-unknown-linux-gnu

7. Clone and build Apple’s sourcekit-lsp project:

$ git clone https://www.github.com/apple/sourcekit-lsp.git
$ cd sourcekit-lsp
$ swift package update
$ swift build -Xcxx -I/usr/local/bin/swift-DEVELOPMENT-SNAPSHOT-2019-05-12-a-ubuntu18.04/usr/lib/swift

8. Move sourcekit-lsp to /usr/local/bin so VS Code can access it.

$ sudo mv .build/x86_64-unknown-linux/debug/sourcekit-lsp /usr/local/bin

9. Install Node.js 11.x or newer:

$ curl -sL https://deb.nodesource.com/setup_11.x | sudo -E bash -
$ sudo apt-get install -y nodejs

10. Generate the extension for VS Code:

$ cd Editors/vscode
$ npm run createDevPackage

11. Install the extension you just created:

$ code --install-extension out/sourcekit-lsp-vscode-dev.vsix

12. Point sourcekit-lsp at your installed toolchain in VS Code’s settings (File > Preferences > Settings > settings.json):

"sourcekit-lsp.toolchainPath": "/usr/local/bin/swift-DEVELOPMENT-SNAPSHOT-2019-05-12-a-ubuntu18.04/"

13. Reload your VS Code window (Control+Shift+P > Reload Window) or restart VS Code.

At this point, you’re all set up to use the Swift Language Server on linux.

One big caveat: on Ubuntu 18.04, opening a project with an existing .build folder causes the LSP to crash on launch. A few other caveats:

• SwiftPM build settings are not updated automatically after files are added/removed.

Workaround: close and reopen the project after adding/removing files

• SourceKit-LSP does not update its global index in the background, but instead relies on indexing-while-building to provide data. This only affects global queries like find-references and jump-to-definition.

Workaround: build the project to update the index

Let’s clone and build a sample project:

$ git clone https://www.github.com/JohnSundell/Splash.git

Now that we’ve cloned the project, open the Splash folder in VS Code to avoid the bug mentioned above.

$ cd Splash
$ swift build

Finally, we can see it in action:

  • Hover over a struct name
  • Command+Click to jump to a definition
  • Type some code and invoke autocomplete

There has been a lot of enthusiasm around the announcement of this project, and for good reason. It serves as yet another reinforcement of Apple’s broad goals for the Swift programming language, which seem to extend further beyond the friendly confines of their own platforms with each passing year.

Originally published at www.bytesized.co on December 21, 2018.

  1. Tested on Ubuntu 16.04 and 18.04. The commands below all specify the 18.04 versions where necessary.

Peter Zignego

Written by

You'll find me hiding behind a pair of sunglasses.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade