Razvan.net

The place for apps & thoughts

Swift on server – Part 1: Installation

Originally I wanted to just write about the workaround I used for handling Date types in Vapor with PostgreSQL, but in order to do so I had to have some basic project, so I started explaining how to create the base. It turned out to be quite a long post, so I decided to make it in 3 parts and add some more details about the installation, configuration and basic usage of Vapor framework.

So the 3 parts are:
Part 1: Installation of the required tools (Swift, Vapor & PostgreSQL) in order to use Swift on server side (this one, so if interested keep reading)
Part 2: Creating a basic app with Vapor and PostgreSQL
Part 3: Handling Date type in the model


Update: April 25, 2017 – New Vapor install process


There are two platforms, for now, where you can use Swift: macOS (of course) and Linux (just Ubuntu 14.04, 16.04, 16.10 – 64-bit versions based distributions).

Install Swift

On Mac

Not much to do for having Swift installed on a Mac, just download and install Xcode.

Open a terminal window and test if all is OK:

If you get at least 3.0 is fine, otherwise update to a more recent Xcode version.

On Linux

Here’s the quick version – just run:

for manual installation, check Vapor Docs.

Docker

If you have Docker installed on your machine, you can grab a Docker Image that has Swift already setup on Ubuntu, just get the image using:

Create a container from the image and attach it:

  • -p parameter maps local port 8080 the container’s 8080 port so I can test the app using url: http://localhost:8080
  • -v parameter for mapping local directory /srv/swift to the container’s /srv, so when in the container we create a new folder/files, they are in fact in hosts /srv/swift folder, so I can use Visual Code, for example, for editing files on my machine and they are changed also in the container where I’ll build and run the server.

Install Vapor toolbox

Run in a terminal window (if you opted for the docker version run this command in the container):
(no longer works)


Update: April 25, 2017
There are new ways of installing Vapor, old one is no longer supported.

On Mac

Installation using Homebrow:

On Linux/Docker

Install using apt:

If sudo and wget are not installed (in the docker image they are not) you must install them before running the previous commands using:

End update: April 25, 017


Test if everything is OK:

Install PostgreSQL

We’ll need a PostgreSQL database for this tutorial.
If you already have a Postgres server on another machine, you still have to install it on your machine in order to compile the Postgres driver that Vapor will use, unless you’re on a Linux where you can only install the dev libs.

On Mac

Install PostgreSQL using Homebrew

On Linux

Install PostgreSQL:

If you already have a Postgres on another server (or Docker container) and you just want to be able to build the Postgresql provider you can just only get libpq-dev:


Now everything is setup and ready to code.

Go to Part 2 to create a basic Vapor project that adds and retrieves data from the Postgres database.