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. 

Tag : Ubuntu
FAQ
Q
Is Swift free to use?
A
Swift is free and open source, and it's available to a wide audience of developers, educators, and students under the Apache 2.0 open source license. We're providing binaries for OS X and Linux that can compile code for iOS, OS X, watchOS, tvOS, and Linux.
Q
What is swift programming language used for?
A
Swift is a general-purpose, multi-paradigm, compiled programming language developed by Apple Inc. for iOS, macOS, watchOS, tvOS, and Linux. Swift is designed to work with Apple's Cocoa and Cocoa Touch frameworks and the large body of existing Objective-C code written for Apple products.
Q
how to check version of swift?
A
run the command as follow
# swift -version
Q
Is there an equivalent to id in Swift?
A
Yes. As mentioned above when an Objective-C API returns id Swift will substitute with AnyObject. The AnyObject type can represent an instance of any class type. There is also Any which can re
Q
How does Swift work with Grand Central Dispatch?
A
The same way, you can use the C APIs as you did in Objective-C. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), { println("test") }); You can also use the hi