October 21, 2021
How to install Swip programming language on Ubuntu 20.04 LTS

How To Install Swift On Ubuntu 20.04 LTS

On this tutorial we will learn how to install Swift programming language version 5.4.3 on Ubuntu 20.04 LTS operating system.

Introduction

Swift is a general-purpose programming language built using a modern approach to safety, performance, and software design patterns. Swift was developed by Apple as a replacement for the older Objective-C language. Swift enables developers or programmers who want to develop applications for macOS or iOS from Linux. On this tutorial, we will learn how to install the latest version of Swift (version 5.4.3 which was released on September 09, 2021) on Ubuntu 20.04 operating system.

Swift Installation On Ubuntu 20.04

On this tutorial we will examine how to install Swift version 5.4.3, the installation will be consist of several steps, namely :

  1. Prerequisite
  2. Install Swift Dependencies Libraries
  3. Download Swift Programming language
  4. Add to system PATH enviroment variable
  5. Verify Swift Installation

1. Prerequisite

Prerequisite for Swift installation on Ubuntu 20.04 LTS:

  • Ubuntu 20.04 LTS system
  • root or an ordinary user with sudo privilege

2. Install Swift Dependencies Libraries

2.1 System Update

By updating our system, it will refresh the local list of available packages software to the newer stable version. To update Ubuntu 20.04 system repositories, we will use the command line:

sudo apt update

2.2 Install Dependencies Libraries

There are any dependencies libraries to be installed first, to run Swift programming language. We will install Swift dependencies by submitting command line:

sudo apt install clang libpython2.7 libpython2.7-dev
mpik@app01:~$ sudo apt install clang libpython2.7 libpython2.7-dev
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages were automatically installed and are no longer required:
  libevent-core-2.1-7 libevent-pthreads-2.1-7 libmecab2 libzip5 linux-headers-5.4.0-26
  linux-headers-5.4.0-26-generic linux-image-5.4.0-26-generic linux-modules-5.4.0-26-generic
  linux-modules-extra-5.4.0-26-generic mecab-ipadic mecab-ipadic-utf8 mecab-utils
Use 'sudo apt autoremove' to remove them.
The following additional packages will be installed:
  binfmt-support clang-10 lib32gcc-s1 lib32stdc++6 libc6-i386 libclang-common-10-dev libclang-cpp10 libclang1-10
  libffi-dev libllvm10 libncurses-dev libobjc-9-dev libobjc4 libomp-10-dev libomp5-10 libpfm4 libtinfo-dev libz3-4
  libz3-dev llvm-10 llvm-10-dev llvm-10-runtime llvm-10-tools python3-pygments
Suggested packages:
  clang-10-doc ncurses-doc libomp-10-doc llvm-10-doc python-pygments-doc ttf-bitstream-vera
The following NEW packages will be installed:
  binfmt-support clang clang-10 lib32gcc-s1 lib32stdc++6 libc6-i386 libclang-common-10-dev libclang-cpp10
  libclang1-10 libffi-dev libllvm10 libncurses-dev libobjc-9-dev libobjc4 libomp-10-dev libomp5-10 libpfm4
  libpython2.7 libpython2.7-dev libtinfo-dev libz3-4 libz3-dev llvm-10 llvm-10-dev llvm-10-runtime llvm-10-tools
  python3-pygments
0 upgraded, 27 newly installed, 0 to remove and 400 not upgraded.
Need to get 85.2 MB of archives.
After this operation, 509 MB of additional disk space will be used.
Do you want to continue? [Y/n] Y

...

Processing triggers for libc-bin (2.31-0ubuntu9) ...
Processing triggers for systemd (245.4-4ubuntu3.11) ...
Processing triggers for man-db (2.9.1-1) ...
Processing triggers for install-info (6.7.0.dfsg.2-5) ...

3. Download Swift Programming Language

We will use the latest stable version of swift (Swift 5.4.3, which was released on September 09, 2021). The file is located on Swift official website (https://swift.org/download/#releases). We will use wget command line, as shown below :

wget https://swift.org/builds/swift-5.4.3-release/ubuntu2004/swift-5.4.3-RELEASE/swift-5.4.3-RELEASE-ubuntu20.04.tar.gz
mpik@app01:~$ wget https://swift.org/builds/swift-5.4.3-release/ubuntu2004/swift-5.4.3-RELEASE/swift-5.4.3-RELEASE-ubuntu20.04.tar.gz
--2021-09-15 08:07:45--  https://swift.org/builds/swift-5.4.3-release/ubuntu2004/swift-5.4.3-RELEASE/swift-5.4.3-RELEASE-ubuntu20.04.tar.gz
Resolving swift.org (swift.org)... 169.47.73.10
Connecting to swift.org (swift.org)|169.47.73.10|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 508231331 (485M) [application/x-gzip]
Saving to: ‘swift-5.4.3-RELEASE-ubuntu20.04.tar.gz’

swift-5.4.3-RELEASE-ubuntu20.04.ta 100%[===============================================================>] 484.69M   802KB/s    in 10m 23s 

2021-09-15 08:18:10 (797 KB/s) - ‘swift-5.4.3-RELEASE-ubuntu20.04.tar.gz’ saved [508231331/508231331]

There is almost 500 MB the file that we must download. After download process is completed done, then we will extract a file by using command line :

tar xzf swift-5.4.3-RELEASE-ubuntu20.04.tar.gz

After extracting Swift tar file was competed, then we will move it to /usr/share/swift directory.

mpik@app01:~$ tar xzf swift-5.4.3-RELEASE-ubuntu20.04.tar.gz
mpik@app01:~$ sudo mv swift-5.4.3-RELEASE-ubuntu20.04 /usr/share/swift/

4. Add Swift to System PATH Enviroment Variable

On this step, we will set the Swift path on the system’s PATH environment variable.

mpik@app01:~$ echo "export PATH=/usr/share/swift/usr/bin:$PATH" >> ~/.bashrc
mpik@app01:~$ source  ~/.bashrc

5. Verify Swift Installation

After Swift installation was completed done, then we will verity its installation by querying its version. For this purpose we will type the command line :

swift -v
mpik@app01:~$ swift -v
Swift version 5.4.3 (swift-5.4.3-RELEASE)
Target: x86_64-unknown-linux-gnu
/usr/share/swift/usr/bin/lldb "--repl=-disable-objc-interop -color-diagnostics"
Welcome to Swift version 5.4.3 (swift-5.4.3-RELEASE).
Type :help for assistance.
  1>

REPL (Read Eval Print Loop)

REPL, is an interactive shell that will read, evaluate, and print the results of any Swift code we enter. We can write any valid swift statements and see them evaluated. We will enter the REPL by typing swift command. The example below, showing help command on REPL.

mpik@app01:~$ swift
Welcome to Swift version 5.4.3 (swift-5.4.3-RELEASE).
Type :help for assistance.
  1> :help

The REPL (Read-Eval-Print-Loop) acts like an interpreter.  Valid statements, expressions, and declarations are immediately compiled and
executed.
The complete set of LLDB debugging commands are also available as described below.
Commands must be prefixed with a colon at the REPL prompt (:quit for example.)  Typing just a colon followed by return will switch to the
LLDB prompt.
Type “< path” to read in code from a text file “path”.
Debugger commands:
  apropos           -- List debugger commands related to a word or subject.
  breakpoint        -- Commands for operating on breakpoints (see 'help b' for shorthand.)
  command           -- Commands for managing custom LLDB commands.
  disassemble       -- Disassemble specified instructions in the current target.  Defaults to the current function for the current thread
                       and stack frame.
  expression        -- Evaluate an expression on the current thread.  Displays any returned value with LLDB's default formatting.
  frame             -- Commands for selecting and examing the current thread's stack frames.
  gdb-remote        -- Connect to a process via remote GDB server.  If no host is specifed, localhost is assumed.
  gui               -- Switch into the curses based GUI mode.
  help              -- Show a list of all debugger commands, or give details about a specific command.
  kdp-remote        -- Connect to a process via remote KDP server.  If no UDP port is specified, port 41139 is assumed.
  language          -- Commands specific to a source language.
  log               -- Commands controlling LLDB internal logging.
  memory            -- Commands for operating on memory in the current target process.
  platform          -- Commands to manage and create platforms.
  plugin            -- Commands for managing LLDB plugins.
  process           -- Commands for interacting with processes on the current platform.
  quit              -- Quit the LLDB debugger.
  register          -- Commands to access registers for the current thread and stack frame.
  reproducer        -- Commands for manipulating reproducers. Reproducers make it possible to capture full debug sessions with all its
                       dependencies. The resulting reproducer is used to replay the debug session while debugging the debugger.
                       Because reproducers need the whole the debug session from beginning to end, you need to launch the debugger in
                       capture or replay mode, commonly though the command line driver.
                       Reproducers are unrelated record-replay debugging, as you cannot interact with the debugger during replay.
  script            -- Invoke the script interpreter with provided code and display any results.  Start the interactive interpreter if no
                       code is supplied.

Conclusion

We have shown how to install Swift programming language on Ubuntu 20.04 LTS operating system successfully. I hope this article will be useful for who need a Swift installation guidance.

Share this article via :

Leave a Reply

Your email address will not be published. Required fields are marked *