DEV Community 👩‍💻👨‍💻

Alex Adam
Alex Adam

Posted on • Originally published at alexadam.dev

How to install and run Swift on Linux

How to install and run Swift on Linux

A step by step guide on how to install Swift on Ubuntu 20.04.

Setup

Download Swift from https://swift.org/download/ -> select the Toolchain archive for Ubuntu 20.04

Extract the archive, then open ~/.bashrc and append this line:

PATH="/.../<absolute-path-to-the-extracted-swift-folder>/usr/bin:$PATH"
Enter fullscreen mode Exit fullscreen mode

Reload .bashrc with source ~/.bashrc

Install the required dependencies. See https://swift.org/download/#using-downloads -> Linux -> Installation -> Install required dependencies (for Ubuntu 20.04)

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 \
          uuid-dev \
          zlib1g-dev
Enter fullscreen mode Exit fullscreen mode

Test it, in a terminal:

swift --version

# you should see something similar to
Swift version 5.5.2 (swift-5.5.2-RELEASE)
Target: x86_64-unknown-linux-gnu
Enter fullscreen mode Exit fullscreen mode

Create an example app

Make a folder:

mkdir swift-app
cd swift-app
Enter fullscreen mode Exit fullscreen mode

Then generate an empty project with:

swift package init --type executable
Enter fullscreen mode Exit fullscreen mode

build it:

swift build
Enter fullscreen mode Exit fullscreen mode

and run the executable:

.build/debug/swift-app

# or
swift run

# it should print Hello, world!
Enter fullscreen mode Exit fullscreen mode

Top comments (0)

Let's Get Wacky


Use any Linode offering to create something unique or silly in the DEV x Linode Hackathon 2022 and win the Wacky Wildcard category

Join the Hackathon <-