$ dpkg -l | grep composer
ii composer 2.7.1-2 all dependency manager for PHP
...
PHP Composer kann entweder lokal (pro Projekt) oder global (für alle Projekte) installiert werden. Wir bevorzugen eine lokale Installation:
$ mkdir my_project
$ cd my_project
$ php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
$ php -r "if (hash_file('sha384', 'composer-setup.php') === 'dac665fdc30fdd8ec78b38b9800061b4150413ff2e3b6f88543c636f7cd84f6db9189d43a81e5503cda447da73c7e5b6') {
echo 'Installer verified';
} else {
echo 'Installer corrupt'; unlink('composer-setup.php');
} echo PHP_EOL;"
Installer verified
$ php composer-setup.php
All settings correct for using Composer
Downloading...
Composer (version 2.8.5) successfully installed to: /tmp/my_project/composer.phar
Use it: php composer.phar
$ php -r "unlink('composer-setup.php');"
$ ls -l
-rwxr-xr-x 1 oli oli 3060478 Jan 24 20:49 composer.phar
Um zu schauen ob PHP Composer sauber funktioniert:
$ php ./composer.phar
______
/ ____/___ ____ ___ ____ ____ ________ _____
/ / / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/
/ /___/ /_/ / / / / / / /_/ / /_/ (__ ) __/ /
\____/\____/_/ /_/ /_/ .___/\____/____/\___/_/
/_/
Composer version 2.8.5 2025-01-21 15:23:40
Usage:
command [options] [arguments]
Options:
-h, --help Display help for the given command. When no command is given display help for the list command
-q, --quiet Do not output any message
-V, --version Display this application version
--ansi|--no-ansi Force (or disable --no-ansi) ANSI output
-n, --no-interaction Do not ask any interactive question
--profile Display timing and memory usage information
--no-plugins Whether to disable plugins.
--no-scripts Skips the execution of all scripts defined in composer.json file.
-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
audit Checks for security vulnerability advisories for installed packages
browse [home] Opens the package's repository URL or homepage in your browser
bump Increases the lower limit of your composer.json requirements to the currently installed versions
check-platform-reqs Check that platform requirements are satisfied
clear-cache [clearcache|cc] Clears composer's internal package cache
completion Dump the shell completion script
config Sets config options
create-project Creates new project from a package into given directory
depends [why] Shows which packages cause the given package to be installed
diagnose Diagnoses the system to identify common errors
dump-autoload [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 Display help for a command
init Creates a basic composer.json file in current directory
install [i] 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 List commands
outdated Shows a list of installed packages that have updates available, including their latest version
prohibits [why-not] Shows which packages prevent the given package from being installed
reinstall Uninstalls and reinstalls the given package names
remove [rm|uninstall] Removes a package from the require or require-dev
require [r] Adds required packages to your composer.json and installs them
run-script [run] Runs the scripts defined in composer.json
search Searches for packages
self-update [selfupdate] Updates composer.phar to the latest version
show [info] Shows information about packages
status Shows a list of locally modified packages
suggests Shows package suggestions
update [u|upgrade] Updates your dependencies to the latest version according to composer.json, and updates the composer.lock file
validate Validates a composer.json and composer.lock
Falls die folgende Fehlermeldung auftritt:
$ php composer.phar update
Composer is operating significantly slower than normal because you do not have the PHP curl extension enabled.
fehlt php-curl
in der richtigen Version:
$ php --version
PHP 8.4.3 (cli) (built: Jan 19 2025 14:20:58) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.4.3, Copyright (c) Zend Technologies
with Zend OPcache v8.4.3, Copyright (c), by Zend Technologies
$ sudo apt install php8.4-curl
Ansonsten kommt folgende Meldung:
$ php composer.phar update
Composer could not find a composer.json file in /tmp/my_project
To initialize a project, please create a composer.json file. See https://getcomposer.org/basic-usage
Erstellen der Datei composer.json
:
// Older version requires PHP v7.2
$ cd my_project
$ cat << _EOF >composer.json
{
"require": {
"monolog/monolog": "3.8.*"
}
}
_EOF
$ ls -l
-rw-rw-r-- 1 oli oli 62 Jan 24 21:03 composer.json
-rwxr-xr-x 1 oli oli 3060478 Jan 24 20:49 composer.phar
Installation und Update der Bibliothek (Library):
$ php composer.phar update
Loading composer repositories with package information
Updating dependencies
Lock file operations: 2 installs, 0 updates, 0 removals
- Locking monolog/monolog (3.8.1)
- Locking psr/log (3.0.2)
Writing lock file
Installing dependencies from lock file (including require-dev)
Package operations: 2 installs, 0 updates, 0 removals
- Downloading psr/log (3.0.2)
- Downloading monolog/monolog (3.8.1)
- Installing psr/log (3.0.2): Extracting archive
- Installing monolog/monolog (3.8.1): Extracting archive
11 package suggestions were added by new dependencies, use `composer suggest` to see details.
Generating autoload files
1 package you are using is looking for funding.
Use the `composer fund` command to find out more!
No security vulnerability advisories found.
oder, wenn es nicht das erste mal ist:
$ php composer.phar update monolog/monolog
Loading composer repositories with package information
Updating dependencies
Nothing to modify in lock file
Writing lock file
Installing dependencies from lock file (including require-dev)
Nothing to install, update or remove
Generating autoload files
1 package you are using is looking for funding.
Use the `composer fund` command to find out more!
No security vulnerability advisories found.
Lokale Paket sieht man wie folgt:
$ php composer.phar show
monolog/monolog 3.8.1 Sends your logs to files, sockets, inboxes, databases and various web services
psr/log 3.0.2 Common interface for logging libraries
Und die Plattform weiten Paket wie folgt:
$ php composer.phar show --platform
composer 2.8.5 Composer package
composer-plugin-api 2.6.0 The Composer Plugin API
composer-runtime-api 2.2.2 The Composer Runtime API
ext-calendar 8.4.3 The calendar PHP extension
...
Erstellen eines simplen, kleinen PHP-Programms:
$ cd my_project
$ cat << '_EOF' >test.php
<?php
require __DIR__ . '/vendor/autoload.php';
$log = new Monolog\Logger('name');
$log->pushHandler(new Monolog\Handler\StreamHandler('app.log', Monolog\Logger::WARNING));
$log->warning('Foo');
?>
_EOF
$ ls -l
-rw-rw-r-- 1 oli oli 62 Jan 24 21:16 composer.json
-rw-rw-r-- 1 oli oli 6898 Jan 24 21:17 composer.lock
-rwxr-xr-x 1 oli oli 3060478 Jan 24 21:15 composer.phar
-rw-rw-r-- 1 oli oli 201 Jan 24 21:17 test.php
drwxrwxr-x 5 oli oli 4096 Jan 24 21:17 vendor
$ php -f test.php
$ cat app.log
[2025-01-24T20:31:45.921841+01:00] name.WARNING: Foo [] []