Table of Contents
In this article, I will take you through the steps to install Swift Programming Language on Ubuntu 20.04 LTS. Swift is a general-purpose, multi-paradigm, compiled programming language developed by Apple Inc. and the open-source community. Initially it was developed as a replacement for Apple earlier programming language called Objective C but since 2015, it was made as open source software under Apache License 2.0. Now it can be used for both Apple as well as Linux Platform.
Swift provides a modern approach to safety and performance which makes it a easy choice for Developers to create range of mobile and desktop apps. There are multiple different ways to install swift programming language on Ubuntu 20.04 LTS but here we will see the most simplest and recommended approach. So without any further delay, let's dive right in and see the steps to install swift programming language on below section.
How to Install Swift Programming Language on Ubuntu 20.04 LTS
Also Read: How to Install Grafana Monitoring Tool on Ubuntu 20.04 LTS [Best Steps]
Step 1: Prerequisites
a) You should have a running Ubuntu 20.04 LTS
Server.
b) You should have sudo
or root
access to run privileged commands.
c) You should have apt, tar and wget utility installed in your Server.
Step 2: Update Your Server
In the very first step, you need to update your Server by using apt update
command. This will download and install all the latest available versions from configured Ubuntu Repo.
NOTE:
root
user to run all the below commands. You can use any user with sudo
access to run all these commands. For more information Please check Step by Step: How to Add User to Sudoers to provide sudo
access to the User.root@localhost:~# apt update
Hit:1 https://apt.releases.hashicorp.com focal InRelease
Hit:2 http://ppa.launchpad.net/ansible/ansible/ubuntu focal InRelease
Hit:3 https://packages.microsoft.com/repos/edge stable InRelease
Hit:4 http://apt.postgresql.org/pub/repos/apt focal-pgdg InRelease
Hit:5 http://in.archive.ubuntu.com/ubuntu focal InRelease
Hit:6 http://in.archive.ubuntu.com/ubuntu focal-updates InRelease
Hit:7 http://in.archive.ubuntu.com/ubuntu focal-backports InRelease
Hit:8 http://ppa.launchpad.net/micahflee/ppa/ubuntu focal InRelease
Get:9 http://security.ubuntu.com/ubuntu focal-security InRelease [114 kB]
Hit:10 https://packages.grafana.com/oss/deb stable InRelease
Get:11 http://security.ubuntu.com/ubuntu focal-security/main amd64 DEP-11 Metadata [29.0 kB]
Get:12 http://security.ubuntu.com/ubuntu focal-security/universe amd64 DEP-11 Metadata [63.9 kB]
Get:13 http://security.ubuntu.com/ubuntu focal-security/multiverse amd64 DEP-11 Metadata [2,464 B]
Step 3: Download Swift Package
You can go to Swift official Download Page and download the latest swift package using wget
utility as you can see below. This will download the package in local system directory. If you want to ignore all the below output then you can use -q
option to run it quietly and to just show the status at the end.
root@localhost:~# wget https://download.swift.org/swift-5.5.1-release/ubuntu2004/swift-5.5.1-RELEASE/swift-5.5.1-RELEASE-ubuntu20.04.tar.gz
--2021-10-28 12:38:30-- https://download.swift.org/swift-5.5.1-release/ubuntu2004/swift-5.5.1-RELEASE/swift-5.5.1-RELEASE-ubuntu20.04.tar.gz
Resolving download.swift.org (download.swift.org)... 2403:300:a06:f000::3, 2403:300:a06:f000::1, 17.253.83.203, ...
Connecting to download.swift.org (download.swift.org)|2403:300:a06:f000::3|:443... failed: Connection timed out.
Connecting to download.swift.org (download.swift.org)|2403:300:a06:f000::1|:443... failed: Connection timed out.
Connecting to download.swift.org (download.swift.org)|17.253.83.203|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 578403385 (552M) [application/octet-stream]
Saving to: ‘swift-5.5.1-RELEASE-ubuntu20.04.tar.gz’
swift-5.5.1-RELEASE-ubuntu20.04.tar.gz 100%[============================================================================>] 551.61M 11.1MB/s in 52s
2021-10-28 12:43:45 (10.7 MB/s) - ‘swift-5.5.1-RELEASE-ubuntu20.04.tar.gz’ saved [578403385/578403385]
Step 4: Extract Package
In the next step you need to extract the swift package using tar utility. But first you need to create a directory called swift
under /usr/share
path using mkdir /usr/share/swift command as shown below. You can choose any location to store your extracted swift package files based on your requirement.
root@localhost:~# mkdir /usr/share/swift
Then extract the package using tar -xvf swift-5.5.1-RELEASE-ubuntu20.04.tar.gz -C /usr/share/swift command as shown below.
root@localhost:~# tar -xvf swift-5.5.1-RELEASE-ubuntu20.04.tar.gz -C /usr/share/swift
swift-5.5.1-RELEASE-ubuntu20.04/usr/
swift-5.5.1-RELEASE-ubuntu20.04/usr/bin/
swift-5.5.1-RELEASE-ubuntu20.04/usr/bin/swift-api-extract
swift-5.5.1-RELEASE-ubuntu20.04/usr/bin/swift-api-digester
swift-5.5.1-RELEASE-ubuntu20.04/usr/bin/llvm-cov
swift-5.5.1-RELEASE-ubuntu20.04/usr/bin/swift-symbolgraph-extract
swift-5.5.1-RELEASE-ubuntu20.04/usr/bin/ld.lld
swift-5.5.1-RELEASE-ubuntu20.04/usr/bin/repl_swift
swift-5.5.1-RELEASE-ubuntu20.04/usr/bin/plutil
swift-5.5.1-RELEASE-ubuntu20.04/usr/bin/swift-package
swift-5.5.1-RELEASE-ubuntu20.04/usr/bin/lld
swift-5.5.1-RELEASE-ubuntu20.04/usr/bin/swift-build-tool
swift-5.5.1-RELEASE-ubuntu20.04/usr/bin/clang-10
swift-5.5.1-RELEASE-ubuntu20.04/usr/bin/sdk-module-lists/
Step 5: Export Binary Path
To enable swift command line tools, you need to export the binary path to current logged in user .bashrc
file using below export
command. This will enable the system wide visibility of swift command line tools. Then refresh .bashrc
file using source ~/.bashrc
command.
root@localhost:~# echo "export PATH=/usr/share/swift/swift-5.5.1-RELEASE-ubuntu20.04/usr/bin/:$PATH" >> ~/.bashrc root@localhost:~# source ~/.bashrc
Step 6: Check Swift Version
If you want to confirm the installation of swift package, then you need to use swift --version
command. As you can see, current installed version is 5.5.1
.
root@localhost:~# swift --version
Swift version 5.5.1 (swift-5.5.1-RELEASE)
Target: x86_64-unknown-linux-gnu
Step 7: Run a Sample Program
It is now time to write a sample program using swift programming language. You can simply run swift
on the terminal to open the prompt. Then we are displaying Hi, This is from CyberitHUB
on the output using a print
statement. Once you are done, you can press Ctrl+D
to exit the prompt.
root@localhost:~# swift Welcome to Swift version 5.5.1 (swift-5.5.1-RELEASE). Type :help for assistance. 1> print("Hi, This is from CyberitHUB!") Hi, This is from CyberitHUB! 2>
Totally gonna try this on my Ubuntu 20.04 box! Thank you!
I really want to try Vapor and Kitura.