Friday, November 23, 2018

Learn Swift on Ubuntu: install and compile

Swift is a new language developed by Apple. Recently, I am studying Swift so that I can build iOS or macOS apps. In this post, I will go over how to setup Swift environment on Ubuntu 18.04 LTS.

First, install clang
$ sudo apt-get update && sudo apt-get install clang -y

Next, download Swift from Swift.org
$ wget https://swift.org/builds/swift-4.2.1-release/ubuntu1804/swift-4.2.1-RELEASE/swift-4.2.1-RELEASE-ubuntu18.04.tar.gz

Decompress
$ tar xfz swift-4.2.1-RELEASE-ubuntu18.04.tar.gz

Add swift binary directory to your PATH environment
$ export PATH=/path/to/swift/usr/bin:${PATH}

This is it for setting up the environment. Let's now build "hello world" in Swift.

Create hello.swift with the following content:
print("hello world")

To run your code, simply run
$ swift hello.swift
hello world

To compile and create a binary, run
$ swiftc hello.swift
./hello
hello world

That's it for this post. Happy hacking!

No comments:

Post a Comment