How to install Swift and Vapor on Ubuntu 16.04

Introduction

Swift is Apple’s programming language. It’s fast, secure, and modern, and it has a huge community to support the language. Swift is mainly used for developing IOS and macOS apps, but Swift 3, you can use for developing server-side apps.

Vapor is a popular Swift server side framework. Paired with Swift, they’re fast and modern, and it supports many of the features you’ll see in a framework for other programming languages.

In this tutorial, you will install Swift and Vapor on Ubuntu 16.04. You will test your settings by building a simple web app using one of the Vapor templates.

Prerequisites

To complete this article, you will need:

  • One Ubuntu 16.04 server with non-root user with sudo access. You can learn how to install it using the Initial Server Configuration Guide.
  • Git installed on your server. Git should already be installed on Ubuntu 16.04, but if it isn’t, run sudo apt-get install git.

Step 1 – Installing Swift

In order to be able to build and run web apps on Vapor, you first need to install Swift.

First, make sure you have the latest package list on your system:

sudo apt-get update

Then install the Swift prerequisites, which includes clang and some Python 2.7 components:

sudo apt-get install clang libicu-dev libpython2.7

After that download the latest version of Swift. This is not available with apt, but you can download it manually using the Swift download page (https://swift.org/download/), or wget:

wget https://swift.org/builds/swift-4.0-release/ubuntu1604/swift-4.0-RELEASE/swift-4.0-RELEASE-ubuntu16.04.tar.gz

Next, make sure your download hasn’t been corrupted or tampered with. The PGP keys will import Swift into your keyring, which will be used to verify the signature of the file:

gpg --keyserver hkp://pool.sks-keyservers.net 

--recv-keys 

'7463 A81A 4B2E EA1B 551F FBCF D441 C977 412B 37AD' 

'1BE1 E29A 084C B305 F397 D62A 9F59 7F4D 21A5 6D5F' 

'A3BA FD35 56A5 9079 C068 94BD 63BC 1CFE 91D3 06C6' 

'5E4D F843 FB06 5D7F 7E24 FBA2 EF54 30F0 71E1 B235'

You will see this output:

Output

...
gpg: key 412B37AD: public key "Swift Automatic Signing Key #1 <[email protected]>" imported
gpg: key 21A56D5F: public key "Swift 2.2 Release Signing Key <[email protected]>" imported
gpg: key 91D306C6: public key "Swift 3.x Release Signing Key <[email protected]>" imported
gpg: key 71E1B235: public key "Swift 4.x Release Signing Key <[email protected]>" imported
gpg: no ultimately trusted keys found
gpg: Total number processed: 4
gpg:               imported: 4  (RSA: 4)

After importing the keys, download the signature file for the downloaded release:

wget https://swift.org/builds/swift-4.0-release/ubuntu1604/swift-4.0-RELEASE/swift-4.0-RELEASE-ubuntu16.04.tar.gz.sig

To verify the signature of the file, run the following command, which generates the following output:

gpg --verify swift-4.0-RELEASE-ubuntu16.04.tar.gz.sig

You will see this output:

Output

gpg: assuming signed data in `swift-4.0-RELEASE-ubuntu16.04.tar.gz'
gpg: Signature made Wed 20 Sep 2017 01:13:38 AM UTC using RSA key ID 71E1B235
gpg: Good signature from "Swift 4.x Release Signing Key <swift-infras[email protected]>"
Primary key fingerprint: 5E4D F843 FB06 5D7F 7E24  FBA2 EF54 30F0 71E1 B235

You can see the warning, it looks like this:

Output

gpg: WARNING: This key is not certified with a trusted signature!
gpg:          There is no indication that the signature belongs to the owner.

This means that the Swift keys you imported are not yet trusted, either explicitly by you or by other keys set in your keychain. You can safely ignore these messages. However, if you get a different error, you must re-download the Swift binary.

Now we can actually install Swift. Run the following command to extract the binary you downloaded earlier:

tar xzf swift-4.0-RELEASE-ubuntu16.04.tar.gz

Then add the Swift toolbox in your path so that you can run the swift command:

export PATH=swift-4.0-RELEASE-ubuntu16.04/usr/bin:"${PATH}"

Entering this command will only add the swift command to the path for the current shell session. To make sure that it is automatically added in subsequent sessions, add it to your .bashrc file.

Bashrc file:

nano ~/.bashrc

Add the following line at the end of the file

~ / .bashrc

. . .
export PATH=swift-4.0-RELEASE-ubuntu16.04/usr/bin:"${PATH}"

Save and close the file.

To make sure everything works, run the swift command:

swift

You will be greeted with the Swift REPL, which indicates that everything is working correctly.

Output

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 will add all integers from 1 to 5. Enter each line into the REPL by pressing the ENTER key after each line:

var x = 0
for i in 1...5 { 
    x += i 
} 
x

The REPL will display the result of the calculation:

Output

$R0: Int = 15

Exit Swift REPL with CTRL + D. Now that Swift is installed, we’re ready to install Vapor.

Step 2 – Installing Vapor

To install Vapor, you will download and execute a script from the Vapor developers that adds the official Vapor package repository to your server’s package listing. Then you will use apt to install the latest version of Vapor.

It is generally not a good security practice to execute scripts loaded from others without checking them first. First, upload the installation script to the server using the curl command with the -o switch to specify the name of the local file:

curl -sL apt.vapor.sh -o apt.vapor.sh

Use less command to test this script:

less apt.vapor.sh

After you have examined the contents of the installation script, execute the script to add the repository:

bash ./apt.vapor.sh

You will be prompted for the SUDO password. Enter it as a script to be able to add new packages from source.

After the script finishes, you can install the vapor package and its dependencies.

sudo apt-get install vapor

You can verify that the pair was successfully installed using another script provided by the Vapor developers. Load the script again, test it, and then execute it:

curl -sL check.vapor.sh -o check.vapor.sh

less check.vapor.sh

bash ./check.vapor.sh

You will see this output, which indicates that the pair was successfully installed:

Output

? Compatible with Vapor 2

Now that both Swift and Vapor have been installed, you can create your first Vapor app.

Step 3 – creating the Vapor app

In order to create our application, we will use the default Vapor template. The web template allows you to create a custom web application.

This template assumes that you are using Git and that you have configured it with your name and email address. If you haven’t, you might see an error message that will show you how to set up Git. You can safely ignore this message, or run these commands to provide information about yourself:

git config --global user.email "ваша_почта@example.ru"

git config --global user.name "Ваше имя"

To create a web application based on this template, run the following command:

vapor new demo --template=web

The script generates a new application in a new directory with the specified name:

Output

Cloning Template [Done]
Updating Package Name [Done]
Initializing git repository [Done]
...
               _       __    ___   ___   ___
                 /  / /  | |_) / /  | |_)
               _/  /_/-- |_|   __/ |_| 
                 a web framework for Swift

              Project "demo" has been created.
       Type `cd demo` to enter the project directory.
   Use `vapor cloud deploy` to host your project for free!
                           Enjoy!

If you wanted to create an API instead of a complete web application, you can use the api template: .vapor new demo –template = api

Take a look at the web template source code (http://github.com/vapor/web-template) and the api template (http://github.com/vapor/api-template) to see how they work.

Let’s run our application and see it in action.

Step 4 – Compiling and Running the Vapor App

Swift applications must be compiled, unlike Python or Ruby applications. This means that before you can launch the Vapor app, you must start the build process.

First, go to the newly created demo folder:

cd demo

Then run the vapor build command to compile the web app.

vapor build

The first time you build your application, the process will get some dependency. It will cache these and skip this step into the future, which will make the build process much faster.

After the build, when the process is complete, start the application with the following command:

vapor run serve

The server will start displaying this output:

Output

Running demo ...
...
Starting server on 0.0.0.0:8080

You will see a warning about insecure hash and encryption keys, but you can ignore them while you are making the demo application. When you build your own application, follow the warning instructions.

Open your web browser and visit the Vapor app’s welcome page: http: // your_server_ip: 8080.

Output

The Swift community is growing steadily and there are many ways to get involved. While Swift is primarily used to build native IOS and MacOS apps, Swift on the Linux platform is on the rise. You can learn more about Swift by checking out The Swift Programming Language (https://itunes.apple.com/us/book/the-swift-programming-language-swift-4/id881256329?mt=11), a free eBook from Apple. To learn more about Vapor, check their documentation (https://docs.vapor.codes/2.0/).

Please disable your ad blocker or whitelist this site!

Sidebar