Develop Server side Swift on Linux

I have for a while now been involved with the server side swift framework Vapor. I am really starting to like Swift as a programming language, there is only one problem… I don’t own a Mac; And because of that I can’t run Xcode.

But luckily there are alternatives. JetBrains, the company behind a lot of popular IDEs (RubyMine, PhpStorm etc.), CLion is a cross-platform IDE originally designed for programming C/C++. JetBrains have built a plugin for CLion providing a lot of Swift love (Auto-complete, Syntax highlighting etc.).

Here i will go through how you would:

  • Install Swift on Linux
  • Setting up CLion with Swift plugin
  • Build Swift and Run your Vapor app with the press of a button

This should get you going with developing Server side swift on Linux. During this post i will refer to Vapor, but should work for other frameworks as well.

This is just a quick walk-through, there is a lot more features, so try to play a bit around with it :)

Install Swift on Linux using APT

If you already know how to do this, you can just skip this step. Installing Swift on Linux is quite simple, thanks to our APT repository. It’s maintained by the Vapor team, and makes installing Swift (and Vapor toolbox really easy)

$ wget -q -O- | sudo apt-key add -
$ echo "deb $(lsb_release -sc) main" | sudo tee /etc/apt/sources.list.d/vapor.list
$ sudo apt-get update
$ sudo apt-get install swift

This will import the GPG key for the repository, add the repository to your system, depending on your distro, and finally installing Swift. If you want to develop Vapor, you can install the Vapor Toolbox, simply by running

$ sudo apt-get install vapor

That’s it. You can validate your installation with

$ swift --version

It should print something like this:

$ swift --version
Swift version 3.1.1 (swift-3.1.1-RELEASE)
Target: x86_64-unknown-linux-gnu

Setting up Swift plugin in CLion

You need to install CLion, you can download a 30 day trial here:

After you have installed CLion, open it up. Open up settings File->Settings

Find Plugins and install the plugin called Swift:

That’s it, easy right? :)

Because CLion isn’t build for Swift, there is one small thing, you will need a CMakeLists.txt file in your project. This can be used to locate your Swift files. I have a Snippet here, you can add, that will add all *.swift files in your project and dependencies. This is required to use auto-complete.

cmake_minimum_required(VERSION 3.7)

file(GLOB_RECURSE swiftFiles *.swift)

SOURCES ${swiftFiles})

Just replace the two occurrences of #PROJECT-NAME# with the name of your project.

When adding new files, you need to reload your CMakeLists file, double tab SPACE and search for reload and select Reload CMake Project this might seem annoying, but you get used to it :)

After this you have full support for Swift including auto-complete example:

Setting up Build and Run Vapor app

Once you need to test your code, you would of cause like to build and run your Vapor app. And luckily this as well is also quite simple in CLion. Click Run->Edit configurations

You should already have a configuration called something like build create another one and call it run

Set it up like this:

Executable is your Vapor Executable from your project dir, default in Vapor 2 is Run (e.g.: .build/release/Run)

And set Working directory to your project dir.

In Before launch click Add and Run another configuration and select your build configuration.

Here you can see build configuration. Find your Swift executable default is: /usr/bin/swift

Program arguments should be build -c release

Now in the top right corner, select run and either press the run button or SHIFT+F10 this will build your Vapor app, and if it succeeds start it up.

Output will automatically be printed out in the window that popup. You can easily hide it. Before rebuilding remember to click the Stop button, to stop the running app.

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.