Make Composer Auto-Update Itself

 

Composer is a great package manager for PHP. It allows us to install, update and upgrade third-party libraries. For those who don't know about composer, here is a good beginners guide on the topic.
Now we know once in a while, composer needs to update its build so it can have the latest security fixes and patches. This can sometimes get annoying as most people (points to self) forget to update their build. If you have used composer for a period longer than 60days, you might have seen this message.
Warning: This development build of composer is over 60 days old. It is recommended to update it by running "C:\ProgramData\ComposerSetup\bin\composer.phar self-
update" to get the latest version.
This quick tip article aims to tackle that problem by letting composer auto-update itself. For composer to auto-update itself, all we have to is set up a command that runs on a certain interval. I set mine to every Saturday. I assume you know composer and have it installed. For more info, check out this article.

# Setup Composer auto-update on Linux

To make composer auto-update on any Linux-based computer, all we need to do is setup a cron task. Don't know about cron, check out this DigitalOcean article. Open your cronfile and add the command.
5 8 * * Sat /path/to/composer/executable self-update
For example, since my composer executable is in usr/bin, mine cron task will look like this.
5 8 * * Sat /usr/bin/composer self-update
This lets composer update itself every Saturday morning at five minutes past eight.
Note: If you don't know where composer installed on your computer, you can open a terminal and run the following command to get composer's install path.
which composer
Since I use MinGW on windows to make Linux commands available for me, the result of running the above command looks like this.

# Setup Composer auto-update on Windows

As usual, setting up a task like this on windows takes a more graphical approach. On your windows machine, search for task scheduler.
In the right-hand panel on the screen, you should see a list of actions, click on create basic task. In the dialog that pops up, fill in the name and description of the task, click next.

The next section asks you to pick a trigger. Trigger is the event which fires an action. It could be when the computer starts, daily, weekly, monthly etc. Pick weekly and click next.

The next page asks you when to start the task, and the day of the week to trigger the task. For ours, we choose the default start time, input 1 in the "recur every" weeks field, check Saturday as the day of the week and click next.

A list of actions show up, now we can choose to start a program as the action to perform and click next.

On the screen that follows, there is a field for the path to composer.bat and another where we can add arguments. To get the path to composer.bat, in the command prompt type
where composer
and copy and paste the path to composer.bat in the Program/script field. In the Add arguments field, type in self-update or selfupdate either is fine and hit next.

The window that shows up next is the last and final screen, it shows an overview of the task you are creating.