Skip to main content
TrustServers Knowledge Base

Tip: Start typing to get instant search results.

Table of Contents

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/bin

Step 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 --version

The 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-update

Upgrade or downgrade to a specific version:

composer self-update 2.6.6

Replace 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 --2

Roll back to the previous version:

composer self-update --rollback

Useful if a new version causes issues and you need to revert to the previous one immediately.

 

Was this article helpful?
Please Share Your Feedback
Πώς μπορεί να βελτιωθεί το άρθρο;
Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.