Install Composer on Mac OS X

May 6, 2016 PHP 中文版

Composer is a tool that focusing on dependency management in PHP. Thus, you can manage your PHP projects in an easier way and spend less time on dealing with library dependency by using it. This post is about how to install and use Composer on Mac OS X.



Install Composer

I’m going to follow the official instructions to install Composer, and there are two ways: install locally and globally. I will demonstrate how to install Composer globally. To install Composer by executing the following commands:

Then we should move Composer PHAR to /usr/local/bin, like this:

mv composer.phar /usr/local/bin/composer

After that, you can execute composer in anywhere.

Use Composer

Here are some useful Composer commands:

# Get the current version
comoposer -version

# Search a package
comoposer search <package-name>

# Remove a package
composer remove <package-name>

What’s more, one of convenient features provided by Composer is that we can use a well-defined composer.json to let Composer install packages automatically. Here is a composer.json example:

{
    ...
    "require": {
        "monolog/monolog": "1.0.*",
        "parse/php-sdk" : "1.0.*"
    }
    ...
}

Once you have a composer.json, you can execute the command in the place where you put the composer.json:

# Install packages
composer install

# Update packages
composer update

After you execute the command, Composer will install monolog and parse for the project. If you want to add more packages into the projects and composer.json, you can use the command below:

composer require <package-name>




You might also like:




If you have any suggestions, questions or even find some typos, feel free to contact me. Thank you! :)

zeckli.devforgalaxy@gmail.com   © 2015-2019 zeckli, thanks to Jekyll and GitHub.