Tip: Start typing to get instant search results.
How to Install PHP Composer Locally and Manage Its Version
PHP Composer is the most popular dependency manager for PHP. It allows developers to declare the libraries that a project requires and installs them automatically, while managing the dependencies between them. It is widely used in frameworks and CMS platforms such as Laravel, Magento, and others.
On our managed dedicated servers that host PHP websites, PHP Composer is always pre-installed at the system level. However, you may need full control over its version (for example, if a project requires a specific Composer version, or if you want to test a newer version without affecting the rest of the system). The solution is a local installation under your own Linux user, in the ~/.local/bin directory.
Step 1: Create the directory
If the ~/.local/bin directory does not already exist, create it:
mkdir -p ~/.local/binStep 2: Install Composer
Run the following commands to download and install Composer directly into your local directory:
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php composer-setup.php --install-dir=$HOME/.local/bin --filename=composer
rm composer-setup.php
With the --install-dir and --filename parameters, the installer places the composer executable directly in the correct directory.
Verify the installation:
which composer
composer --versionThe which composer command should return ~/.local/bin/composer, confirming that the local version is being used instead of the system-wide one.
Managing the Composer Version
Once Composer is installed locally, you have full control over its version using the self-update command.
Upgrade to the latest stable version:
composer self-updateUpgrade or downgrade to a specific version:
composer self-update 2.6.6Replace 2.6.6 with any version you need. You can find all available releases on the Composer GitHub page.
Stay on a major version branch:
# Stay on the v1.x branch (for legacy projects only)
composer self-update --1
# Stay on the v2.x branch (recommended)
composer self-update --2Roll back to the previous version:
composer self-update --rollbackUseful if a new version causes issues and you need to revert to the previous one immediately.