Composer is a useful tool for PHP developers, that simplifies the process of managing PHP application dependencies. If you’re working on MacOS and need to set up Composer, this guide will walk you through the steps in simple terms. Whether you’re a beginner or a seasoned developer, installing Composer on your MacOS system is a straightforward process that can enhance your development environment.
Why Use Composer?
Composer is a dependency manager for PHP. It allows you to declare the libraries your project depends on and it will manage (install/update) them for you.
- Efficient management of project dependencies
- Streamlines package updates
- Simplifies autoloading of packages
Prerequisites
- Shell access to a running macOS
- PHP 5.3 or higher version must be installed
Step 1: Download and Install Composer on macOS
To install Composer, follow these steps:
- Open a terminal and swtch to a directory where you have rights to install software (such as your home directory).
- Run the following command to download Composer setup script:
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
- Next, execute the installer script as below:
php composer-setup.php
- To make Composer globally accessible, move the composer.phar to a directory in your PATH:
mv composer.phar /usr/local/bin/composer
- Verify the installation by typing:
composer
If Composer is installed correctly, you will see a list of commands.
Step 2: Upgrade PHP Composer
The PHP composer provides a command-line option (self-update) to upgrade itself. You can simply run the below command from the terminal to upgrade compose on your macOS.
sudo composer self-update
Step 3: Uninstall PHP Composer (Optional)
As the composer is configured with a single file, you can simply remove it from your system.
sudo rm /usr/local/bin/composer
Conclusion
Installing Composer on MacOS is a quick process that can greatly aid in managing PHP dependencies. By following the steps above, you’ll be able to set up Composer and start using it for your PHP projects. This setup not only ensures a robust development environment but also streamlines your project management tasks.
8 Comments
thx
Rahul, your name describes your way of updating composer. “Do not run Composer as root/super user! See https://getcomposer.org/root for details”
You also have to create the bin directory first. Like so:
sudo mkdir /usr/local/bin
Then
sudo mv composer.phar /usr/local/bin/composer
chmod +x /usr/local/bin/composer
You are right
You might have to create the /bin directory (If on Catalina)
Run using homebrew. Run the command brew install composer
Thank you for information but I have a problem when installing composer.
I’m using MacOS Catalina and when I try to use composer globally I can’t. I can’t move the phar file.
Any suggestion?
Thank you
You should use sudo mv composer.phar /usr/local/bin/composer