How to update composer to its latest version

In this post, I am guiding you through the process of updating/installing composer on your Linux system. Also, I assume that you have the latest PHP version running on your system and it is accessible from the shell.

The first step is to download the composer installer:

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

Next, you should verify the authenticity of the installer as follows:

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

If the verification is successful, you can install composer on your system by running the below command:

$ php composer-setup.php --install-dir=bin --filename=composer
  • php composer-setup.php will download composer.phar file into your system
  • –install-dir flag allows you to set the target location for the installation.
  • –filename flag let you rename the downloaded composer.phar file to the name that you wish. Usually, we call it as composer.

Now you should have a working composer installation on your system.

Instead of the above method, you can also install composer by running below commands:

$ php composer-setup.php
$ [sudo] mv composer.phar /path/to/composer

In this second method, we have intentionally left out the –install-dir and –filename flags. That results in composer.phar file getting downloaded to the same location as where the composer-setup.php is located. After the .phar file has been downloaded, we have used the mv command to move the downloaded composer.phar file into the target install location (/path/to/composer) as composer. If you already have a previous composer installation on your system, you should set that path as the target location for the new installation. If you don’t know your current composer location, run which composer to learn about the location.

If the installation was successful, you should remove the composer-setup.php script with the below command:

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

Finally, you can verify the installation by running:

$ composer --version

Or

$ composer -V

If you see something like Composer version 2.0.12 2021-04-01 10:14:59, then your installation has been a success.

Was this helpful?
+1
0
+1
0
+1
1
+1
0
+1
0
+1
0
+1
0