Skip to content

📦 InstallComposerViaPackageManager

Installs Composer via the system's native package manager.

When to Use

Use this when you are on Linux and want to install Composer with apt/dnf/yum/pacman/apk/zypper rather than downloading the phar directly. 🐧

Signature

go
func InstallComposerViaPackageManager(distro *DistroInfo, useSudo bool) (bool, error)

Parameters

ParameterTypeDescription
distro*DistroInfoDistribution info, typically obtained from DetectLinuxDistro
useSudoboolWhether to prefix the command with sudo

Return Value

  • bool: Whether installation was attempted (true when a matching package manager is found, false otherwise). 🎯
  • error: Returns a wrapped error on command execution failure; nil when not attempted. ⚠️

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)
	}
	attempted, err := installer.InstallComposerViaPackageManager(distro, true)
	if !attempted {
		fmt.Println("no package manager available")
		return
	}
	if err != nil {
		log.Fatalf("installation failed: %v", err)
	}
	fmt.Println("installed via package manager")
}

Advanced

  • A return of attempted=false means the distro has no known package manager; the caller should fall back to direct download. 🔄
  • Called by Installer.Install and SmartInstaller when PreferPackageManager is set. 🔗
  • Package names and command arguments vary across distros (e.g. pacman uses -S --noconfirm); these are handled internally. 🛠️

Released under the MIT License