Swift is a programming language developed by Apple Inc. It is a general-purpose, multi-paradigm, compiled programming language developed for iOS, macOS, watchOS, tvOS, Linux, and z/OS. According to the developers, Swift is a fantastic way to write software, whether it’s for phones, desktops, servers, or anything else that runs code. It’s a safe, fast, and interactive programming language that combines the best in modern language thinking with wisdom from the wider Apple engineering culture and the diverse contributions from its open-source community. The compiler is optimized for performance and the language is optimized for development, without compromising on either.
In this article, we will explain how you can install the latest version of Swift on Ubuntu. More importantly, we will tell you how to get started with Swift by explaining how to write your first Swift program and also create a Hello World project in Swift.
We have run the commands and procedures mentioned in this article on a Ubuntu 18.04 LTS system.
All the Swift releases can be found through the following webpage:
Here, we will describe the installation of Swift version 5.0.1, all through the command line.
Installing the pre-requisites
Open your Terminal application either through the system Application Launcher Search or through the Ctrl+Alt+T shortcut.
The next step is to update your system’s repository index through the following command:
$ sudo apt-get update
This helps you in installing the latest available version of software from the Internet. Please note that only an authorized user can add, remove and configure software on Ubuntu.
$ sudo apt-get upgrade
The first prerequisite that we will install is the Clang compiler. You can do so by running the following command as sudo:
$ sudo apt-get install clang
The system might ask you the password for sudo and also provide you with a Y/n option to continue the installation. Enter Y and then hit enter; the software will be installed on your system. The process may, however, take some time depending on your Internet speed.
Swift also needs a few libraries to work swiftly. Run the following commands to add the required libraries to your system.
$ sudo apt-get install libcurl3 libpython2.7 libpython2.7-dev
You are now ready to install Swift.
We have decided to install Swift version 5.0.1 on our Ubuntu. Let us first download it to our system by running the following command:
$ wget https://swift.org/builds/swift-5.0.1-release/ubuntu1804/swift-5.0.1-RELEASE/swift-5.0.1-RELEASE-ubuntu18.04.tar.gz
Tip: Instead of typing the command, you can copy it from here and paste in the Terminal by using the Ctrl+Shift+V, or by using the Paste option from the right-click menu.
The tar.gz installation package will be downloaded to your Home folder.
Run the following command in order to extract the tar.gz file:
$ tar xzf swift-5.0.1-RELEASE-ubuntu18.04.tar.gz
And then, move the extracted folder to the /usr/share/swift directory through the following command:
$ sudo mv swift-5.0.1-RELEASE-ubuntu18.04 /usr/share/swift
The next and the final step is to configure the Swift binary to the PATH environment variable of your Ubuntu. Run the following commands to do so:
$ echo "export PATH=/usr/share/swift/usr/bin:$PATH" >> ~/.bashrc
$ source ~/.bashrc
Verify Swift Compiler installation
Swift is now installed and configured on your system. In order to verify that the software is indeed installed on your system, you can check it’s version number through the following command:
$ swift --version
Running the Swift REPL
To begin with, you can run some interactive commands in the Swift REPL(Read Eval Print Loop). This REPL helps you in various ways:
- To run and verify some Swift code quickly
- For learning purposes
- Finding new and unique features and test them in a command-line console
Run the following command to get to the Swift console:
Here is an example of how I assigned my name to a variable and then printed it along with a string:
You can use the “:q” command in order to quit the Swift REPL.
The REPL is a good way to run a few small programs but to build an application, you need to be introduced to the Swift projects and packages. Please have a look below to get started with a Swift Project.
The Swift Hello World Project
Now we will introduce you to the simplest Hello world project that you can create and build through Swift. Please follow the steps below for a project that simply prints “Hello, World!” to your screen.
1. Create a project directory through the mkdir command and then switch to it. I am creating a directory by the name of “Hello_swift”
$ mkdir Hello_swift
$ cd Hello_swift
2. Now, run the following command to create an executable package:
$ swift package init --type executable
The command created a hierarchy of the basic files needed for a Swift project. The most important ones are:
The Package.swift file that looks like this:
And, the main.swift file located in the Sources folder that looks like this:
This main.swift file already contains the required code to print hello world. You can, of course, edit this file to make your project do something else.
3. Compile this project by running the following command:
$ swift build
Now you have an executable program in the .build/debug folder having the same name as your Swift project.
4. You are now ready to run the project by running the following command:
Here is the output of my Swift program:
In case of error:
In case you get the following error on running the “swift build” command,
Run the following command in order to install libcurl:
$ sudo apt-get install libcurl4-openssl-dev
This was all you needed to get started with the Swift programming. Luckily, there is a lot of documentation and tutorials available both through the official website and otherwise, to help you build productive applications through Swift.