Swift, often referred to as “Objective-C, without the C,” is an open-source programming language developed and maintained by Apple. Swift is a general-purpose programming language built using modern safety, performance, and software design patterns. The Swift project aims to create the best available language for systems programming to mobile and desktop apps, scaling up to cloud services.
In the following tutorial, you will learn how to install Swift on Debian 11 Bullseye.
Table of Contents
- Recommended OS: Debian 11 Bullseye
- User account: A user account with sudo privilages or root access (su command).
- Required Packages: wget
Updating Operating System
Update your Debian 11 operating system to make sure all existing packages are up to date:
sudo apt update && sudo apt upgrade
Root or Sudo Access
By default, when you create your account at startup with Debian compared to other distributions, it does not automatically receive sudoers status. You must either have access to the root password to use the su command or visit our tutorial on How to Add a User to Sudoers on Debian.
Install Python 3.8
To use Swift on Debian 11, you will need to install Python 3.8. As this is not available in Debian 11 default library, you will need to compile from scratch, but the process is very straightforward. You will need to visit the Python official download page to get the latest version; at the time of the creation of this guide, it is 3.8.12, but in time this will change, make sure to keep checking for updates in the future and repeating this process.
Once you have got the download link, use the wget command to download the Python 3.8 archive:
Extract the Python archive. Remember, change the version number if you downloaded a newer one:
tar -xf Python-3.8.12.tar.xz mv Python3.8.12 /opt/Python3.8.12
Now install the dependencies required to install Python 3.8:
sudo apt install build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libsqlite3-dev libreadline-dev libffi-dev curl libbz2-dev -y
Navigate to the directory and run the ./configure –enable-optimizations command:
cd /opt/Python3.8.12/ ./configure --enable-optimizations --enable-shared
Note, The script performs several checks to make sure all of the dependencies on your system are present. The ./configure –enable-optimizations will optimize the Python binary by running multiple tests, which will make the build process slower.
Now that you have built and configured the environment, it is time to compile it with the command make.
A handy trick is to specify the -j <number of cpu> as this can significantly increase compiling speed if you have a powerful server. For example, the LinuxCapable server has 6 CPUs, and I can use all 6 or at least use 4 to 5 to increase speed.
make -j 6
Once you have finished building, install Python binaries as follows:
sudo make altinstall
Note, it’s advised to use the make altinstall command NOT to overwrite the default Python 3 binary system.
Next, after the installation, you need to configure the dynamic linker run-time bindings:
sudo ldconfig /opt/Python-3.8.12
Confirm that Python 3.8 is installed and the build version by running the following command:
Swift has no Debian package available to install; however, to successfully get Swift working in Debian, you can install the Ubuntu package from Swift’s repository, which will work. To download Swift, visit the official download page that has the latest version listed. In time, these will change, but the tutorial will use an example of how to download Swift version build 5.4.2 using the wget command as follows:
Next, run the following command to install the dependencies required:
sudo apt install binutils git gnupg2 libc6-dev libcurl4 libedit2 libgcc-9-dev libpython2.7 libsqlite3-0 libstdc++-9-dev libxml2 libz3-dev pkg-config tzdata zlib1g-dev -y
The installation process is relatively quick and easy. First, you will need to extract the archive as follows:
tar -xvzf swift-5.4.2-RELEASE-ubuntu20.04.tar.gz
Next, move the extracted directory to /opt location using the following command:
sudo mv swift-5.4.2-RELEASE-ubuntu20.04 /opt/swift
Next, you will need to download the GPG signature to verify the integrity of the download. To do this, use the following :
wget -q -O - https://swift.org/keys/all-keys.asc | sudo gpg --import -
Example output if successful:
gpg: Total number processed: 8 gpg: imported: 7 gpg: new signatures: 1
Now you need to export the path of the Swift to the .bashrc file as follows:
echo "export PATH=/opt/swift/usr/bin:$PATH" >> ~/.bashrc
Once the path is exported, you can activate the .bashrc file with the following command:
To round off the installation, verify the version and build:
You should get a similar output:
Swift version 5.4.2 (swift-5.4.2-RELEASE) Target: x86_64-unknown-linux-gnu
Note, you will need to be logged out of root to use this command.
After the installation, you need to configure the dynamic linker run-time bindings:
sudo ldconfig /usr/swift/lib/python3
Connect to Swift REPL
To connect to the Swift REPLL (Read Eval Print Loop) can be achieved using the following command:
You can write valid Swift statements in this terminal and see them evaluated, and you can even use standard C Libraries by importing the GNU C Libary.
Below are some basic examples of Swift in action:
To exit the Swift terminal, use the following command:
Comments and Conclusion
In the tutorial, you have learned how to install Swift programming language by downloading and importing the PGP key, extracting the bash and path required, and basic commands to get a feel for the Swift terminal.