Installing Composer
I needed Composer recently to run a WordPress theme built on the latest version of Sage and having never used it before I found it confusing to install. But after figuring it out it’s not hard and doesn’t take long so to save you several minutes I’ve listed the installation steps below.
The main steps come from the Download Composer page — the four lines of code listed at the top there. However, we need to add some additional parameters to line 3; those parameters can be found in the installation section on the Docs page for Linux / Unix / macOS.
These parameters aren’t necessary but they make it a little easier because they rename the composer.phar file to just composer. And then we’ll also move the resulting file to your local bin directory so you can use the command globally. The reason for all of this is in my situation websites would tell you to run composer install
, but if you just followed the steps on the Download page you’d have to run php composer.phar install
instead (not to mention in the directory wherever you installed the file). I found this confusing, hence the extra steps.
So, all that to say, I’ve listed the steps to install Composer locally in the five sections below. You can run these commands from any place in Terminal as the end result will be moved to a specific place in your file system. But if you want a specific place I would recommend starting from your home directory, you can get to that by running this command: cd ~
.
1 Download the installer file
This just downloads the installer file from getcomposer.org. We’ll remove it after setup is complete
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
2 Verify the installer
Note: You should copy this command from Composer’s Download page; it’s the second line of the list of commands at the top. The hash may change from time to time. I’ve pasted the version that was there when I accessed the page.
php -r "if (hash_file('sha384', 'composer-setup.php') === '48e3236262b34d30969dca3c37281b3b4bbe3221bda826ac6a9a62d6444cdb0dcd0615698a5cbe587c3f0fe57a54d8f5') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
After running this you should see the message:
Installer verified
3 Run Installer File
Now run the installer file. Note the extra parameters we’ve added here. The --install-dir
doesn’t matter because we’re going to be moving it after we’ve installed it. I just have it setup to install to the current directory. The --filename
parameter renames the file to simply “composer”.
php composer-setup.php --install-dir=./ --filename=composer
You should get some sort of success message.
Mine said:
All settings correct for using Composer
Downloading...
Composer (version 1.8.4) successfully installed to: [MY_LOCAL_PATH]/composer
Use it: php ./composer
4 Remove the installer file
Now we can remove the installer file:
php -r "unlink('composer-setup.php');"
If you view the directory (or list the files in terminal with the ls
command) you should no longer see the composer-setup.php
file.
5 Move Composer file
Almost finished, the last step is to move the file to the local bin directory so we can use the command composer
from anywhere.
mv composer /usr/local/bin/composer
Similar to the last step, after running that command the composer file should no longer be in the current directory because it was moved to the /usr/local/bin directory. Now you should be able to run the composer
command via Terminal from any directory.