Swift is a modern, open-source, and powerful programing language developed by Apple Inc in 2014. It is developed to replace the Object-C language which was the primary language for building iOS, iPadOS, macOS, tvOS, and watchOS applications. This language can also be used for other purposes like server-side programming.
In this article, I will discuss how to install and use the Swift programming language in Ubuntu.
Features of Swift programming language
Some of the key features of the Swift programming language are –
- Designed for safety
- Supports closure (or lambdas in other languages ), strings, etc
- Tuples and multiple return values
- Protocol oriented language
- Fast and concise iteration over a range or collection
- Structs that support methods, extensions, and protocols
- Functional programming patterns, e.g., map and filter
- Powerful built-in error handling
Installing Swift in Ubuntu
Follow the given steps to install Swift programming language in Ubuntu.
Update and upgrade your system
Run the following command to update the local package index and upgrade packages to their latest version –
sudo apt update && sudo apt upgrade -y
Install required dependencies
Next, use the following command to install the required dependencies
sudo apt install binutils git gnupg2 libc6-dev libcurl4 libedit2 libgcc-9-dev libpython2.7 libsqlite3-0 libstdc++-9-dev libxml2 libz3-dev pkg-config tzdata zlib1g-dev
Go to the download page of Swift and download the latest binaries for installing Swift on your Ubuntu system.
OR use the following command to download the Swift tarball –
Once the download is completed you need to extract and install it on your system.
Use the following command to extract the downloaded package –
sudo tar -xvzf swift-5.4.3-RELEASE-ubuntu20.04.tar.gz -C ~
Add Swift executable to our PATH environment variable –
echo "PATH=~/swift-5.4.3-RELEASE-ubuntu20.04/usr/bin:$PATH" >> ~/.bashrc
Use the following command to update the PATH variable in currently running shell –
Verify you installation
Now you can verify the installation of swift on your system by checking its version. Run the following command to check the Swift version –
Writing your first Swift program
Swift provides REPL (Read Eval Print Loop) which is an interactive Swift shell. Here you can write and execute any valid Swift statement.
To access this shell run the following command in your terminal –
You can type the given statement to print the Hello World! –
Now you can see the output in the image below.
There are several online resources you can learn and write your code in the Swift programming language.
You have successfully set up Swift programming in your Ubuntu system. For any query please write us in the comments below.