Jens Klingenberg

How to run Swift 4.0 on Ubuntu 16.04

Posted on July 13, 2017  •  2 minutes  • 257 words
Table of contents

Swift is a programming language developed by Apple Inc. for iOS, macOS, watchOS, tvOS, and Linux at the WWDC 2014. It shares a few similarities with Kotlin, which is now officially supported from Google for Android Development. Because it’s a general purpose language you can even use Swift for Web Development. And because you can use it on linux, i wanted play around with it. Here is my instruction how to run it on Ubuntu 16.04.

Be aware of that you can not build iOS apps with it, because you still need a Mac and XCode.

How to install it?

1) Download a Snapshot

Download a snapshot from Apple of the Swift version that you want to run and the Ubuntu version you are running. I took THIS

2) Extract it

3) Create an environment path

You don't have to do this, but then you can easily run the command "swiftc" without specifying the full path to your swift version.

Open your .bashrc:

sudo nano ~/.bashrc

Add the path:

export PATH="${PATH}":/home/<login>/swift-<version>/usr/bin

You need to change it to the folder that contains your swift version

Save and reload the .bashrc:

source ~/.bashrc

4) Test the compiler

To test if everything is fully working, create a new text file "hello.swift". Write and save in it:
print("Hello World!")

This is a “Hello World” program in Swift

Now open a terminal and run:

swiftc "pathToYourHelloSwiftFile"

If everything works correct the swift compiler will create a file called “hello”.

Screenshot

Run the hello file from a terminal and it will print: Hello World!

Let's connect: