Skip to content

♻️ Reinstall

Reinstalls the specified package using Composer 2.2+'s native command, equivalent to composer reinstall packageName.

When to use

  • 🩹 Recover after a package installation is corrupted (missing or modified files).
  • 🔧 Reinstall when switching between --prefer-source / --prefer-dist installation modes.
  • 🧪 Fix installation anomalies caused by cache or interruption.

Signature

go
func (c *Composer) Reinstall(packageName string) error

Parameters

ParameterTypeDescription
packageNamestringThe name of the package to reinstall

Return value

ValueTypeDescription
ErrorerrorReturned when an error occurs during reinstallation

Example

go
package main

import (
    "log"

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

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

    if err := comp.Reinstall("symfony/console"); err != nil {
        log.Fatalf("reinstall package failed: %v", err)
    }
}

Advanced

ReinstallWithOptions

Use this when you need to attach flags such as --prefer-source or --prefer-dist:

go
func (c *Composer) ReinstallWithOptions(packageName string, options map[string]string) error
go
options := map[string]string{"prefer-source": ""}
err := comp.ReinstallWithOptions("symfony/console", options)

Batch reinstall

To reinstall multiple packages at once, use ReinstallMultiple(packages []string), equivalent to composer reinstall pkg1 pkg2 ....

Version requirement

reinstall is a native command introduced in Composer 2.2+. For older Composer versions, use the Remove + RequirePackage combination to achieve reinstallation.

Released under the MIT License