Swift for Linux packages are officially provided for Ubuntu. If you want to run Swift on another Linux server flavor, you are on your own.
Fortunately, building Swift on Linux is pretty straightforward once you know how to do it.
Please, note that the build process is quite heavy and your likely need more than 16 GB or RAM to build Swift (especially if you prepare a debug build). In my example, I used a server with 32 GB. At the end of the article, you will find a link to download the result of the build. You may want to use it to avoid rebuilding it on your own on a smaller server.
Now, let’s check the build process on Linux Debian Stretch.
Preparing the server
Install Swig 3.12 on Debian
First, let’s fix a specific Debian issue.
The default version of Swig provided with Debian Stretch is Swig 3.0.10. The problem is that it is not compliant with LLDB. See LLDB source code for details.
We first need to ensure Swig is not already installed:
$ sudo apt-get -y remove swig3.0 swig
You can then install Swig a newer version of Swig from packages compiled for Ubuntu. I used the package for Swig and Swif3.0 for Ubuntu 18.04 LTS, as found on pkgs.org. You may want to select other packages, depending on your need.
Let’s download and install Swig 3.12 packages:
$ wget http://archive.ubuntu.com/ubuntu/pool/universe/s/swig/swig_3.0.12-1_amd64.deb
$ wget http://archive.ubuntu.com/ubuntu/pool/universe/s/swig/swig3.0_3.0.12-1_amd64.deb
$ sudo dpkg -i swig*.deb
Other dependencies required for Swift are described in Swift README.
You can install them all, with the following command:
$ sudo apt-get install -y git cmake ninja-build clang python uuid-dev libicu-dev icu-devtools libbsd-dev libedit-dev libxml2-dev libsqlite3-dev libpython-dev libncurses5-dev pkg-config libblocksruntime-dev libcurl4-openssl-dev systemtap-sdt-dev tzdata rsync swig
You should be ready to download and compile Swift from Github repository.
You can type the following commands to build Swift from source.
First, you need to clone Swift repository in a subdir of
swift-source new directory:
$ mkdir swift-source
$ cd swift-source
$ git clone https://github.com/apple/swift.git
Then, you need to download all the repositories it will build in the process:
$ ./swift/utils/update-checkout --clone
Finally, you need to launch Swift build itself. The following command is the easiest one to use to install Swift on your Linux server:
$ swift/utils/build-script --preset=buildbot_linux,no_test install_destdir=~/swift-install installable_package=~/swift.tgz
It will build Swift Swift in
swift-install dir and prepare a
.tgz archive with the content of the installation.
Have a long break, it will take a while :)
Once it is done, you can simply copy the resulting structure in
$ sudo cp -R ~/swift-install/usr/* /usr/local/
Testing Swift a try
The first thing you can test to get started is the REPL.
swift command can be used to launch the REPL. You can then type interactive Swift commands:
Welcome to Swift version 4.2-dev (LLVM cbe8d5e28f, Clang 3452631569, Swift 0d2787fb31).
Type :help for assistance.
1> print("Hello, Swift!")
You can also put that print statement in a
hello.swift file and compile it with
$ echo "print(\"Hello, Swift\")" > Hello.swift
$ swiftc Hello.swift
Your Swift environment is now ready and you can now start developing in Swift on Linux.
From there, the next step is to explore Swift on the server-side, using a server framework like Swift-NIO, Vapor, Kitura or Perfect.
Downloading the resulting build
If you would like to try Swift on Debian to learn the language on a smaller instance, you can try downloading our prebuilt archive: swift-4.2-git.tgz.
Once downloaded, you can download the signature file swift-4.2-git.tgz.asc and check the signature as follows:
$ gpg --keyserver pgp.mit.edu --recv-key CF34B813
$ gpg --verify swift-4.2-git.tgz.asc
gpg: Signature made Thu 15 Nov 2018 11:03:00 AM CET using RSA key ID CF34B813
gpg: Good signature from "Mickaël Rémond <mremond@p*.net>"
gpg: WARNING: This key is not certified with a trusted signature!
gpg: There is no indication that the signature belongs to the owner.
Primary key fingerprint: 58F4 6916 C32F 20EE 648F 9475
It is signed with my GPG key (Mickaël Rémond).