Composerは、Node.JsのNPMやPythonのPIPに似たPHPプログラミング言語のアプリケーションレベルのパッケージマネージャーです。 Composerは、プロジェクトに必要なすべてのPHPパッケージをダウンロードして管理することにより、PHPソフトウェアと必要なライブラリのすべての依存関係を管理するための標準形式を提供します。 Laravel、Drupal、Magento、Symfonyなどの最新のPHPフレームワークで使用されています。
次のチュートリアルでは、composerをダウンロードしてインストールする方法と、Debian11Bullseyeでcomposerを操作するための基本的な方法を学びます。
- 推奨OS: Debian11ブルズアイ
- ユーザーアカウント: sudo特権を持つユーザーアカウント またはrootアクセス(suコマンド) 。
- 必要なパッケージ: wget、PHP5.3以降
オペレーティングシステムの更新
Debian 11を更新します 既存のすべてのパッケージが最新であることを確認するためのオペレーティングシステム:
sudo apt update && sudo apt upgrade
ルートまたはSudoアクセス
デフォルトでは、他のディストリビューションと比較して、Debianを使用して起動時にアカウントを作成すると、sudoersステータスを自動的に受け取りません。 ルートパスワードにアクセスできる必要があります suコマンドを使用するには または、DebianのSudoersにユーザーを追加する方法に関するチュートリアルをご覧ください。
依存関係のインストール
以下のパッケージは、composerをダウンロードして将来使用するために必要です。 PHPの特定のバージョンを使用している場合は、バージョンに合わせて以下のPHPパッケージを調整する必要があることに注意してください。たとえば、PHP8.0のインストールは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
PHPComposerのインストール
Composerチームは、システムにPHPComposerをインストールして構成するための公式のPHPスクリプトを作成しました。これをダウンロードするには、ダウンロードページにアクセスするか、Debianターミナルを開いて以下を実行します。
PHPメソッド:
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
WGETメソッド:
wget -O composer-setup.php https://getcomposer.org/installer
setup.phpをダウンロードしたら、composerをインストールします。これには2つの異なる方法があります。1つはcomposerをグローバルにインストールする方法、もう1つはPHPアプリケーションごとにインストールする方法です。
グローバルにインストールするには:
php composer-setup.php --install-dir=/usr/local/bin --filename=composer
chmod +x /usr/local/bin/composer
アプリケーションごとにインストールするには:
cd /example-project/php-application && mkdir -p bin
php composer-setup.php --install-dir=bin --filename=composer
chmod +x bin/composer
上記のように、PHPプロジェクトディレクトリに移動し、PHPコンポーザーをインストールするためのbinディレクトリを作成する必要があります。
完了したら、composerがインストールされていることと、それがどのビルドとバージョンであるかを確認します。
composer --version
Composer version 2.1.8 2021-09-15 13:55:14
次に、次のコマンドを使用して、composerのインストールをテストします。
composer
出力例:
______
/ ____/___ ____ ___ ____ ____ ________ _____
/ / / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/
/ /___/ /_/ / / / / / / /_/ / /_/ (__ ) __/ /
\____/\____/_/ /_/ /_/ .___/\____/____/\___/_/
/_/
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.
PHPComposerの操作方法
チュートリアルでは、PHPComposerをインストールしてその動作をテストする際のいくつかの重要な機能を紹介します。
まず、プロジェクトディレクトリを作成します:
mkdir ~/composer-test
cd ~/composer-test
次に、Packagistからパッケージを見つけるか、チュートリアルの例を使用して次のコマンドを実行し、新しいcomposer.jsonファイルを初期化して、carbonパッケージをインストールします。
composer require psr/log
出力例:
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
上記のように、composerは、必要な依存関係とともに、composer.jsonファイルを自動的に作成および更新します。または、既存のパッケージをインストールしたり、特定のバージョン番号にダウングレードしたりすることもできます。
例:
composer require psr/log=1.0
プロジェクトディレクトリで、lsコマンドを使用してその中のファイルとディレクトリを一覧表示できます。 :
ls -l
出力例:
./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
用語とリストされているファイルの内訳。
- composer.json –プロジェクトおよびすべてのPHP依存関係用に作成されたファイル。
- composer.lock –すべてのパッケージとバージョンのリストを含むファイル。
- ベンダー –依存関係がダウンロードおよびインストールされるディレクトリ。
プロジェクトでは、依存関係を更新する必要がある場合があります。これは、次のコマンドを実行して実行できます。
composer update
出力例:
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
上記のように、依存関係を更新する必要はありません。もしあれば、プロセスが始まります。
モジュールが不要になった場合は、composerremoveコマンドを使用してモジュールを削除できます。
composer remove psr/log
出力例:
./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
パッケージを削除する場合、インストールと同様に、削除すると、composer.jsonファイルが自動的に更新されます。これは、catコマンドを使用して確認できます。 。
cat composer.json
psr / logパッケージを削除する前の例:
{
"require": {
"psr/log": "1.0"
}
}
psr / logパッケージを削除した後の例:
{
}
PHPComposerをアップグレードする方法
PHP Composerの便利な機能は、利用可能な最新の安定バージョンに自己アップグレードできることです。次のコマンドを実行してアップグレードします。
composer self-update
出力例:
You are already using the latest available Composer version 2.1.8 (stable channel).
上記のように、PHPコンポーザーはすでに最新バージョンになっています。ただし、アップグレードが利用可能な場合は、アップグレードするように求められます。