Using Composer via SSH: PHP Dependency Management
Composer is the standard package manager for PHP. It makes installing libraries and frameworks a breeze. At Theory7, we support Composer on all our hosting packages with SSH access. In this guide, you will learn to use Composer effectively for your PHP projects.
What is Composer?
Composer manages dependencies for PHP projects. Instead of manually downloading and updating libraries, you let Composer do this automatically. Benefits:
- Automatic updates of dependencies
- Dependency resolution - conflicts are resolved automatically
- Autoloading - classes are loaded automatically
- Version control - lock exact versions
Popular tools that use Composer: Laravel, Symfony, WordPress plugins, PHPUnit.
Checking Composer Availability
On Theory7 servers, Composer is usually already installed. Check with:
composer --version
If Composer is not found:
which composer
Or try the full path:
/usr/local/bin/composer --version
Basic Composer Commands
Starting a New Project
For a new project with composer.json:
composer init
Or install a framework directly:
composer create-project laravel/laravel mijnproject
Installing Dependencies
If you clone a project with an existing composer.json:
composer install
This installs all dependencies specified in composer.lock.
Adding a New Package
composer require vendor/package
Example:
composer require monolog/monolog
composer require guzzlehttp/guzzle
Development Dependencies
For packages only needed during development:
composer require --dev phpunit/phpunit
Removing a Package
composer remove vendor/package
Updating Everything
composer update
Or a specific package:
composer update vendor/package
Composer for Production
Optimized Installation
For production servers:
composer install --no-dev --optimize-autoloader
What these flags do:
- --no-dev - Skip development dependencies
- --optimize-autoloader - Generate optimized class map
Autoloader Optimization
After deployment:
composer dump-autoload --optimize
This significantly speeds up class loading.
Composer.json Structure
A basic composer.json:
{
"name": "mijnbedrijf/mijnproject",
"description": "Mijn PHP project",
"require": {
"php": ">=8.1",
"monolog/monolog": "^3.0"
},
"require-dev": {
"phpunit/phpunit": "^10.0"
},
"autoload": {
"psr-4": {
"App\\": "src/"
}
}
}
Version Constraints
- ^3.0 - Compatible with 3.0 and above, but not 4.0
- ~3.0 - Minimum 3.0, maximum 3.x
- 3.0.* - Any 3.0.x version
- >=3.0 - 3.0 or higher
- 3.0.5 - Exactly this version
Composer.lock
Composer.lock locks exact versions. This ensures everyone uses the same versions.
Important:
- Commit composer.lock to version control
- Use composer install (not update) in production
- composer update only locally for conscious updates
Troubleshooting
Memory Limit Errors
COMPOSER_MEMORY_LIMIT=-1 composer install
Or adjust php.ini:
php -d memory_limit=-1 /usr/local/bin/composer install
Timeout During Installation
composer config --global process-timeout 600
Cache Issues
Clear the Composer cache:
composer clear-cache
Platform Requirements
If your local PHP version differs from production:
{
"config": {
"platform": {
"php": "8.1"
}
}
}
Composer with Shared Hosting
On shared hosting with limited resources:
- Install dependencies locally
- Upload vendor/ folder to server
- Or use composer with memory limit override
Composer Scripts
Automate tasks with scripts in composer.json:
{
"scripts": {
"post-install-cmd": [
"php artisan optimize"
],
"test": "phpunit"
}
}
Run with:
composer test
Private Packages
For private repositories:
{
"repositories": [
{
"type": "vcs",
"url": "git@github.com:mijnbedrijf/privaat-package.git"
}
]
}
Ensure SSH keys are configured correctly.
Related Articles
- SSH connection from Mac/Linux
- Firewall configuration (UFW)
- Server backup strategy
- PHP-FPM configuration
Need Help?
We are here for you! Are you facing any issues or have questions? Our support team is happy to assist you personally. Send us a message via the ticket system - we usually respond within a few hours and are happy to help.
0 van 0 vonden dit nuttig