know-how.dev

How to install composer on linux or mac

2 years ago 675

If you're using a Mac or Linux machine, there are two ways to install composer on your system: the official way, which is safer but longer, and a faster way with only one command.

Officially recommended way

php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php -r "if (hash_file('sha384', 'composer-setup.php') === '55ce33d7678c5a611085589f1f3ddf8b3c52d662cd01d4ba75c0ee0459970c2200a51f492d557530c71c15d8dba01eae') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
php composer-setup.php
php -r "unlink('composer-setup.php');"
sudo mv composer.phar /usr/local/bin/composer

The last step is optional and you can skip it if you don't want to install composer globally.

This method is safer, because it verifies SHA-384 checksums for downloaded installer. Note that the checksum in this example is for version 2.4.0; you can find an up-to-date checksum here.

Fast way

curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

Use this option if you need to install Composer more quickly. However, the installer will not verify the checksum, so use it with caution. It will install composer in /usr/local/bin directory and make it globally available, but if you don't want to - you can change that by removing the --install-dir option from the command line.