Swift is a popular programming language used for developing programs and apps for any of Apple’s devices such as iphone, ipads and Mac. Although it was initially meant for only Mac devices, Apple has recently started supporting Swift language for Linux systems as well. In this article, we will look at how to install Swift in Ubuntu.
How to Install Swift in Ubuntu
Here are the steps to install Swift in Ubuntu. We will look at how to get started with Swift programming language in Linux, and also write a simple “Hello World” program.
1. Download Swift
Apple has already provided snapshots for Ubuntu. You can download it from this link on Apple website. You can also download it using wget command
Ubuntu 14.04
$ sudo wget https://swift.org/builds/ubuntu1404/swift-2.2-SNAPSHOT-2015-12-10-b/swift-2.2-SNAPSHOT-2015-12-01-b-ubuntu14.04.tar.gz
Ubuntu 15.04
$ sudo wget https://swift.org/builds/ubuntu1510/swift-2.2-SNAPSHOT-2015-12-10-a/swift-2.2-SNAPSHOT-2015-12-10-a-ubuntu15.10.tar.gz
2. Extract Downloaded Files
Use tar command to extract the .tar.gz file downloaded above.
$ sudo tar -xvzf swift-2.2-SNAPSHOT*
3. Setup Environment Variables
Once you have extracted files, you need to add the location to its binary file to your system’s environment variable.
cd swift-2.2-SNAPSHOT*
cd usr/bin
pwd
We basically navigate to the usr/bin subfolder inside the extracted folder and run pwd command to get its full path.
Next, include that path in $PATH variable with the following command
export PATH=path_to_swift_usr_bin:$PATH
So if the path to usr/bin folder on your system is
/home/john/swift-2.2-
SNAPSHOT-2021-07-01-a-ubuntu15.10/usr/bin
then run the following command
export PATH=/home/john/swift-2.2-
SNAPSHOT-2021-07-01-a-ubuntu15.10/usr/bin:$PATH
4. Install Dependencies
Run the following command swift dependencies
$ sudo apt-get install clang libicu-dev
5. Verify installation
You can verify installation with the following command. It will display the version of Swift on your system.
$ swift -version
If you run swift command without any options, then it will open swift shell where you can run swift commands. Type :q to exit the shell.
$ swift >
6. Create First Swift Program
Now we will create a very simple Swift application just for testing purposes. Run the following command to create Test directory to hold all required files for our Swift application.
$ sudo mkdir Test $ cd Test
You need to create a Package.swift file in this folder. Every Swift project needs to have this file.
$ sudo touch Package.swift
Create a directory Source inside our project folder and create main.swift file in it.
$ sudo mkdir Sources $ sudo vi Sources/main.swift
Add the following line to print “Hello world” when you run this application.
print("Hello, world")
Save and close the file.
Run the following command to generate the executable file for your application.
$ sudo swift build
You will find the executable in .build/debug/Test. Simply enter the full file path of your executable in terminal to run the program. You will see the output “Hello world”. It is similar to running a shell script from terminal.
$ .build/debug/Hello Hello world
That’s it. In this article, we have learnt how to install Swift application in your system.
Also read:
How to Install Monit on CentOS 7 Linux
How to Install mod_wsgi in Apache
How to Install Memcached in Ubuntu
NGINX Block URL Access
How to Resize Linux Partition without Data Loss
Related posts:
Sreeram has more than 10 years of experience in web development, Python, Linux, SQL and database programming.