When I first started learning PHP, I kept running into the same problem: installing other people’s code. I’d find a cool library online, but I had no idea how to include it in my project without copy-pasting dozens of files. That’s when I discovered Composer—a tool that makes handling PHP dependencies a lot easier.
Dependencies are pieces of code your project needs in order to work. Instead of writing everything yourself, you can use code others have already built. Composer helps you download, update, and organize those pieces automatically.
In this guide, I’ll show you how to install Composer on your Linux computer. I’ll keep it simple and explain what each part does. By the end, you’ll have Composer running smoothly and might even feel a bit like a code wizard (or at least a “composer-conductor”).
What Is Composer?
Let’s break it down.
Composer is a dependency manager for PHP. Think of it like a librarian for your code. If your project needs a specific book (like a login system or a mail library), Composer knows where to find it, brings it in, and keeps it on the right shelf.
Here’s what Composer helps you do:
- Add external PHP libraries
- Automatically load classes
- Update libraries when there’s a new version
Without Composer, you’d have to download each library manually, set up all the paths, and track updates yourself. Not fun. Composer makes it as easy as typing a few commands.
Why Use Composer?
Let’s be honest: PHP is flexible, but it can get messy fast. Composer keeps your project organized and consistent.
Here’s why I think Composer is useful:
- Saves time
You don’t have to copy code manually or write everything from scratch. - Reduces errors
It installs the correct versions of libraries so they work together. - Makes sharing easier
If someone else wants to use your project, they just runcomposer install
.
Compared to managing things manually, Composer is like using a GPS instead of asking strangers for directions.
What You’ll Need
Before installing Composer, make sure you have:
- A Linux computer (Ubuntu, Debian, Fedora, etc.)
- PHP installed (version 7.2 or later is best)
- Access to the terminal
- Internet connection (Composer needs to download files)
If you’re not sure whether PHP is installed, open a terminal and type:
php -v
If you see something like PHP 8.1.2
, you’re good to go. If not, you’ll need to install PHP first.
Step-by-Step: Installing Composer
Now let’s get Composer up and running. I’ll walk you through it slowly. Don’t worry—it’s easier than trying to untangle headphone wires.
1. Open Your Terminal
Use Ctrl + Alt + T if you’re on Ubuntu, or find “Terminal” in your applications.
2. Download the Installer
We’ll use PHP to download the Composer installer script:
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
This grabs the file from Composer’s website and saves it as composer-setup.php
.
3. Check the Installer (Optional but Smart)
This step checks if the installer file is safe. It’s like checking your pizza delivery before eating.
php -r "if (hash_file('sha384', 'composer-setup.php') === '<EXPECTED_HASH>') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
Now, you’re probably wondering, “Where do I get that <EXPECTED_HASH>
?”
You can get it from:
https://getcomposer.org/download/
Look for the SHA384 hash near the version you want. Copy it and replace <EXPECTED_HASH>
.
4. Install Composer Locally
This command installs Composer to your current directory:
php composer-setup.php
After that, you’ll see a new file called composer.phar
.
5. Move Composer to a Global Location
You probably want to use Composer from anywhere in the terminal. Let’s move it to /usr/local/bin/
:
sudo mv composer.phar /usr/local/bin/composer
Now you can just type composer
from anywhere.
Try It Out
Let’s test it:
composer
If it shows a bunch of helpful options and version info, you’re all set.
Congrats. You now have Composer installed.
How Composer Works (The Short Version)
When you use Composer, you’ll usually work with a file called composer.json
. This file lists the dependencies your project needs.
Here’s an example of what it might look like:
{
"require": {
"monolog/monolog": "^2.0"
}
}
This means: “I need the Monolog library, version 2.0 or newer.”
When you run:
composer install
Composer reads that file, downloads the libraries, and sets everything up.
If you want to add a new library:
composer require guzzlehttp/guzzle
Composer updates your files, and your project is ready to use Guzzle (a library for HTTP requests).
Composer vs Manual Work
Here’s a little comparison from my own experience:
Task | Without Composer | With Composer |
---|---|---|
Adding a library | Download zip, extract, move | composer require |
Autoloading code | Write require manually |
Composer does it for you |
Updating dependencies | Re-download each one | composer update |
Sharing your project | Zip whole folder | Share composer.json file |
Basically, Composer is like having a helpful intern who organizes your stuff, keeps it updated, and doesn’t complain.
Three Common Composer Commands
Once Composer is installed, here are three commands you’ll use the most:
composer install
Sets up all dependencies listed incomposer.json
.composer require vendor/package
Adds a new library to your project.composer update
Updates all packages to the latest version that matches your rules.
Three Things to Watch Out For
Composer is great, but here are some common mistakes I made when starting out:
- Forgetting
composer install
after cloning a project
If you skip this, the project won’t work properly. - Not committing
composer.lock
This file locks versions so others get the same setup. Don’t ignore it. - Trying to edit
vendor/
folder
That folder is for Composer. If you touch it, things might break.
Three Handy Tips
Here are some simple tips to make life easier:
- Use version control
Keepcomposer.json
andcomposer.lock
in Git or whatever tool you use. - Read error messages
Composer is polite. It usually tells you exactly what went wrong. - Explore Packagist
Packagist.org is the main website where PHP packages live. It’s like a library for libraries.
Final Thoughts
Installing Composer might seem like a small step, but it opens a big door. You’ll have access to thousands of packages, organized projects, and cleaner code.
Composer is like a pizza delivery app. Instead of making everything from scratch, you just tell it what you need, and it shows up ready to go. And unlike real pizza, you won’t gain any weight from downloading too many packages.
I hope this guide helped you get started without feeling overwhelmed. And hey, now that you’ve got Composer installed, you’re officially composing some beautiful code—Mozart would be proud.
Want me to show you how to use Composer with a real project next?