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:
1 |
$ swift --version |
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:
1 |
$ curl -sL swift.vapor.sh/ubuntu | bash |
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:
1 |
$ docker pull swift |
Create a container from the image and attach it:
1 |
$ docker run -it -p 8080:8080 -v /srv/swift:/srv --name myswiftcontainer swift:latest /bin/bash |
- -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)
1 |
$ curl -sL toolbox.vapor.sh | bash |
Update: April 25, 2017
There are new ways of installing Vapor, old one is no longer supported.
On Mac
Installation using Homebrow:
1 |
$ brew install vapor/tap/vapor |
On Linux/Docker
Install using apt:
1 2 3 |
$ eval "$(curl -sL https://apt.vapor.sh)" $ sudo apt-get update $ sudo apt-get install vapor |
If sudo and wget are not installed (in the docker image they are not) you must install them before running the previous commands using:
1 2 |
$ apt-get update $ apt-get install sudo wget |
End update: April 25, 017
Test if everything is OK:
1 |
$ vapor --help |
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
1 2 3 4 5 6 |
$ brew install postgresql $ brew link postgresql $ brew services start postgresql // to stop $ brew services stop postgresql |
On Linux
Install PostgreSQL:
1 2 3 |
$ sudo apt-get update $ sudo apt-get install postgresql postgresql-contrib libpq-dev $ psql -h dbhost -U username dbname |
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
:
1 2 |
$ sudo apt-get update $ sudo apt-get install 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.