Skip to content
Log In Sign Up info@muvcloud.com

Composer in One Paragraph

Composer reads a project's composer.json, finds versions of each required library that fit together, downloads them from Packagist (the main public package repository) into vendor/, and writes the exact versions it chose into composer.lock. Frameworks such as Laravel and Symfony are installed and updated this way. Composer works per project; it does not install libraries system-wide.

composer.json and composer.lock

A small composer.json:

{
    "require": {
        "php": ">=8.3",
        "monolog/monolog": "^3.0"
    },
    "autoload": {
        "psr-4": { "App\\": "src/" }
    }
}
  • The constraint ^3.0: any 3.x release from 3.0 up, but not 4.0.
  • composer.lock: the exact version of every package, including dependencies of dependencies. It makes every install of the project identical.
  • What to commit: both files go into Git; the vendor/ folder does not. See how to use .gitignore.

install vs update

CommandWhat it does
composer installInstalls the versions in the lock file
composer updatePicks newer allowed versions, rewrites the lock
composer require a/bAdds a package and installs it
composer remove a/bRemoves a package

Run update deliberately, in development, then test and commit the new lock file. Servers only ever run install.

Install Composer on a Server

Composer needs the PHP command-line binary, and works best with unzip and git installed; see how to install PHP on Ubuntu. There are two routes:

  • Distribution package: sudo apt install composer on Debian and Ubuntu. It is maintained by the distribution, but often older than the current release.
  • The project's installer: the download page on getcomposer.org gives a short set of commands. They download the installer with PHP, compare its SHA-384 hash with the hash the Composer project publishes, run the installer only if the two match, and delete it. Copy those commands from the page each time, because the hash changes with every installer release. Then move composer.phar to /usr/local/bin/composer.

Check the result with composer --version.

Do Not Run Composer as Root

Packages can include plugins and scripts that run during install and update with your permissions. Run as root, a compromised package controls the whole server. Composer prints a warning when it runs as root. Run it as the user that owns the project folder instead:

sudo -u deploy composer install

A Production Deploy

cd /var/www/app
composer check-platform-reqs --lock --no-dev
composer install --no-dev --optimize-autoloader --no-interaction

check-platform-reqs confirms the server's PHP version and extensions meet the requirements; --lock reads them from the lock file, so it works before vendor/ exists. --no-dev skips the packages under require-dev, such as test tools, which do not belong on a live server. --optimize-autoloader builds a class map for faster loading. Run composer audit to list installed packages with known security advisories or abandoned packages.

Autoloading

One line at the start of your application loads every installed library and your own classes:

require __DIR__ . '/vendor/autoload.php';

After you change the autoload section, run composer dump-autoload.

Common Problems

  • Missing PHP extension: the error names a requirement such as ext-mbstring. Install the matching package, for example php-mbstring.
  • Lock file out of date: someone changed composer.json without updating the lock. Fix it in development and commit the new lock.
  • Out of memory: large updates need more memory. Run them in development, not on a small production server.

To keep deploys repeatable, pull code with Git and run composer install afterwards; see GitHub SSH key for server access to a repository.

Updated:

Linux VPS with Full Root Access

MUVCloud cloud servers run on KVM with Enterprise NVMe SSD storage and full root access, in Turkey, the Netherlands, and the USA.

Cloud VPS Plans Starting at2.99 $/mo Cheap Linux VPS

No Limits on Service or Support

You are not on your own as your projects grow. Our support engineers are ready 24/7, on several channels, whenever you need them.

Support specialist
  • 100% customer satisfaction goal
  • Our own hardware
  • 24/7 ticket and email support
  • 4 countries with locations