How to Install Swift on Ubuntu 16.04

To Install Swift on Ubuntu 16.04

Swift is a fast, safe, and modern programming language from Apple and it has an enormous community backing the language. Swift is used primarily for developing iOS and macOS applications, but as of Swift 3, you can use it for server-side application development as well. It is so simple to install Swift on Ubuntu 16.04 and this tutorial covers the ground on the same process. 

 

Installing Swift

First, ensure you have the latest list of packages on your system:

root@linuxhelp:~# apt-get update
Hit:1 http://ppa.launchpad.net/ansible/ansible/ubuntu xenial InRelease
Hit:2 http://in.archive.ubuntu.com/ubuntu xenial InRelease
.
Reading package lists... Done

 

Then install Swift's prerequisites, which include clang and some Python 2.7 components.

root@linuxhelp:~# apt-get install clang libicu-dev libpython2.7
Reading package lists... Done
Building dependency tree       
.
Processing triggers for ureadahead (0.100.0-19) ...

 

After that, you need to download the latest Swift binary. Since it is not available via apt, you can download it manually from the Swift downloads page, or with wget like in the following manner. 

root@linuxhelp:~# wget https://swift.org/builds/swift-4.0-release/ubuntu1604/swift-4.0-RELEASE/swift-4.0-RELEASE-ubuntu16.04.tar.gz
--2018-02-04 05:33:25--  https://swift.org/builds/swift-4.0-release/ubuntu1604/swift-4.0-RELEASE/swift-4.0-RELEASE-ubuntu16.04.tar.gz
.
2018-02-04 05:36:57 (812 KB/s) - ‘swift-4.0-RELEASE-ubuntu16.04.tar.gz’ saved [174827917/174827917]

 

Once it is done, you shall download the signature file for the release you downloaded. 

root@linuxhelp:~# wget https://swift.org/builds/swift-4.0-release/ubuntu1604/swift-4.0-RELEASE/swift-4.0-RELEASE-ubuntu16.04.tar.gz.sig
.
.
2018-02-04 05:54:10 (785 KB/s) - ‘swift-4.0-RELEASE-ubuntu16.04.tar.gz.sig’ saved [819/819]

 

After that, you can install Swift. Execute the following command to extract binary you downloaded earlier. 

root@linuxhelp:~# tar xzf swift-4.0-RELEASE-ubuntu16.04.tar.gz

 

Then add the Swift toolchain to your path so you can run the swift command system-wide.

root@linuxhelp:~# export PATH=swift-4.0-RELEASE-ubuntu16.04/usr/bin:"${PATH}"

 

Entering this command will only add the swift command to your path for your current shell session. To make sure that it is added automatically in future sessions, add it to the .bashrc file.

root@linuxhelp:~# vim ~/.bashrc
Once it is done, you shall add the following line at the end of the file. 
. . .
export PATH=swift-4.0-RELEASE-ubuntu16.04/usr/bin:"${PATH}"
Save and exit the file.


Make sure everything works fine so run the swift command. 

root@linuxhelp:~# swift

You'll be greeted with the Swift REPL, which indicates that everything is working correctly.

Welcome to Swift version 4.0 (swift-4.0-RELEASE). Type :help for assistance.
  1>


Let's double check that everything is working correctly. Enter this program which sums all integers between 1 and 5. Enter each line into the REPL, pressing the ENTER key after each line.

1> var x = 0
x: Int = 0
  2> for i in 1...5 {  
  3.      x += i  
  4. } 
  5> x
$R0: Int = 15

Exit the Swift REPL with CTRL+D.

 

With this, the tutorial on how to install Swift comes to an end. 


Thank you! for using Linux Help.

You find this tutorial helpful? Share with your friends to keep it alive.
For more help topics browse our website www.linuxhelp.com
Be the first to comment, we value your suggestions. For further queries please comment below.

Tags: Ubuntu
user image Author :  Jayce