Swift Programming on Linux
The steps below walk you through the process of downloading, installing & using the Swift Programming language on Linux. For this I will be using Ubuntu Linux 14.04.3 LTS version.
Setup & Configuration
- Downloading Swift
The first step is to download Swift from the link given below. Select the correct version of the OS.
https://swift.org/download/#latest-development-snapshots - Install clang
The next step is to get hold of clang which is a compiler front end for C,C++,Objective-C & Objective-C++. For more information on clang:
https://en.wikipedia.org/wiki/Clang
http://clang.llvm.org
To install clang run the command:
sudo apt-get install clang - Extract the Swift files you downloaded and place them in a folder of your choice.
- Next we will add swift to our path. Do that by running the command:
export PATH=/path to your swift folder/usr/bin/:”${PATH}” - Verify whether it works by trying the following 2 commands
which swift
swift –version
Testing the REPL
- Next we try the REPL for swift. To invoke this just type the command:
swift - Write something like let myName : String = “Swift Code”
and then hit enter. - Follow it by print(myName) and then hit enter.
- You should see the output printed. This is a nice way to test individual Swift statements.
Creating a Single File Project
- To create a file based project:
- Create a folder of your choice, lets call it Hello World:mkdir HelloWorld
- Create a manifest file for the Package with the command: touch Package.swift
- The source files for all your projects must be in the Sources folder, let’s create that: mkdir Sources
- Create a file called main.swift using an editor of your choice. Place the statement print(“Hello, World”) in it.
- Step out of the sources folder and then run the command to build the project: swift build
- The executable will be inside a hidden folder called .build/debug
- The name of the executable will be the same as the name of the project folder.
- To run it simply type: .build/debug/HelloWorld
Creating a multi file project
- Create a folder of your choice, lets call it Hello World:mkdir CentigradeToFahrenheit
- Create a manifest file for the Package with the command: touch Package.swift
- The source files for all your projects must be in the Sources folder, let’s create that: mkdir Sources
- Create a file called converter.swift using an editor of your choice.
- Write the following code in it.:
//note the code below is for demonstrating multi file projects & may not necessarily be accurate or correct
func centigrade_to_fahrenheit(temperatureInCentigrade : Float) -> Float
{
return ((temperatureInCentigrade*9.0/5.0)+32.0)
}
func string_to_float(input : String) -> Float
{
var number : Float = 0.0;
var result : Float = 0.0
for charac in input.characters
{
switch charac
{
case “0”:
number = 0.0;
case “1”:
number = 1.0;
case “2”:
number = 2.0;
case “3”:
number = 3.0;
case “4”:
number = 4.0;
case “5”:
number = 5.0;
case “6”:
number = 6.0;
case “7”:
number = 7.0;
case “8”:
number = 8.0;
case “9”:
number = 9.0;
default:
break
}
result = (result * 10.0) + number;
}
return result
} - Create a second file called main.swift
- Write the following code in it:
if Process.arguments.count != 2
{
print(“USAGE: centigradeToFahrenheit 33.4”)
print(“You are missing an argument”)
}
else
{
let temperatureInCentigrade = string_to_float(Process.arguments[1])
print(“\(temperatureInCentigrade) is equal to \(centigrade_to_fahrenheit(temperatureInCentigrade))”)
} - Step out of the sources folder and then run the command to build the project: swift build
- The executable will be inside a hidden folder called .build/debug
- The name of the executable will be the same as the name of the project folder.
- To run it simply type: .build/debug/CentigradeToFahrenheit 100
- Try with different input values and no input value.
- That’s it. You are now ready to start typing code in Swift.
Pingback: UPDATE: Swift on Linux | Arun Patwardhan's Blog
A few points to note:
– It is not possible to develop iOS/macOS/watchOS/tvOS Apps on Linux as the necessary SDKs are not available as of now
– This article has been updated. Visit the link: https://arunpatwardhan.com/2018/03/27/update-swift-on-linux/ to view the changes.