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
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:
For Ubuntu 15.04, use this command below:
Step 2: Extract the files
In the terminal, switch to Downloads directory using the command below:
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:
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.
This screenshot will help you to understand the procedure
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:
The result should be something like:
Swift version 2.2-dev (LLVM 7bae82deaa, Clang 53d04af5ce, Swift 5995ef2acd)
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
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:
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:
You need to have a Sources directory with main.swift file in it.
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:
Now use the below command to compile it:
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
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.
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.