How To Use Swift Programming Language In Ubuntu Linux

Getting started with Swift in Linux

Apple has announced that its programming language Swift will be open source with ports for Linux. This was as big a news for Linux users as Microsoft open sourcing .net. If you are itching to know about using Swift in Linux, then I have a basic tutorial for you.

This tutorial will help you in getting started with Swift in Linux. Tutorial includes installation of Swift in Linux and help set up the environment for writing the first “Hello World” program in Swift.

Things to note here is that at the time of writing this article, Swift for Linux is still under heavy development. You might experience issues every now and then. I am using Ubuntu 15.10 for this tutorial, but you can use the same steps for Ubuntu 14.04 as well.

Installing Swift in Ubuntu Linux

Step 1: Download the files

Apple has provided snapshots for Ubuntu. You can download the files from the link below. Files are around 90 MB in size.

Download Swift for Ubuntu

Alternatively, you can use the commands below to download Swift for respective Ubuntu versions. I strongly advise to use the link above because the files are being regularly updated and the links in the command below might become outdated soon.

For Ubuntu 14.04, use the command below:

wget https://swift.org/builds/ubuntu1404/swift-2.2-SNAPSHOT-2015-12-10-b/swift-2.2-SNAPSHOT-2015-12-01-b-ubuntu14.04.tar.gz

For Ubuntu 15.04, use this command below:

wget https://swift.org/builds/ubuntu1510/swift-2.2-SNAPSHOT-2015-12-10-a/swift-2.2-SNAPSHOT-2015-12-10-a-ubuntu15.10.tar.gz

Step 2: Extract the files

In the terminal, switch to Downloads directory using the command below:

cd ~/Downloads

And in here, extract the downloaded file:

tar -xvzf swift-2.2-SNAPSHOT*

This will create a directory named swift-2.2-SNAPSHOT-2015-12-10-a-ubuntu15.10 (or something similar).

Step 3: Set up environment variables

Once you have extracted the files, time to set up the path to binaries so that you could execute the programs.

There is a usr/bin directory in your extracted directory. We need to add this path to PATH environment variable. For this you would need the ‘absolute path’ to the extracted directory.

Considering that you have followed the exact steps I mentioned, your extracted files would be in /home/Downloads location. Just for the sake of beginners, I advise you to do this:

cd ~/Downloads/swift-2.2-SNAPSHOT*
cd usr/bin

The result of pwd will give you the exact location that we will be using. So, when you have the path to bin directory, add them to the PATH variable like this. Do change the value of path_to_swift_usr_bin in the command below.

export PATH=path_to_swift_usr_bin:$PATH

This screenshot will help you to understand the procedure:

Installing Swift in Linux

I know if you have even a little bit of Linux command line experience, you will find these instructions boring and tedious, but it might help someone who is a complete noob to command line in Linux.

Step 4: Install dependencies

You’ll have to install a few dependencies to make Swift work in Linux. Use the command below to install them. The download size be around 260 MB.

sudo apt-get install clang libicu-dev

Step 5: Verify the installation

Use the command below to see if Swift is installed:

swift -version

The result should be something like:

Swift version 2.2-dev (LLVM 7bae82deaa, Clang 53d04af5ce, Swift 5995ef2acd)
Target: x86_64-unknown-linux-gnu

Writing programs in Swift in Linux

Once you have everything ready, it’s time to write a simple program perhaps.

1. Using REPL

Swift comes with its own interactive shell, REPL, where you can run a few commands. This is good if someone wants to verify Swift code quickly.

If you just run swift, it will launch the REPL and you can definitely print your “Hello World” here. Take a look at this screenshot for example:

Swift REPL example

You’ll have to use :q to get out of the shell.

But this REPL shell is not enough to build applications. That’s a different procedure.

2. Building a sample Swift project

This part will show you how to make a Swift project that prints Hello World. After all, most of the programming language tutorials start with Hello World, isn’t it?

In the directory of your choice, create a Hello directory and switch to it:

mkdir Hello
cd Hello

We have just created a Swift package named Hello. But it is not complete yet as there each package must have Package.swift file. Create this file using this command:

touch Package.swift

You need to have a Sources directory with main.swift file in it.

mkdir Sources
touch Sources/main.swift

Now, edit this main.swift file. You can use a graphical text editor or do it in command line. All you need to put the following line in the file:

print("Hello, world")

Now use the below command to compile it:

swift build

This will give you the executable program in .build/debug/Hello. Run the program and you will see Hello, world printed in the output:


Screenshot to give you an idea:

Swift language in Linux

Next step

Well, as I had said in the beginning, this tutorial is not going to teach you Swift programming. This is more of getting started with Swift in Linux with configuration and settings.

Unlike Xcode, you won’t get to use Playground to easily create iOS apps. In fact, there is no decent IDE for Swift in Linux yet. You can use Atom for programming and debugging Swift but it’s not the same experience as Xcode.

Since you have just made your first program on Swift in Linux, I advise you to follow other tutorials and documentations on official Swift website.

Similar Posts

  • This introduction may be more up to date:

    The commands
    $ swift package init
    $ swift package init –type executable
    are especially helpful, because they create a directory structure and fill Package.swift and other files with sensible defaults.