Skip to content

🌐 GlobalRequire

Installs (requires) a package in the Composer global environment so that the command-line tools it provides are available anywhere on the system.

When to use

Use this when you need to install globally available CLI tools (such as php-cs-fixer, psy/psysh). These packages are installed into Composer's global directory, and the corresponding executables go into the global vendor/bin.

Signature

go
func (c *Composer) GlobalRequire(packageName string, version string) error

Parameters

ParameterTypeDescription
packageNamestringThe package name to install globally, e.g. symfony/console
versionstringVersion constraint, e.g. ^5.0; pass an empty string to install the latest version

Return value

error: Returns the corresponding error message when an error occurs during execution; nil on success.

Example

go
package main

import (
	"fmt"
	"log"

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

func main() {
	comp, err := composer.New(composer.DefaultOptions())
	if err != nil {
		log.Fatalf("Initialization failed: %v", err)
	}

	// Install psysh globally, using the latest version
	if err := comp.GlobalRequire("psy/psysh", ""); err != nil {
		log.Fatalf("Global install failed: %v", err)
	}

	// Install a specific version globally
	if err := comp.GlobalRequire("friendsofphp/php-cs-fixer", "^3.0"); err != nil {
		log.Fatalf("Global install failed: %v", err)
	}

	fmt.Println("Global install completed")
}

Advanced

Released under the MIT License