Composer: Managing PHP Packages

PHP ~ 8 minutes

# Meet Composer

Hello and welcome! In this tutorial, we will learn Composer. Composer is the package manager for PHP. It helps you add code written by other developers into your own project, with just one command.

Imagine you need to send emails, work with dates, or talk to a payment service. You do not need to write all of that yourself. Other people have already built it and shared it as a package. Composer downloads the package for you. It also downloads every other package that it needs. These are called dependencies.

Laravel itself is built with Composer. So if you know Composer well, you will understand your Laravel projects much better.

What you will learn

  • How to check that Composer is installed
  • How to start a new project with composer.json
  • How to add, update and remove packages
  • How autoloading works
  • How to use the lock file in a team

You can find the full guide at getcomposer.org. Let us begin.

Check your Composer version
Code Copied to Clipboard

# Install Composer if needed

If you see a version number, then Composer is ready. Very good! If you see command not found, please install it first. Go to the download page and follow the steps for your system. On Windows, there is a simple installer. On Linux and macOS, you can run a small script from the same page.

You also need PHP on your computer. You can check this by running php -v. Composer needs a modern PHP version, so please keep it updated.

Now, let us make a small new project. We will make a folder first, then go inside it. After that, we will ask Composer to prepare the project for us.

Start a new project
Code Copied to Clipboard

# Answer the questions

Composer will now ask you some short questions. Please answer them one by one. You can press Enter to accept the default answer when you are not sure.

Questions you will see

  • Package name, like yourname/my-app
  • Description, a short sentence about the project
  • Author, your name and email
  • Minimum stability, where you can press Enter
  • Package type, where you can press Enter

At the end, Composer creates a file called composer.json. This file is the heart of your project. It lists everything your project needs. Let us look at it.

A simple composer.json
Code Copied to Clipboard

# Reading composer.json

Do not be afraid of this file. It is only a list. Let us read it together.

The name and description tell people what the project is. The require part lists the packages we need. Right now it only asks for a PHP version. The ^8.1 sign means version 8.1 or any newer 8.x version. This sign is called the caret, and it is a very common way to write versions.

The autoload part tells PHP where to find our own classes. Here, the namespace App points to the folder src. We will see how useful this is soon.

About version rules

Versions have three numbers, like 2.5.1. They are major, minor and patch. Most packages follow semantic versioning. A patch change fixes bugs. A minor change adds features and stays compatible. A major change may break old code.

The caret ^2.5 allows 2.5 up to, but not including, 3.0. This way, you get safe updates, and you avoid big surprises. If you want to read more, see the versions guide.

# Add your first package

Now comes the fun part. Let us add a package. We will use Carbon. It is a very popular package that makes dates and times easy to handle. Packages are published on a big website called Packagist. Composer searches there by default.

The package name has two parts: the vendor and the project, joined with a slash. For Carbon, it is nesbot/carbon.

Please run the command below inside your project folder. Composer will download Carbon and all of its dependencies. It will also update composer.json for you.

Require a package
Code Copied to Clipboard

# What changed in your folder

Let us see what Composer has done. Please look at your project folder. You will now find three new things.

New files and folders

  • vendor/ holds all the downloaded packages
  • composer.lock saves the exact version of every package
  • vendor/autoload.php loads all classes for you

The composer.json file also has a new line inside require. It shows nesbot/carbon with a version rule.

You never edit anything inside the vendor folder by hand. Composer controls it, and it can rebuild it at any time. We usually do not put it in Git.

Use the package
Code Copied to Clipboard

# How autoloading works

Look at the third line of the code above. We load only one file, which is vendor/autoload.php. After this, we can use any class from any package. We do not need to write many require lines.

This magic is called autoloading. When PHP sees a class name that it does not know, it asks the Composer autoloader. The autoloader finds the right file and loads it. The rule used for this is called PSR-4. It matches a namespace to a folder.

This works for our own code too. If you make a class App\Greeter in the file src/Greeter.php, it will load by itself. When you add new autoload rules to composer.json, you must rebuild the autoloader. Let us do that now.

Rebuild the autoloader
Code Copied to Clipboard

# Working in a team

Now, imagine a friend wants to work on your project. You send them your code, but without the vendor folder. That is fine! Your friend only needs composer.json and composer.lock.

Your friend runs the install command. Composer reads the lock file and downloads the exact same versions that you used. So the code works in the same way on every computer. This is the main reason why we always save composer.lock in Git.

The same command is used when you set up a Laravel project from GitHub, and when you deploy to a server.

Install from the lock file
Code Copied to Clipboard

# Update packages

Over time, authors fix bugs and add features. To get the new versions, we use the update command. Composer checks the rules in composer.json, finds the newest versions that are allowed, and writes them into composer.lock.

Please note the difference. The install command follows the lock file. The update command changes the lock file. So, run update only when you really want newer versions, and then test your app.

You can also update only one package by writing its name after the command. This is safer when you are not sure about the others.

Update to newer versions
Code Copied to Clipboard
Safe habits with Composer

Please keep these important points in mind. They will protect your projects.

Good habits

  • Always commit composer.json and composer.lock to Git
  • Never commit the vendor folder
  • Use composer install on servers, and not composer update
  • Check a package on Packagist before you use it, for downloads and recent updates
  • Keep your packages updated for security fixes

A package runs code inside your app. So only use packages that you trust. Composer can also check for known problems. Run composer audit from time to time to see security warnings.

# Development only packages

Some tools are needed only while you build the app, and not on the live server. Testing tools are a good example. For these, we use the --dev flag. Composer saves them in a separate require-dev part of composer.json.

On a live server, you can skip them with composer install --no-dev. This keeps the server small and a little safer.

Let us add PHPUnit, the most popular testing tool for PHP, as a dev package.

Require a dev package
Code Copied to Clipboard

# Remove a package

Sometimes you no longer need a package. Do not delete files from vendor by hand. Please let Composer do it. It removes the package, cleans its unused dependencies, and updates both composer.json and composer.lock.

Let us remove Carbon, as an example. After this, you can add it back at any time with the require command.

Remove a package
Code Copied to Clipboard

# Summary and next steps

Congratulations! You now know how to manage PHP packages in a professional way. Let us remember the main points.

Summary

  • Composer is the package manager for PHP, and Packagist is its store
  • composer.json lists what the project needs
  • composer require adds a package, and composer remove deletes it
  • composer install follows the lock file, and composer update changes it
  • vendor/autoload.php loads all classes using PSR-4 autoloading

What to learn next

  • Composer scripts, which run commands after install
  • Creating and publishing your own package
  • Private packages for your company

The Composer documentation has answers to many more questions. Keep practicing, and thank you for learning with us!