Before Composer arrived in 2012, PHP developers copied libraries in by hand and tracked versions themselves. Now one line adds a library such as Guzzle, and Composer pulls in everything it depends on.
The key files
composer.json: required libraries, version constraints and autoload settings.composer.lock: the exact installed version of every library. Commit it to Git.vendor/: the libraries themselves, plusvendor/autoload.php.
{
"require": {
"php": "^8.2",
"guzzlehttp/guzzle": "^7.9"
},
"autoload": {
"psr-4": { "App\\": "src/" }
}
}
The constraint ^7.9 means any version from 7.9 up to, but not including, 8.0: you get fixes without breaking changes. In your code, a single require __DIR__ . '/vendor/autoload.php'; makes every library available.
Essential commands
| Command | Use |
|---|---|
composer require vendor/package |
Add a library |
composer install |
Install what’s in the lockfile |
composer install --no-dev --optimize-autoloader |
Production install without dev tools |
composer update vendor/package |
Update a single library |
composer dump-autoload |
Rebuild the autoload map |
Common problems
- Running out of memory:
updatein particular is memory-hungry. Run it locally, commit the lockfile, and useinstallon the server: see Composer memory limit. - PHP version mismatch: a message like
requires php ^8.2means the command-line PHP is older than required, and it may differ from the version your site uses. If your computer runs a newer PHP than the server, setconfig.platform.phpincomposer.jsonto the server’s version so Composer doesn’t pick libraries that won’t run there. Class not found: stale autoload map or wrong namespace: check the error page.
On ArabyHost
Composer is available on AI/Developer plans along with SSH jailed in CageFS, so you can run composer install --no-dev right in your project folder, with PHP 8.1 to 8.3. It runs within your account’s resource limits, so avoid a full composer update on the server. Other plans don’t include SSH: run Composer on your computer and upload the project, vendor included, as a ZIP. For a Laravel project, follow the Laravel deployment guide.
FAQ
What's the difference between composer install and composer update?
install sets up exactly the versions recorded in composer.lock, which is what you want on a server. update looks for newer versions within your constraints and rewrites the lockfile, so run it locally and test.
Should I commit the vendor folder?
Usually not: add it to .gitignore and run composer install on the server. The exception is hosting without a command line, where you upload vendor ready-made.
Why do I get Class not found after adding a new file?
The autoload map is out of date. Run composer dump-autoload and check that the file's name and path match the class namespace.