In this lab, you will install Composer, the PHP Dependency Manager, onto your Mac. We use Composer in our library videos for the basic plugin which includes Kint and Whoops. In your projects, you will use Composer to package up your project as well as load and manage dependencies for you and autoload your files.
Composer may change their installation process. Therefore, when in doubt, the master installation instructions are at this link: https://getcomposer.org.
This lab was done on March 27, 2016.
Composer serves two purposes in your projects:
- Dependency management for PHP
- File autoloading
Dependency Management for PHP
Using Composer, you can specify production and/or development packages which are stored locally in your project’s
assets/vendor folder. Running an update will update all of the packages specified in the composer file.
For Object Oriented Programming (OOP), Composer allows us to specify the location of the class files. Then, the files are automatically loaded when an object is instantiated. Instead of having
require() within your code, Composer abstracts away the file loading process. The result is clean, purposeful code.
It’s Time to Install Composer
Open your browser and navigate to https://getcomposer.org.
Go to Download Composer installer page. At the top of the page, simply copy and paste the code they provide into terminal. This code does the following for you:
- Downloads the installer onto your machine.
- Runs it.
- Then it removes it.
Return to the Global installation page. You need to follow the instructions here by copying
mv composer.phar /usr/local/bin/composer into your terminal. You may get a few errors. Let’s talk about those.
If you get a permissions error, then you need to run with
sudo, like this:
sudo mv composer.phar /usr/local/bin/composer. Then enter your password.
Directory does not exist
If you get an error that the
/usrmkdir -p /usr/local/bin. You may have to add
sudo at the start of it though if you get a permissions error:
sudo mkdir -p /usr/local/bin.
Try it out
When complete, type
composer. If you receive a list of commands, Composer is properly installed on your machine.
Updating and Uninstalling Composer
Before updating, you should check the current version of Composer on your local machine. To do this, type into your terminal application:
composer –-version or
Since Composer manages the updates of your dependencies, it is possible for you to update the dependencies with the command below:
If you do not currently have any active projects with dependencies managed by Composer in your working directory, then Composer will let you know it was unable to find a composer.json file.
To update Composer itself, you should run the command:
sudo composer self-update
This will run the update script for composer. Once it has completed the update, you will see something similar to the following:
And that is all there is to updating Composer and its dependencies.
To uninstall packages in Composer, run
composer remove <package-name>
Make sure to remove the “< >” from around the package name. This will uninstall the package.