Developing for Ubuntu on Windows WSL

I managed to install swift on Ubuntu WSL but I was not able to get an IDE working properly. Does anyone know how to configure Visual Studio Code so the sourcekit lsp works on WSL?

I haven't tried it yet for Swift but you could try the new Remote Development extension pack from Microsoft. It works well with Rust on WSL. The extension should also work with Visual Studio Code on macOS and a Ubuntu Docker container.

1 Like

Hi @martini, I have tried that and I think I could make it work if I spent enough, time. The real issue is getting the linting from sourcekit-lsp installed in windows or WSL so I can actually use VS code as an editor with syntax/type checking, autocomplete etc.

Manually installing the latest Swift toolchain in Ubuntu is more work than it could be, so i didn't try that (why not an APT repository?). Using a Swift container is easier if you already have Docker set up but SourceKit-LSP needs at least Swift 5.1 and the latest docker image only has 5.0. So unless someone is really interested it's probably better to wait a while.

1 Like

Hi @martini, installing swift and lsp in Ubuntu isn't a problem, but I don't know how to make the visual studio in Windows use the remote lsp.

If i made a docker image with swift 5.1 and push to the registry so you just need to do a pull, how confident are you that you could make lsp work on it?

So i have installed the latest Swift toolchain in WSL and compiled SourceKit-LSP with it. I have to say it all works very well! Above expectations really. When SourceKit-LSP is stable this will be an excellent option for Swift server-side development.

A few tips to get it working:

  • SourceKit-LSP needs the development headers for SQLite and ncurses. Install them on Ubuntu with sudo apt install libsqlite3-dev libncurses5-dev

  • Install the unofficial SourceKit-LSP extension by Pavel Vasek from the marketplace. When you are editing remotely in WSL the option to install the extension on WSL appears. VS Code then copies the extension to WSL.

  • Edit the locations of the sourcekit-lsp executable and the Swift toolchain directory in the settings of the extension. Use full paths e.g. /home/martini/sourcekit-lsp/.build/debug/sourcekit-lsp

If you get stuck or have a question don't hesitate to ask.

1 Like

Thanks, that looks extremely helpful. I have installed all the necessary packages, then I also installed Swift 5.1 dev version (as you said 5.1 is needed to build sourcekit) but it seems I'm missing something as I'm getting the following error when I do swift package update inside sourcekit-lsp/:

/usr/share/swift/usr/bin/swift-package: error while loading shared libraries: /home/buildnode/jenkins/workspace/oss-swift-5.1-package-linux-ubuntu-18_04/build/buildbot_linux/foundation-linux-x86_64/Foundation.dir/libFoundation.so: cannot open shared object file: No such file or directory

I have been googling around for a while with no luck. Any help would be appreciated.

I've been using swift-DEVELOPMENT-SNAPSHOT-2019-07-28-a-ubuntu18.04.tar.gz. Try that one. It's a snapshot from the master branch. I think you have a snapshot from the earlier 5.1 branch. SourceKit-LSP was cloned from master (rev 91a4532).

1 Like

That worked flawlessly, thanks a lot @martini. Is there any way to run the code apart from using Run in Terminal - Visual Studio Marketplace? Not sure if it's a silly question, never used VS code before this.

I'm not really a VS Code expert either. Often i just open the integrated terminal with Ctrl+` and type swift run, swift build etc. Commonly used commands and tools can be automated with the tasks system. It's more DIY than an IDE but also flexible and works for many programming languages.

1 Like

Great, that answers just about everything. I understand now why there's a lack of information on how to do this, it wasn't obvious to me that sourcekit-lsp is still in alpha from looking at their github page and depending on a development version of swift to build their master branch seems strange. The server side swift ecosystem is also much smaller and less developed than I thought when I started this out.

With that being said I didn't expect to get this working any time soon when I made this post given the lack of tutorials, and it works reasonably well. I'm optimistic about the future once sourcekit-lsp is stable and I think good guides on how to set up a server side swift development environment on windows are indispensable to get people to try it out, so I will contribute back to the community by editing this post (or creating a new one, I'm open to suggestions) with a how-to guide on setting it up once I have had some time to try it out.

Once again thanks @martini for all your help.

1 Like

I really tried to make this work and for a bit it kind of did but I have given up for now. When I tried to change to a new directory in VS Code with remote interpreter it crashed my PC. It now crashes it every time I try to start VS Code. I know this probably isn't Swift's fault but it was the straw that broke the camel's back since this has been far from a pleasant experience setting up.

The only thing that will keep me coming back is how nice the language looks and I feel like it has a lot of potential with few compromises, but I've decided to wait a few months or years until the environment feels more mature.

Thanks to everyone who helped me and good luck advancing this further. I'll be keeping an eye out.

1 Like