Ki jan yo enstale ak itilize PHP Composer sou Debian 11 Bullseye

Composer se yon manadjè pakè nivo aplikasyon pou lang pwogram PHP ki sanble ak NPM pou Node.Js oswa PIP pou Python. Composer bay yon fòma estanda pou jere tout depandans nan lojisyèl PHP ak bibliyotèk obligatwa yo lè li telechaje tout pakè PHP ki nesesè nan pwojè ou a epi jere yo pou ou. Li se itilize pa pi modèn PHP kad tankou Laravel, Drupal, Magento, ak Symfony.

Nan leson patikilye sa a, ou pral aprann ki jan yo telechaje ak enstale konpozitè, ansanm ak kèk esansyèl ki jan yo travay ak konpozitè sou Debian 11 Bullseye.

Kondisyon

  • OS rekòmande: Debian 11 Bullseye
  • Kont itilizatè: Yon kont itilizatè ak sudo privilèj or aksè rasin (su kòmand).
  • Pakè obligatwa: wget, PHP 5.3 oswa pi wo

Mete ajou sistèm operasyon

Mete ajou ou Debian 11 sistèm operasyon pou asire tout pakè ki egziste deja yo ajou:

sudo apt update && sudo apt upgrade

Rasin oswa Sudo Aksè

Pa default, lè ou kreye kont ou nan demaraj ak Debian konpare ak lòt distribisyon, li pa otomatikman resevwa estati sudoers. Ou dwe swa gen aksè a modpas rasin yo sèvi ak kòmandman su a oswa vizite leson patikilye nou an sou Ki jan yo ajoute yon itilizatè nan Sudoers sou Debian.


reklam


Enstale Depandans yo

Pake sa yo oblije telechaje konpozitè ak pou itilize nan lavni. Remake byen, si w ap itilize yon vèsyon espesifik PHP, w ap bezwen ajiste pakè PHP ki anba yo pou adapte vèsyon an. Pou egzanp, enstale PHP 8.0 yo pral php8.0-mbstring.

sudo apt install curl wget php-common php-cli php-gd php-mysql php-curl php-intl php-mbstring php-bcmath php-imap php-xml php-zip git unzip

Enstale PHP konpozitè

Ekip Composer la te fè yon script PHP ofisyèl pou enstale ak konfigirasyon PHP Composer sou sistèm ou an. Ou ka telechaje sa lè w vizite download paj oswa ouvri tèminal Debian ou a epi egzekite sa ki annapre yo.

Metòd PHP:

php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" 

Metòd WGET:

wget -O composer-setup.php https://getcomposer.org/installer

Koulye a, ak setup.php telechaje, li se tan enstale konpozitè. Sa a ka nan de fason diferan, youn nan ki ou ka enstale konpozitè globalman, oswa altènatif nan dezyèm ki se enstale pou chak baz aplikasyon PHP.

Pou enstale globalman:

php composer-setup.php --install-dir=/usr/local/bin --filename=composer
chmod +x /usr/local/bin/composer

Pou enstale pou chak aplikasyon:

cd /example-project/php-application && mkdir -p bin 
php composer-setup.php --install-dir=bin --filename=composer
chmod +x bin/composer

Dapre pi wo a, w ap bezwen navige nan anyè pwojè PHP a epi kreye yon anyè bin pou konpozitè PHP a dwe enstale.

Yon fwa fè, konfime konpozitè enstale ak ki bati ak vèsyon li ye.

composer --version
Composer version 2.1.8 2021-09-15 13:55:14

Koulye a, teste enstalasyon konpozitè ou a ak lòd sa a:

composer

Egzanp pwodiksyon:

   ______
  / ____/___  ____ ___  ____  ____  ________  _____
 / /   / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/
/ /___/ /_/ / / / / / / /_/ / /_/ (__  )  __/ /
\____/\____/_/ /_/ /_/ .___/\____/____/\___/_/
                    /_/
Composer version 2.1.8 2021-09-15 13:55:14

Usage:
  command [options] [arguments]

Options:
  -h, --help                     Display this help message
  -q, --quiet                    Do not output any message
  -V, --version                  Display this application version
      --ansi                     Force ANSI output
      --no-ansi                  Disable ANSI output
  -n, --no-interaction           Do not ask any interactive question
      --profile                  Display timing and memory usage information
      --no-plugins               Whether to disable plugins.
  -d, --working-dir=WORKING-DIR  If specified, use the given directory as working directory.
      --no-cache                 Prevent use of the cache
  -v|vv|vvv, --verbose           Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug

Available commands:
  about                Shows a short information about Composer.
  archive              Creates an archive of this composer package.
  browse               Opens the package's repository URL or homepage in your browser.
  cc                   Clears composer's internal package cache.
  check-platform-reqs  Check that platform requirements are satisfied.
  clear-cache          Clears composer's internal package cache.
  clearcache           Clears composer's internal package cache.
  config               Sets config options.
  create-project       Creates new project from a package into given directory.
  depends              Shows which packages cause the given package to be installed.
  diagnose             Diagnoses the system to identify common errors.
  dump-autoload        Dumps the autoloader.
  dumpautoload         Dumps the autoloader.
  exec                 Executes a vendored binary/script.
  fund                 Discover how to help fund the maintenance of your dependencies.
  global               Allows running commands in the global composer dir ($COMPOSER_HOME).
  help                 Displays help for a command
  home                 Opens the package's repository URL or homepage in your browser.
  i                    Installs the project dependencies from the composer.lock file if present, or falls back on the composer.json.
  info                 Shows information about packages.
  init                 Creates a basic composer.json file in current directory.
  install              Installs the project dependencies from the composer.lock file if present, or falls back on the composer.json.
  licenses             Shows information about licenses of dependencies.
  list                 Lists commands
  outdated             Shows a list of installed packages that have updates available, including their latest version.
  prohibits            Shows which packages prevent the given package from being installed.
  reinstall            Uninstalls and reinstalls the given package names
  remove               Removes a package from the require or require-dev.
  require              Adds required packages to your composer.json and installs them.
  run                  Runs the scripts defined in composer.json.
  run-script           Runs the scripts defined in composer.json.
  search               Searches for packages.
  self-update          Updates composer.phar to the latest version.
  selfupdate           Updates composer.phar to the latest version.
  show                 Shows information about packages.
  status               Shows a list of locally modified packages.
  suggests             Shows package suggestions.
  u                    Upgrades your dependencies to the latest version according to composer.json, and updates the composer.lock file.
  update               Upgrades your dependencies to the latest version according to composer.json, and updates the composer.lock file.
  upgrade              Upgrades your dependencies to the latest version according to composer.json, and updates the composer.lock file.
  validate             Validates a composer.json and composer.lock.
  why                  Shows which packages cause the given package to be installed.
  why-not              Shows which packages prevent the given package from being installed.

reklam


Ki jan yo travay ak PHP Composer

Leson patikilye a pral montre w kèk fonksyon esansyèl lè konpozitè PHP a enstale pou teste operasyon li yo.

Premyèman, kreye anyè pwojè ou a:

mkdir ~/composer-test
cd ~/composer-test

Apre sa, jwenn yon pake nan Packagist, oswa altènatif sèvi ak egzanp leson patikilye a epi kouri lòd sa a pou inisyalize yon nouvo fichye composer.json epi enstale pake kabòn nan:

composer require psr/log

Egzanp pwodiksyon:

Using version ^1.1 for psr/log
./composer.json has been created
Running composer update psr/log
Loading composer repositories with package information
Updating dependencies
Lock file operations: 1 install, 0 updates, 0 removals
  - Locking psr/log (1.1.4)
Writing lock file
Installing dependencies from lock file (including require-dev)
Package operations: 1 install, 0 updates, 0 removals
  - Downloading psr/log (1.1.4)
  - Installing psr/log (1.1.4): Extracting archive
Generating autoload files

Kòm pi wo a, konpozitè a pral otomatikman kreye epi mete ajou dosye composer.json la ansanm ak depandans ki nesesè yo. Altènativman, ou ka enstale ak oswa desann pakè ki deja egziste nan yon nimewo vèsyon espesifik.

Egzanp:

composer require psr/log=1.0

Nan anyè pwojè a, ou ka lis fichye yo ak repèrtwar ki nan li lè l sèvi avèk la ls lòd:

ls -l

Egzanp pwodiksyon:

./composer.json has been updated
Running composer update psr/log
Loading composer repositories with package information
Updating dependencies
Lock file operations: 0 installs, 1 update, 0 removals
  - Downgrading psr/log (1.1.4 => 1.0.0)
Writing lock file
Installing dependencies from lock file (including require-dev)
Package operations: 0 installs, 1 update, 0 removals
  - Downloading psr/log (1.0.0)
  - Downgrading psr/log (1.1.4 => 1.0.0): Extracting archive
Generating autoload files

Yon pann nan tèm yo ak dosye yo ki nan lis.

  • composer.json – Fichye kreye pou pwojè ak tout depandans PHP.
  • konpozitè.lock – Dosye ki gen lis tout pakè ak vèsyon yo.
  • machann – Anyè kote depandans yo telechaje epi enstale.

Avèk pwojè ou a, ou ka vle oswa mande pou mete ajou depandans yo, epi sa a ka fè kouri lòd sa a:

composer update

Egzanp pwodiksyon:

Loading composer repositories with package information
Updating dependencies
Nothing to modify in lock file
Installing dependencies from lock file (including require-dev)
Nothing to install, update or remove
Generating autoload files

Kòm ou ka wè pi wo a, pa gen okenn depandans ki bezwen mete ajou. Si ta gen, pwosesis la ta kòmanse.

Si ou pa bezwen modil la ankò, ou ka retire li ak konpozitè a retire lòd:

composer remove psr/log

Egzanp pwodiksyon:

./composer.json has been updated
Running composer update psr/log
Loading composer repositories with package information
Updating dependencies
Lock file operations: 0 installs, 0 updates, 1 removal
  - Removing psr/log (1.0.0)
Writing lock file
Installing dependencies from lock file (including require-dev)
Package operations: 0 installs, 0 updates, 1 removal
  - Removing psr/log (1.0.0)
Generating autoload files

Lè w retire pakè yo, menm jan ak enstale, retire ap otomatikman mete ajou fichye composer.json la. Ou ka verifye sa lè l sèvi avèk chat lòd.

cat composer.json

Egzanp anvan retire pake psr/log:

{
    "require": {
        "psr/log": "1.0"
    }
}

Egzanp apre yo fin retire pake psr/log:

{
}

Ki jan yo ajou PHP Composer

Yon karakteristik pratik nan PHP konpozitè se li ka pwòp tèt ou ajou nan dènye vèsyon ki estab ki disponib. Kouri lòd sa a pou ajou.

composer self-update

Egzanp pwodiksyon:

You are already using the latest available Composer version 2.1.8 (stable channel).

Kòm mansyone pi wo a, konpozitè PHP a deja nan dènye vèsyon an. Sepandan, si yon ajou disponib, w ap mande w ajou.


reklam


Kòmantè ak konklizyon

Ou te aprann kijan pou enstale PHP konpozitè sou sistèm Debian 11 Bullseye ou a ak kèk fonksyon nan leson patikilye a. An jeneral, konpozitè a se yon zouti trè popilè, e pa gen dout ou pral rankontre sa a kòm yon pwomotè. Youn nan pi bon avantaj ki genyen nan gen yon konpozitè se ke li fè fas ak depandans PHP sou yon baz pou chak pwojè. Ou ka gen plizyè pwojè sou menm machin nan ki depann de gen plizyè vèsyon PHP enstale.

Ban-m pran abònman
Notifye nan
0 kòmantè
Aliye komantè
Wè tout kòmantè
0
Ta renmen panse ou, tanpri fè kòmantè.x