📦 Install
Installs all project dependencies according to composer.json, with the option to skip development dependencies or optimize the autoloader.
When to use
Use this after pulling project code, before deployment, or when composer.lock exists and dependencies need to be restored at the locked versions. Equivalent to running composer install [--no-dev] [--optimize-autoloader].
Signature
go
func (c *Composer) Install(noDev bool, optimize bool) errorParameters
| Parameter | Type | Description |
|---|---|---|
noDev | bool | When true, does not install the development dependencies in require-dev |
optimize | bool | When true, optimizes the autoloader by generating a class map, improving production performance |
Return value
error: Returns the corresponding error message when an error occurs during installation; 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)
}
// Development environment: install all dependencies (including dev dependencies)
if err := comp.Install(false, false); err != nil {
log.Fatalf("Install dependencies failed: %v", err)
}
// Production environment: install only production dependencies and optimize the autoloader
if err := comp.Install(true, true); err != nil {
log.Fatalf("Production install failed: %v", err)
}
fmt.Println("Dependencies installed")
}Advanced
- To pass more options (such as
--prefer-dist,--no-scripts), useInstallWithOptions. - To only simulate an install without actually modifying files, use
InstallDryRun. - To force installing from source or distribution packages, see
InstallWithPreferSourceandInstallWithPreferDist. - To skip script execution, see
InstallNoScripts.