Composer is a PHP tool to manage dependencies in your PHP project. Using simple commands you can add, update or remove the dependencies.[toc]
Install methods
There is some ways to install it. I will describe some of them.
Brew
If you don’t have a brew, let’s go there. I’ve previously noted about what a brew is and how to install it.
Open your terminal and type:
brew install composer
After it you should see something like that:
==> Installing homebrew/php/composer
==> Downloading https://homebrew.bintray.com/bottles-php/composer-1.3.2.el_capitan.bottle.tar.gz
######################################################################## 100.0%
==> Pouring composer-1.3.2.el_capitan.bottle.tar.gz
/usr/local/Cellar/composer/1.3.2: 5 files, 1.7M
Let’s test it, try to run the command below:
composer --version
If you saw a number of version then everything has gone fine.
Downloading directly from the composer website
Copy and paste that link – https://getcomposer.org/composer.phar
– to your browser. It’s always the latest version of Composer.
After getting it, open your terminal to test it. You need just run that command:
php ~/Downloads/composer.phar --version
I assume that you don’t want to write … every time when you need to use a composer. Let’s move it to bin
directory.
cp ~/Downloads/composer.phar /usr/local/bin/composer
sudo chmod +x /usr/local/bin/composer
That second command makes your composer executable. Let’s try again, type that command:
composer --version
Have you seen the version?
Using docker
If you want you can install composer also as independent service in container. If you’ve read my previous note about installing docker on Mac, you can do it.
Composer’s Team provide the official image of composer in docker hub, so you don’t need to play with Dockerfile.
Everything that you need to work with this is going to terminal and run it:
docker run --rm --interactive --tty \
composer --version
The most important commands
At this moment, composer provides us more than 30 commands. In daily work, you will be working only with a few of them.
composer install
Assume that you want to install some framework, it could be symfony
. Each framework has a lot of dependencies, which you can install manually. However, you can also run composer install
in folder where you storage your project. And that command will install all dependencies.
composer update
You know each application that is being developed, owner fixes bugs, adds new improvements etc. If you always want to use the newest available version of dependencies, you need to run only this command
composer require
You are working on your a new hello world
app and you need to use some external library. As always you can just download a ZIP, unpack and start using it. But you can also run command composer require library-which-I-need:version-which-I-want
and it install that library and save it for the future as new dependency.
composer remove
Have you decided whether you don’t need library that you’ve installed lastly? Let’s remove it using that command, a syntax is similar to require command:
composer remove library-which-I-do-not-need
Last words
As you’ve seen working with composer is extremely easy. Now you can forget about installing dependencies manually, make your project a little bit smarter!