Setting up Composer on Ubuntu (22.04+)

Composer is a PHP Package Manager. Really it is managing code level dependencies and installations/downloads of vendor (external) code.

Assuming you have completed a basic server setup, like described here

When installing composer, really our best friend simply is https://getcomposer.org/ and more specifically the https://getcomposer.org/download/ page.

Assuming we are on the command line on our Ubuntu (Linux) box, we can execute the instructions shared there (check there if fails, as the hash version would have updated).

php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"

php -r "if (hash_file('sha384', 'composer-setup.php') === 'edb40769019ccf227279e3bdd1f5b2e9950eb000c3233ee85148944e555d97be3ea4f40c3c2fe73b22f875385f6a5155') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"

php composer-setup.php

php -r "unlink('composer-setup.php');"

If you trust this, like we do, you can even just ignore the second line, it will always work, no matter the hash.

Executing the commands should result in the following

Now you have a composer.phar file, sitting where ever you are on your system. Its always advised to move it to the local bin so that the system knows and can simply use composer instead of right now needing to know the path and full name of the actual file (composer.phar).

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

Wondering what a “phar” is? Well, its nothing but a PHP Archive. A collection of PHP files that work together without needing any dependencies to execute all code it consists of.

That’s it, you can now use Composer!

composer -v

Should show

Proof you have it all at your fingertips. See you next time!


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *