Photo by Zugr on Unsplash

How to run a Vapor Application on a Raspberry PI with Ubuntu 20.4

Alex Ilovan
salt&pepper
Published in
4 min readMar 7, 2023

--

Recently I have been doing some research into Distributed Querying in IoT networks.

Long story short, in the P.O.C that I’ve built for my research I’ve needed to deploy a server on a Raspberry PI. I went with Vapor just for the convenience of writing everything in Swift (both client and server-side apps) but things were a little bit trickier than I have originally expected.

Vapor is a popular web framework written in Swift that can be used to create server-side applications.

In this article, I’ll guide you through the process of deploying a Vapor web server on a Raspberry Pi running Ubuntu 20.04.

There are some prerequisites though…before we begin, you’ll need to make sure that you have the following:

  • 1 x Raspberry Pi
  • basic understanding of the command line interface
  • sudo privileges

Step 1: Install Ubuntu 20.4

You can do this by using the Raspberry PI imager. You need to plug an SD card into your computer and use this software to do the rest. In the choose OS — select Ubuntu 20.4 and choose storage-select the SD card.

Step 2: Install Swift

Vapor is built using the Swift programming language, so the first step is to install Swift on your Raspberry Pi. You can do this by following these steps:

  1. Open a terminal window on your Raspberry Pi or SSH into it 😉
  2. Run the following command to download the Swift package:
curl -s https://packagecloud.io/install/repositories/swift-arm/release/script.deb.sh | sudo bash

3. Run the following command to install the swift language package:

sudo apt install swiftlang

4. Update the swift packages:

sudo swift package update

You can verify that Swift is installed and working by running the following command:

swift --version

You will know that everything is alright when the output will show the Swift version number.

Till here, you have everything in order to run a Vapor project. You can just git clone your project, cd into its directory and run the following commands:

sudo swift build

and then just:

sudo swift run

You know you’ve made it when:

[ NOTICE ] Server starting on http://127.0.0.1:8080

OR if you want to create a Vapor project from scratch:

Step 3: Install Vapor

Now that you have Swift installed, you can install Vapor by following these steps:

  1. Run the following command to install Vapor
sudo apt install vapor

2. Verify that Vapor is installed by running the following command:

vapor --version

You will know that everything is alright when the output will show the Vapor version number.

Step 4: Create a new Vapor Project

Now that you have Vapor installed, you can create a new Vapor project by following these steps:

  1. Run the following command to create a new Vapor project:
vapor new myproject

This creates a new Vapor project named “myproject” in the current directory.

3. Navigate into the new project directory by running the following command:

cd myproject

You can now build and run your Vapor project by following these steps:

4. Run the following command to build your project:

vapor build --arch arm64

5. Run the following command to run your project:

vapor run serve --hostname 0.0.0.0 --port 8080

This command runs your project and binds it to the IP address 0.0.0.0 and port 8080.

6. Open a web browser and navigate to http://<your_raspberry_pi_ip_address>:8080. You should see the Vapor “Hello, world!” message displayed in your browser.

And that’s it ✨… but there are a couple of things that we can do that more or less go hand in hand with deploying a server.

The following steps are marked as optional. You don’t necessarily need to add them if you don’t require them.

[Optional] Step 5: Install PostgreSQL

Usually, when using a webserver, we also need a database server. You can use the following command to install a PostgreSQL server.

sudo apt-get install openssl libssl-dev zlib1g-dev libsqlite3-dev

[Optional] Step 6: Deploy Vapor Project as a Systemd Service

Now that your Vapor project is up and running, you may want to set it up as a systemd service so that it starts automatically at boot. Here are the steps to do that:

  1. Create a new file in the /etc/systemd/system/ directory named myproject.service:
sudo nano /etc/systemd/system/myproject.service

2. Paste the following contents into the file:

[Unit]
Description=MyProject Vapor Web Server

[Service]
User=<your_username>
WorkingDirectory=/path/to/myproject
ExecStart=/usr/bin/vapor run serve --hostname 0.0.0.0 --port 8080
Restart=always

[Install]
WantedBy=multi-user.target

Be sure to replace <your_username> with your actual username and /path/to/myproject with the path to your Vapor project directory.

3. Save and close the file.

4. Run the following command to reload the systemd configuration:

sudo systemctl daemon-reloadsudo apt update

5. Run the following command to start the myproject service:

sudo systemctl start myproject

6. Run the following command to enable the myproject service to start automatically at boot:

sudo systemctl enable myproject

You can now reboot your Raspberry Pi, and your Vapor project will start automatically as a systemd service.

From this point on, the sky is the limit 🚀

Let me know what you think and don’t be shy to share your story if you ever needed to deploy Vapor on a Raspberry PI 🎶

--

--

Alex Ilovan
salt&pepper

🚀Head of Mobile Development @S&P 💻Comp. Engineer 🪐Engineering Manager. You can visit at: https://www.linkedin.com/in/alex-ilovan-129161b4/