How To Install Vapor On A Virtual Machine

Emmanuel Orvain
4 min readNov 10, 2020

In this story we will learn how to install Vapor 4.0 in the OVH Public Cloud. Vapor is a server-side Swift web framework. OVH is a French cloud computing company.

We will set up the following configuration :

  • Ubuntu 20.04
  • Vapor 4.0
  • Swift 5.2.5
  • MongoDB
  • Vapor 4.0 on Ubuntu 20.04 required a swift version >= 5.2.4

Create the Virtual Machine

Create an account on OVHCloud, then go to your Public Cloud section and create a new instance.

  1. Select a OVH sandbox.
  2. Select Ubuntu 20.04
  3. Create ssh key.
  4. Choose your billing period.
  5. Connect to you Virtual machine with ssh.
Create a sandbox instance
Select instance location and Operating System

Setup the virtual machine

Install MongoDB (optional)

Follow this guide to install MongoDB on your virtual machine.

Follow the next link to learn how to use MongoDB with Vapor 4.0

Install required dependencies

To install Swift and Vapor pre-installation are required.

# 1
sudo apt-get update
# 2
sudo apt-get 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

Install Swift

The following command will install a release of Swift that is necessary to run Vapor. We choose to use Swift 5.2.5. You can, of course select another release (≥5.2.4).

# 1 Download the release
wget https://swift.org/builds/swift-5.2.5-release/ubuntu2004/swift-5.2.5-RELEASE/swift-5.2.5-RELEASE-ubuntu20.04.tar.gz
# 2 Unarchive
tar xzf swift-5.2.5-RELEASE-ubuntu20.04.tar.gz
# 3 Add the Swift binaries to path
vim ~/.bashrc
# 4 Copy and save in your .bashrc file
export PATH=/home/ubuntu/swift-5.2.5-RELEASE-ubuntu20.04/usr/bin:”${PATH}”
# 5 In your ssh terminal
source ~/.bashrc
# 6 Check that swift is installed (optional)
swift --version

Install Vapor as part of the Vapor toolbox

You can find the latest release of the toolbox on the releases page on GitHub. Here we have chosen 18.2.2 version

# 1
git clone https://github.com/vapor/toolbox.git
# 2
cd toolbox
# 3
git checkout 18.2.2
# 4
swift build -c release --disable-sandbox --enable-test-discovery
# 5
sudo mv .build/release/vapor /usr/local/bin

Create/Build/Run serve a project

We will create and build the auto generated project from Vapor. We must create the file Tests/LinuxMain.swift to avoid an error at the build. The build duration can take up to 5 minutes.

# 1
mkdir ~/vapor
# 2
cd ~/vapor
# 3
vapor new HelloVapor
# 4
cd HelloVapor
# 5
touch Tests/LinuxMain.swift
# 6 This can be long at the first time
vapor run serve --hostname 0.0.0.0 --port 8080 &

You can now test with your favorite web browser or with Postman.

Serve a project on port 80

As you may know, to serve on port 80 you must be logged as root. As we did just before, we must update the path to the swift binaries. Then, serve your project at port 80.

# 1 Log as root
sudo su
# 2 Add the Swift binaries to path
vim ~/.bashrc
# 3 Copy and save in your .bashrc file
export PATH=/home/ubuntu/swift-5.2.5-RELEASE-ubuntu20.04/usr/bin:”${PATH}”
# 4 In your ssh terminal
source ~/.bashrc
# 5 Go to the project
cd /home/ubuntu/vapor/HelloVapor
# 6 Check that swift is installed (optional)
swift — version
# 7 Run the server on port 80
vapor run serve — hostname 0.0.0.0 — port 80

External links

Vapor 4.0 installation on Linux: https://docs.vapor.codes/4.0/install/linux/

Swift releases: https://swift.org/download/#releases

If you enjoyed this post, please leave some claps. 👏👏👏

You can clap up to 50 times, so get clicking/tapping! 😉

--

--

Emmanuel Orvain

French iOS / Android experimented developper from Toulouse in south of France. https://occirama.com/scanandfile/