How to run swift on linux when on MacOS

Konstantinos Nikoloutsos
3 min readDec 18, 2022
Photo by ThisIsEngineering

Why would you ever want to run swift on a linux machine? That’s a valid question until you:

  • Realize that macOS is way more expensive compared to linux machines on your CI 💸
  • You just want to test your swift library works on linux (e.g danger-swift)

By the end of this article you will have run swift on linux entirely for free 😍

1. Download docker desktop 🐳

Go to https://www.docker.com/ and download the appropriate one based on your machine.

After downloading it, install it just like any other app.

2. Setup and run Ubuntu in docker

For doing that you have to download the ubuntu image. Open your terminal and type

docker pull ubuntu

current version of ubuntu is 22.04

Then you have to create an instance(container) of that image.

docker run ubuntu

After that a container with the ubuntu image will be running. Use the following command in your terminal to connect as a root.

docker exec -u root -t -i <container_id> /bin/bash

<container_id> can be found in docker desktop app containers tab.

After connecting to your container.

Congrats, finally your Mac runs ubuntu 🍭 💪

3. Install swift

Try running swift in the terminal now will give bash: swift: command not found also git is not installed as well.

No worries we have to install them. In ubuntu there is a command apt that let us download from the outside world.

First, there are few packages or dependencies we required to install and configure Swift:

apt update && apt-get install \
binutils \
git \
gnupg2 \
libc6-dev \
libcurl4-openssl-dev \
libedit2 \
libgcc-9-dev \
libpython3.8 \
libsqlite3-0 \
libstdc++-9-dev \
libxml2-dev \
libz3-dev \
pkg-config \
tzdata \
unzip \
zlib1g-dev \
wget

This will download git as well 😄

Go to https://www.swift.org/download/ and download the swift based on your linux architecture (mine is aarch64 on ubuntu 22.04).

You will get the link to use in wget from here!
wget https://download.swift.org/swift-5.7.2-release/ubuntu2204-aarch64/swift-5.7.2-RELEASE/swift-5.7.2-RELEASE-ubuntu22.04-aarch64.tar.gz

And then install it.

tar -xvf swift-5.7.2-RELEASE-ubuntu22.04-aarch64.tar.gz
mv swift-5.7.2-RELEASE-ubuntu22.04 /opt/swift
echo "export PATH=/opt/swift/usr/bin:$PATH" >> ~/.bashrc
source ~/.bashrc

Happy linux coding 🎉

Remember swift is a general-purpose language and not running only on MacOS. Nevertheless, some features may not be available.

If you liked what you read please leave a 👏

--

--