Os module for Ubuntu

Greetings,

I'm new to swift programming and am working with it on Ubuntu Linux, swift-6.0.2-RELEASE-ubuntu24.04. I want to use the Logger but, I cannot import the os module, I get:

import os
' - error: no such module 'os'

As you can guess, my code runs fine on macOS. what do I need to do to fix my ubuntu swift package?

thanks
John
jjrushford@gmail.com

Many modules you'd find on Apple platforms aren't available for Linux and Windows.

Apple has a Swift logging library available for all platforms: swift-log.

Thanks for your reply, I've learned something new. Took me a bit to figure out the Package.swift but, I have it working now.

best regards
John

As a note: Swift doesn’t have a os library. If you want to have some OS functionality you should use Darwin(Apple‘s Kernal Name) on Apple Devices and GLibC on Linux or check if Foundation contains the functionality you want. This can prevent Future confusion. You may have come from python which doesn’t have a discernment between Kernals as it requires some special C-Libraries to make os work on all devices so it is reimplemented per kernal/OS(depending on the variance in OSs among a kernal), this does work for JIT languages but Compiled Languages cannot abstract OS functionality and as such need to properly integrate the OS functionality.

There is a package that some of the core of the Swift ecosystem (things like swift package manager, etc) is leveraging in this regard: GitHub - apple/swift-system: Low-level system calls and types for Swift - it's a collection of platform specific APIs to make interacting with the C API's otherwise exposed through Darwin, MUSL, GLibC, WASM, etc a bit more pleasant. Not everything is in there, and I'm sure quite a lot won't ever be, but it's worth knowing about.

1 Like

Would you import that with import System? It might be good to just provide for packages to have a import statement when people are introduced to them.

I was trying to use an older logging API and the documentation indicated that I needed to import os. I’m using swift-log now.

Thanks
John

For the swift-system package, you would write import SystemPackage. See the usage section of the readme file.

On macOS, you can import System without adding a package dependency. That will provide you with a library that is very similar to the one provided by the swift-system package.

1 Like