Using CLion as an IDE for Server-Side Swift Apps on Linux

One of the best Swift IDEs on Linux is CLion. In this tutorial, you’ll set up CLion for Swift on Linux and build a Swift server app with Vapor. By Andy Pereira.

Leave a rating/review
Download materials
Save for later
Share

When Swift became open source, one of the great additions to the community was that it was ported to Linux. Since then, the community has made many tools and platforms to allow for easy, solid Swift development on Linux.

One question that may come up in your quest to use Swift on Linux: “Which IDE is right?”

While there are several great options on macOS, aside from Xcode, there aren’t many for Linux. The great team over at JetBrains made CLion, a cross-platform IDE that works on macOS and Linux.

In this tutorial, you’ll learn how to get set up with Swift on Linux, setup CLion to work with Swift, and use Vapor to build a Swift server app.

Note: This CLion tutorial assumes you already have a version of Linux installed on a machine, whether natively or in a virtual machine. You should also be familiar with using the command-line.

Getting Started

If you want to follow along with this tutorial, you should use Ubuntu 16.04. If you are using another version of Linux, your mileage may vary, and you may encounter an issue not covered here.

To install Swift on other versions of Linux, follow the instructions on the official Swift website.

Installing Swift

First, for the install and other components of this tutorial to work, open Terminal and run the following command:

sudo apt-get install clang libicu-dev libcurl4-openssl-dev libssl-dev git docker.io
Note: Each time this tutorial refers to Terminal, it means a command prompt on Linux and not the macOS Terminal.

To download Swift 4.2.1, start by navigating to your Downloads directory in Terminal:

cd ~/Downloads

Next, download Swift 4.2.1 with the following command:

wget https://swift.org/builds/swift-4.2.1-release/ubuntu1604/swift-4.2.1-RELEASE/swift-4.2.1-RELEASE-ubuntu16.04.tar.gz

Finally, download the Signature file for Swift:

wget https://swift.org/builds/swift-4.2.1-release/ubuntu1604/swift-4.2.1-RELEASE/swift-4.2.1-RELEASE-ubuntu16.04.tar.gz.sig

If Swift isn’t already installed on your Linux OS, follow the installation instructions found on the page for Linux.

It is important to follow the steps laid out, as they ensure you are using a signed download of the language. Feel free to download and unzip Swift to your desired location. Ensure that you remember where, as you’ll need the path for future steps.

Finally, to make sure you can use Swift even after you restart Terminal or your computer, you need to export your Swift path in your .bashrc file. In Terminal, open the file in gedit:

gedit ~/.bashrc

Scroll to the bottom of the file and paste the following line:

export PATH=~/Downloads/swift-4.2.1-RELEASE-ubuntu16.04/usr/bin:"${PATH}"
Note: If you’re using Ubuntu 18.04, replace the path with the correct folder for the Swift toolchain you downloaded.

Save the file, and quit gedit. Restart Terminal. Ensure Swift is set up by running the Swift REPL in Terminal.

swift

If prompted, enter your password and try running REPL again.

To quit the REPL, type:

:quit

Installing CLion

Open the Ubuntu Software Center, search for CLion and install. This will install a trial version of the software, which should give you plenty of time to complete this tutorial. Once installed, open CLion. You’ll need to click through a few dialogs when opening CLion the first time. If you didn’t purchase CLion, you can choose Evaluate for free when prompted. This will give you 30 days to try out the app.

Setting Up Swift Support

To use Swift in CLion, you’re going to need to install the Swift plug-in. At the bottom of the Welcome screen, go to Configure ▸ Settings. Here, you can configure your settings globally.

Since Swift support is not installed in CLion by default, you’ll need to install a plug-in. In the left-hand column, search for Plugins. Then, search for Swift and select Install. Once this finishes, restart CLion.

Now that the plug-in is installed, you’ll need to configure CLion to know where your Swift toolchain is. From the Welcome screen, select Configure ▸ Settings. In the left-hand column, expand Build, Execution, Deployment. Now, select Swift in the expanded list.

Next, enter the path where you unzipped your Swift toolchain download. Select the Folder icon in the Swift toolchain path field and navigate to the installed directory of Swift. You’ll know when you’ve selected the proper directory when the OK button becomes active. If you installed according to the instructions above, this folder should be in ~/Downloads.

Select OK, and follow the prompts to restart CLion.

Creating Your First Swift App

Now that CLion setup is complete, you’re ready to create your first app!

From the Welcome screen, select New Project. Under Swift choose Swift Package. On the right-hand side, change the path of the project from ending with untitled to Roar. Then, click Create.

Once the project window is open, there are a few things you’ll need to familiarize yourself with. You can expand Roar ▸ Sources ▸ Roar to see where your project files are located. As of now, there is only main.swift.

In the Project window, you’ll also find Package.swift, the manifest file for Swift Package Manager, which manages dependencies for the project. If you need to add any dependencies to your project, you do it here.

In main.swift, you’ll find the actual code for this project. Replace the contents of the file with the following:

class Lion {
  func speak() {
    print("Roar")
  } 
} 

let application = Lion()
application.speak() 

Now, select Run, shaped like a Play button, in the upper-right corner of the screen.

In Messages at the bottom, you’ll see Roar printed to the console.

Adding Files

Since any project you write won’t be contained in one file, you’ll need to know how to add and debug files. If you’re used to Xcode, things will be slightly different, but still easy.

First, expand your project’s files in the left-hand side of CLion, then expand Sources. Right-click on Roar and choose New ▸ Swift Type

In Name, enter Lion and select OK.

Replace the contents of Lion.swift with the following:

import Foundation

class Lion {
  func speak(_ text: String) {
    print(text)
  } 
}

Now, replace the contents of main.swift with the following:

let application = Lion()
application.speak("Roar")

Then, select Run and you should see the same result as before.