⚙️ Config
A struct controlling installer behavior — where to install, whether to use sudo, proxy settings, target version, and whether to auto-install PHP.
Package path: github.com/scagogogo/composer-skills/pkg/installer
Config Struct
type Config struct {
DownloadURL string // Composer installer script download URL
InstallPath string // Composer installation directory
UseProxy bool // Whether to use proxy for download
ProxyURL string // Proxy server URL
TimeoutSeconds int // Download/install timeout (seconds)
UseSudo bool // Whether to use sudo/admin privileges (Unix)
PreferBrewOnMac bool // Whether to prefer Homebrew on macOS
PreferPackageManager bool // Whether to prefer system package manager on Linux
AutoInstallPHP bool // Whether to auto-install PHP when missing
TargetVersion string // Target version: "latest"/"1"/"2"/"preview"/"2.5.1"
}Field Details
| Field | Type | Default (DefaultConfig) | Description |
|---|---|---|---|
DownloadURL | string | https://getcomposer.org/installer | URL to fetch composer-setup.php |
InstallPath | string | Windows: %ProgramFiles%\Composer; darwin/linux: /usr/local/bin | Composer landing directory |
UseProxy | bool | false | Whether to use proxy for download |
ProxyURL | string | Empty | Proxy URL, paired with UseProxy |
TimeoutSeconds | int | 300 | Download/install timeout in seconds |
UseSudo | bool | false | Whether to use sudo when writing to system directories (macOS/linux/unix) |
PreferBrewOnMac | bool | true | Try brew install composer first on macOS |
PreferPackageManager | bool | true | Try apt/dnf/pacman etc. first on Linux |
AutoInstallPHP | bool | true | Auto-install PHP via package manager when missing |
TargetVersion | string | latest | Specify install version; empty string equals latest |
Two Preset Constructors
DefaultConfig
Returns a general default config suitable for the current OS.
func DefaultConfig() ConfigFeatures:
- ✅ Enables
PreferBrewOnMac/PreferPackageManager/AutoInstallPHPon both macOS and Linux. - ⚠️
UseSudo=false— writing to/usr/local/binon most desktop Linux requires manually enabling sudo or settingDefaultConfig().UseSudo=true.
SmartConfig
Further optimizes based on OS on top of DefaultConfig, giving "most likely to succeed in one shot" config.
func SmartConfig() Config| OS | SmartConfig Adjustment |
|---|---|
| 🍎 darwin | PreferBrewOnMac=true, AutoInstallPHP=true |
| 🐧 linux | PreferPackageManager=true, AutoInstallPHP=true, UseSudo=true |
| 🪟 windows | PreferPackageManager=false, AutoInstallPHP=false (direct phar download, no PHP involvement) |
When to Use SmartConfig
Most "install without thinking" scenarios can directly use SmartConfig(). It enables sudo by default on Linux and disables PHP auto-install on Windows, avoiding errors on Windows due to missing package manager.
DefaultConfig vs SmartConfig Comparison
| Field | DefaultConfig (linux/darwin) | SmartConfig (linux) | SmartConfig (darwin) | SmartConfig (windows) |
|---|---|---|---|---|
InstallPath | /usr/local/bin | /usr/local/bin | /usr/local/bin | %ProgramFiles%\Composer |
UseSudo | false | true | false | false |
PreferBrewOnMac | true | true | true | false |
PreferPackageManager | true | true | true | false |
AutoInstallPHP | true | true | true | false |
TargetVersion | latest | latest | latest | latest |
TimeoutSeconds | 300 | 300 | 300 | 300 |
Core difference: SmartConfig enables sudo on Linux, and on Windows doesn't rely on package manager or auto-install PHP — two most common pitfalls are both avoided by default.
Installation Paths
| OS | Default InstallPath | Executable Output |
|---|---|---|
| 🪟 Windows | %ProgramFiles%\Composer | composer.phar + composer.bat (need to manually add to PATH) |
| 🍎 darwin | /usr/local/bin | composer.phar + composer (shell wrapper script) |
| 🐧 linux | /usr/local/bin | composer.phar + composer (shell wrapper script) |
The composer wrapper script content is #!/bin/sh\nphp "<phar>" "$@", Windows .bat is @php "<phar>" %*.
Whether to Use sudo
When UseSudo=true:
- File writing uses
sudo tee/echo ... | sudo tee. chmodusessudo chmod.- Package manager commands prefix with
sudo. - File deletion uses
sudo rm -f.
When UseSudo=false, all operations use normal file operations; unwritable target directory returns ErrInsufficientRights.
Container Environment
CI containers, Docker images are usually already root, sudo may not exist or behave unexpectedly. Explicitly set UseSudo=false and point InstallPath to a writable directory (e.g., /usr/local/bin is usually writable in root images).
Whether to Auto-install PHP
When AutoInstallPHP=true, Install() / InstallWithProgress() when HasPHP() is false will:
- Call
DetectLinuxDistro()to get distro and package manager. - Call
InstallPHP(distro, useSudo)to install PHP and common extensions (mbstring/xml/curl) via corresponding package manager. - Verify again with
HasPHP(), if still fails returnErrPHPNotFound.
When AutoInstallPHP=false, missing PHP directly returns ErrPHPNotFound without any attempt — suitable for environments that want strict control over PHP source.
No Auto PHP Install on Windows
Windows has no unified package manager, SmartConfig defaults AutoInstallPHP=false. Windows users need to install PHP themselves and ensure php is in PATH.
Quick Examples
One-click Install with SmartConfig
package main
import (
"log"
"github.com/scagogogo/composer-skills/pkg/installer"
)
func main() {
inst := installer.NewInstaller(installer.SmartConfig())
if err := inst.Install(); err != nil {
log.Fatalf("Installation failed: %v", err)
}
}Custom Path + Proxy
package main
import (
"log"
"github.com/scagogogo/composer-skills/pkg/installer"
)
func main() {
cfg := installer.DefaultConfig()
cfg.InstallPath = "/opt/composer/bin"
cfg.UseProxy = true
cfg.ProxyURL = "http://127.0.0.1:7890"
cfg.TimeoutSeconds = 120
cfg.TargetVersion = "2.5.1"
inst := installer.NewInstaller(cfg)
if err := inst.Install(); err != nil {
log.Fatalf("Installation failed: %v", err)
}
}Runtime Config Switch
inst := installer.DefaultInstaller()
// First check current config
cfg := inst.GetConfig()
// Modify field and write back
cfg.UseSudo = true
inst.SetConfig(cfg)Advanced
- Target Version Semantics:
TargetVersionempty orlatestpulls stable;previewpulls preview;1/2pulls major version; like2.5.1pulls specific version phar. See Distro & PHP'sInstallComposerVersion. - Proxy & Timeout:
UseProxy/ProxyURL/TimeoutSecondsonly affect downloads viacomposerutils.DownloadFile(e.g., macOS/unix direct download installer script);curldirect downloads (InstallComposerVersioninternally) currently don't read proxy config. - Working with SmartInstaller:
SmartInstallerreceivesInstallOptions, whereConfigfield is thisConfig; empty auto-fallback toSmartConfig().