Test your Swift Packages Linux Compatibility on Mac

Ever wondered how to test your Swift code’s compatibility with Linux from your Mac without diving into Docker? In this article, I’ll share a simple command that makes the process effortless!

Test your Swift Packages Linux Compatibility on Mac

Recently, I encountered a situation where I needed to extract Swift networking code from one of my applications for reuse on a Vapor server. While the code built perfectly on my Mac, I ran into multiple errors when deploying it to my server. This prompted me to look for a way to easily test the code’s compatibility with Linux, which is the OS typically running on servers. Luckily, I found this article by Ole Begemann from 2020 that saved me from having to learn Docker.

In his article, Ole provides a straightforward approach to running Swift code in a Linux environment with a single command. All you need to do is to install the free Docker Desktop App on your Mac. But the command seemed quite lengthy and hard to remember, so I wanted to simplify things even further. I ended up with an approach where all I need to remember was swift-linux. Here's how:

Simplifying the Docker Command

First, I decided to further simplify Ole's command for brevity:

docker run --rm -it -v "$(pwd):/src" -w "/src" swift

Explanation of the Command (in case you're interested):

  • docker run: This command creates and starts a container.
  • --rm: Automatically removes the container when it exits.
  • -it: Runs the container in interactive mode with a terminal attached.
  • -v "$(pwd):/src": Mounts the current directory ($(pwd)) to /src in the container, allowing you to access your Swift files.
  • -w "/src": Sets the working directory in the container to /src.
  • swift: Specifies the Swift Docker image to use.

Note that I also removed the --privileged option from Ole's command for security reasons since it is rarely needed to test Swift packages.

Adding a Convenient Alias

To make running this command easier, I added an alias to my ~/.zshrc file. The ~/.zshrc file is a configuration file for the Zsh shell, which is the default shell on macOS nowadays. Here’s how you can do it:

  • Open the Terminal
  • Edit the ~/.zshrc file
    You can use TextEdit, which is the pre-installed text editor on macOS.
    Simply run: open ~/.zshrc
  • Add the alias
    Scroll to the bottom of the file and add the following line:
   alias swift-linux='docker run --rm -it -v "$(pwd):/src" -w "/src" swift'
  • Save and close the file
  • Apply the changes
    Run the following command to reload your config file: source ~/.zshrc

Running Commands in a Linux Environment

Now that the alias is set up, you can easily test your Swift code in a Linux environment directly from your Mac. Simply navigate to your project directory in the terminal and run:

swift-linux
On first run, the download of the Linux container will take some time.

This command will drop you into a Linux environment where you can execute swift build or swift test depending on your needs. Just type exit to return back to your Mac. This allows you to quickly see if everything works as expected and to catch any errors before deploying to your server.

And all you need to remember is swift-linux!

🎧
Enjoyed this article? Need personalized support?
If you need guidance with your Swift(UI) projects, I’m here for you!
Book a session with me to overcome your development challenges.
👨‍💻
Want to Connect?
Follow me on 🐦 Twitter (X), on 🧵 Threads, and 🦣 Mastodon.