Skip to content

🐧 DetectLinuxDistro

Detects the current Linux distribution information.

When to Use

Use this when you need to choose the appropriate package manager (apt/dnf/pacman/apk, etc.) based on the distribution to install PHP or Composer. Only effective on Linux. 🔍

Signature

go
func DetectLinuxDistro() (*DistroInfo, error)

Parameters

ParameterTypeDescription
NonePackage-level function, takes no parameters

Return Value

  • *DistroInfo: Distribution info containing ID, Name, Version, and PackageManager. ✅
  • error: Returns an error on non-Linux environments; when no clues are found, PackageManager is "unknown". ⚠️

Example

go
package main

import (
	"fmt"
	"log"

	"github.com/scagogogo/composer-skills/pkg/installer"
)

func main() {
	distro, err := installer.DetectLinuxDistro()
	if err != nil {
		log.Fatalf("failed to detect distro: %v", err)
	}
	fmt.Printf("%s %s (package manager: %s)\n", distro.Name, distro.Version, distro.PackageManager)
}

Advanced

  • Detection order: /etc/os-releaselsb_release command → distro-specific files (/etc/redhat-release, etc.) → fallback to available package managers. 🛠️
  • Supports major distributions such as Ubuntu/Debian, Fedora/CentOS/RHEL, Arch, Alpine, openSUSE, and Gentoo. 📦
  • The result is used by InstallComposerViaPackageManager and InstallPHP. 🔗

Released under the MIT License