Write your first Debian Apple Swift program

Swift is a programming language developed by Apple Inc. It is a versatile, multi-paradigmatic, compiled programming language developed for iOS, macOS, watchOS, tvOS, Linux and z / OS. According to the developers, Swift is a fantastic way to write software, whether for phones, desktops, servers, or anything else that runs code. It is a safe, fast, and interactive programming language that combines the best of modern language thinking with the wisdom of Apple’s broad engineering culture and the diverse contributions of the open source community. The compiler is optimized for performance, and the language is optimized for development without sacrificing any of them.

In this article, we will explain how you can install the latest version of Swift on your Debian. More importantly, we’ll tell you how to get started with Swift by explaining how to write your first Swift program, as well as the Hello World project in Swift.

We ran the commands and procedures mentioned in this article on the Debian 10 Buster system.

Installing the Swift Compiler on Debian 10

All Swift releases can be found on the following web page:

https://swift.org/download/#releases

Here we describe the installation of Swift version 5.0.1 through the command line.

Setting prerequisites

Open the “Terminal” application by pressing the “Super / Windows” key and then searching for the terminal by searching in the application launcher

The next step is to update the repository index of your system with the following command:

$ sudo apt-get update

This will help you install the latest available software version from the Internet. Please note that only an authorized user can add, remove and configure software in Debian.

$ sudo apt-get update

Install package updates

The first condition we establish is the Clang compiler. You can do this by running the following command as sudo:

$ sudo apt-get install clang

Set clang

The system may ask you to enter a password for sudo, as well as provide you with the option Y / n to continue the installation. Type Y, then press Enter; software will be installed on your system. However, the process may take some time depending on the speed of your internet.

Swift also needs several libraries for quick work. Run the following commands to add the necessary libraries to your system.

$ sudo apt-get install libcurl4 libpython2.7 libpython2.7-dev

Set prerequisites

You are now ready to install Swift.

Install Swift

We decided to install Swift version 5.0.1 on our Debian. The swift.org website provides Swift downloads for Xcode and Ubuntu, not specifically for Debian. However, you can download the archive for Ubuntu 18.04 and use it for the Debian 10 Buster.

Let’s first upload it to our system by running the following command:

$ wget https://swift.org/builds/swift-5.0.1-release/ubuntu1804/swift-5.0.1-RELEASE/swift-5.0.1-RELEASE-ubuntu18.04.tar.gz

Tip: Instead of entering a command, you can copy it from here and paste it into the Terminal using Ctrl + Shift + V, or using the Paste option in the context menu.

Download swift compiler package

The tar.gz installation package will be downloaded to your home folder.

Run the following command to extract the tar.gz file:

$ tar xzf swift-5.0.1-RELEASE-ubuntu18.04.tar.gz

Unzip the swift archive

Then move the extracted folder to the / usr / share / swift directory using the following command:

$ sudo mv swift-5.0.1-RELEASE-ubuntu18.04 /usr/share/swift

The next and final step is to set up the Swift binary to your Debian PATH environment variable. To do this, run the following commands:

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

Add a switch to the PATH variable

$ source ~/.bashrc

source ~ / .bashrc

Check Swift Installation

Swift is now installed and configured on your system. To verify that the software is actually installed on your system, you can check the version number with the following command:

$ swift --version

Check swift compiler version

Running Swift REPL

To get started, you can run some interactive commands in the Swift REPL (Read Eval Print Loop). This REPL will help you in various ways:

  • Quickly run and verify Swift code.
  • For educational purposes
  • Find new and unique features and test them in the command line console

Run the following command to access the Swift console:

$ swift

Here is an example of how I assigned my name to a variable and then printed it along with a string:

Run swift REPL

You can use the “: q” command to exit Swift REPL.

REPL is a good way to run several small programs, but to create an application you need to become familiar with Swift projects and packages. Please see below to get started with the Swift Project.

Swift Hello World Project

Now we will introduce you to the simplest Hello world project, which you can create and create using Swift. Please follow the instructions below for a project that simply displays “Hello, World!” On your screen.

1. Create a project directory using the mkdir command, and then switch to it. I am creating a directory called “Hello_swift”

$ mkdir Hello_swift
$ cd Hello_swift

2. Now run the following command to create an executable package:

$ swift package init --type executable

Create a fast executable package

The team created a hierarchy of core files needed for the Swift project. The most important of them:

Package.swift file that looks like this:

Write Hello World

And the main.swift file located in the Sources folder looks like this:

Hello world print team

This main.swift file already contains the necessary code to print hello world. Of course, you can edit this file so that your project does something else.

3. Compile this project by running the following command:

$ swift build

Build a quick program

Now you have the executable program in the .build / debug folder, whose name matches the name of your Swift project.

4. Now you are ready to start the project by running the following command:

$ .build/debug/[program_name]

Here is the result of my Swift program:

Debug build

That was all you needed to start programming in Swift. Fortunately, there are many documentation and tutorials available both on the official website www.swift.org and in other areas that will help you create productive applications using Swift.

Write your first Debian Apple Swift program

Sidebar