Open Source Swift on Ubuntu Linux

Swift is a general purpose, compiled programming language that has been developed by Apple for macOS, iOS, watchOS, tvOS and for Linux as well. Swift offers better security, performance & safety & allows us to write safe but strict code.

As of now, Swift is only available for installation on Ubuntu for Linux platform. So in this tutorial, we are going to discuss how to install Swift on Linux, or to be more precise how to Install Swift on Ubuntu.

Checking Your Ubuntu Version From the Terminal

This will output the version information. 
Once you are at the command line, there is a simple command you can use to find your Ubuntu version number: lsb_release -a

$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 14.04.5 LTS
Release: 14.04
Codename: trusty

Install Dependencies

Three packages are required to be installed on Ubuntu systems to be able to install & use Swift. These packages are ‘clang, libicu-dev, & git’. Install these packages from the terminal using the following command,

$ sudo apt-get install clang libicu-dev -y
sudo apt-get install clang libicu-dev -y

Download the Swift packages

We will need to download Swift packages from the official website. Package for Swift are available for Ubuntu 14.04, 16.04 & 16.10. Mine OS is Ubuntu 14.04. Hence, download the swift package applicable to Ubuntu 14.04.

$ wget https://swift.org/builds/swift-4.1.2-release/ubuntu1404/swift-4.1.2-RELEASE/swift-4.1.2-RELEASE-ubuntu14.04.tar.gz
wget command

Run ls command

to see successful saving of Swift package.

ls command

Extract Package

Now we will extract the downloaded package into a folder in our home directory (you can use another directory),

$ mkdir ~/swift
$ tar -xvzf swift-4.1.2-RELEASE-ubuntu14.04.tar.gz -C ~/swift
tar command

Configuring environment variables

We need to configure PATHvariable in our .basrc file as this will be required to execute the programs. So open .basrc & make the following entry into the file,

$ sudo vi ~/.bashrc
export PATH=~/swift/swift-4.1.2-RELEASE-ubuntu14.04/usr/bin:$PATH

Press ESC +:wq to save file.

That’s it, we now have swift installed & ready to use.

Checking Swift

To make sure that swift has been installed properly, end the session in the Terminal and relaunch the terminal & run the following command to check the swift version,

$ swift –version
Swift version

Creating your Program Using the Swift Package Manager

The package manager defines a simple, common-sense directory structure for any Swift code that you would like to build into an executable or library.

First, execute the following command at the shell prompt to create a directory helloworld-project and dive into it:

$ mkdir helloworld-project && cd helloworld-project
$ swift package init --type executable

The current directory helloworld-project is important when you run swift package init because it becomes the name of the generated package. You will see a few files and folders in the output that were created for you. Take some time to get familiar with the project structure:

Project structure
  1. Package.swift has your package description, and it will also have your package’s dependencies.
  2. Sources/, as the name suggests, is where you will have all your Swift source files. A main.swift file has also been created for you. This will be the entry point for your application. For now, it prints hello, world to the Terminal.
  3. Tests/ will contain unit tests you can write using XCTest. You will write tests for your code soon!

Go back to your Terminal window and run the following:

$ swift build

This will build the package and create an executable at .build/debug/helloworld-project. Execute the app by running:

.build/debug/helloworld-project

You should see Hello, world! printed to the screen.

output of program

Congratulations: you’ve created and built your first Swift package on Linux Ubuntu!

What Apple Hasn’t Released on Linux

That’s quite a pile of functionality — but what hasn’t Apple released?

Well, there’s no Xcode, no AppKit and no UIKit. There’s no Core Graphics, no Core Animation, no AVFoundation, nor many of the other familiar core Objective-C libraries. Basically, most of what you need to create beautiful iOS or Mac apps isn’t here yet.

But what Apple has released is quite significant. Consider the Core Libraries project. They provide higher-level functionality such as networking, file system interaction, date and time calculation. These are brand new, cross-platform, Objective-C-free re-implementations of existing Apple libraries. The fact that the Core Libraries project doesn’t rely on the Objective-C runtime suggests that Apple is creating the underpinnings for Swift to replace Objective-C completely in the long run. And the fact that it’s cross-platform by design suggests Apple is seriously hoping people will use the language for Linux software development — at least for server software, if not for GUI apps.

But the package manager is most significant of all.

Where to Go From Here?

You can create the Library and link with your executable project.

As always, any feedback is appreciated, so feel free to comment down here or reach out on twitter — and, as always,