What is composer?

Composer has become PHP's package & dependency manager. Why you should use it.

On a high level

A few years back, bower has dominated packages. The need to reuse libraries & other packages for the web has grown massively since then. While the idea wasn't revolutionary (things like brew & apt-get have been around for quite some time), web development introduced the need for project-specific setup. Node has npm, Ruby gems, PHP composer etc. But what do these managers do and why do you need them?

Dependency management

Managing dependencies manually is nearly impossible: a particular SDK requires a particular api-library which in turn requires a particular version of a curl-wrapper which uses a particular library to deal with headers. Since we move away from monolithic packages, manual dependency management would be extremely painful. But if every library would hold standardized information on what it requires, we would be able to track these dependencies, wouldn't we? With composer, such a file would be called composer.json. It helps composer to install, remove or update dependencies for the current project.


Installing dependencies is one thing, but how do I use them? In PHP, I have a couple of possibilities. And certainly the last thing I want to do is to write a thousand include-statements in every file. Especially, as noted above, if I don't always know what I need. PHP can resolve files by namespace with the use of autoloading. Composer automates this process for us by generating one single autoload-file which in turn let's us have access to all installed packages. You can make your own namespaces and include them into your composer.json to include your own files in the same way. Let's walk through it:


The first thing you want to do is to install composer and make sure it is included in your system's PATH variable.


Make sure php is within your PATH by opening up a terminal (Windows: cmd) and trying: php -v. The unleash the full power of composer, make sure you have GIT installed as well: `git --version'. If either of those commands don't work for you, make sure you have php & git installed and explore how to add them to the PATH variable of your system.

Installing composer

Composer offers a simple installer for Windows, if you don't want to use a command line. In general, composer can be installed only using php: official guide

Verifying installation

After installation, open anew terminal and type composer. This should display common commands.

First project

To give it a test-run, let's create a new folder/project. Enter the path in a terminal and run composer init. You will notice that a composer.json will be created which holds basic information about your project.

Now we require our first dependency: composer require vlucas/phpdotenv. You will notice that composer installs three packages in order to deal with dependencies of this package. Additionally, composer created a vendor-folder to hold these packages.

NOTE: This package is used to easily integrate .env-file variables as system-environment variables.

Next, let's create an index.php

require 'vendor/autoload.php';

We now have "access" to all installed packages via namespace. e.g.

$dotenv = Dotenv\Dotenv::createImmutable(__DIR__);

As you can see, Dotenv\Dotenv will be loaded using the autoloader and we don't have to worry about the dependencies either.


Lastly, the package repository for composer can be found at packagist.org

neoan @neoan

651 readers
php composer beginner
On topic

Install PHP 7.4 on Ubuntu

Finally PHP 7.4 is out! You have read about the new features, you have followed externals, you have gathered ideas on how new capabilities will save time. Let's get it running.

Static content pages - still the fastest web-experience

Tutorial: How to utilize blua.blue to generate static content for your website.

Scaffolding REST APIs with JWT authentication

Ever had the need for your own backend while developing your web-app?

Git: globally change GitHub-remotes from git@ to https

Does your IDE or composer set remote repositories to ssh rather than https? Or are repositories you are using set up that way? You are not alone. Let's fix it once and for all!

VueJS & neoan3: a love story.

Setting up neoan3 to play nice with Vue isn't hard. Here is how the two frameworks are combined to support fast, dynamic and rapid development.

Install PHP 8 on Ubuntu

PHP8 has finally been released and it is FAAAAST! Time to update.

SEO strategies for blua.blue

How to get your content listed where you want it to.

MySQL: ERROR1364 fix

The painful realization of why people use containers.

blua.blue PHP SDK

Create your own blog.

How to Build an Express App in Node That Reads From a Headless CMS

A headless CMS lets you design your own front-end, sometimes your own back-end. Let's set up a small application to get us started. Beginner friendly. Approx. 20 min. to reproduce / follow along

MySQL in PHP - how to deal with databases

How I handle MYSQL database transactions in PHP

Cross publishing to dev.to

How to publish your content to dev.to from blua.blue - hopefully

How to install global npm packages without sudo on Ubuntu

Running npm on a server can be painful. Privileges are there for a reason, and so is sudo. Running npm with sudo is not the solution.

Transformer - falling in love with PHP's magic methods all over again

PHP's magic functions have been around for a long time. But there were always good reasons to avoid them. Transformer is a practical and reliable way to make use of the most common operations we program: CRUD operations.