68
votes

I am trying to update Composer without any luck!

What I have tried:

$ composer self-update

[InvalidArgumentException] Command "self-update" is not defined.

$ sudo -H composer self-update

[InvalidArgumentException] Command "self-update" is not defined.

$ sudo apt-get install composer

Reading package lists... Done Building dependency tree Reading state information... Done composer is already the newest version. The following packages were automatically installed and are no longer required: libntdb1 linux-headers-4.2.0-30 linux-headers-4.2.0-30-generic linux-image-4.2.0-30-generic linux-image-extra-4.2.0-30-generic python-ntdb Use 'apt-get autoremove' to remove them. 0 upgraded, 0 newly installed, 0 to remove and 10 not upgraded.

I am trying to self-update Composer because I am facing the following each time I try:

$ composer update

Loading composer repositories with package information Updating dependencies (including require-dev) [RuntimeException] Could not load package rmrevin/yii2-fontawesome in http://packagist.org: [UnexpectedValueException] Could not parse version constraint v4.1 .: Invalid version string "v4.1." [UnexpectedValueException] Could not parse version constraint v4.1.: Invalid version string "v4.1."

How can I fix this issue?

My PHP version is:

php --version

PHP 5.6.11-1ubuntu3.4 (cli) Copyright (c) 1997-2015 The PHP Group Zend Engine v2.6.0, Copyright (c) 1998-2015 Zend Technologies with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2015, by Zend Technologies

My composer version is:

composer --version

Composer version @package_branch_alias_version@ (@package_version@) @release_date@

8
I'd suggest to just uninstall it and install according to the official docs (ie not using apt or any package manager)JimL
What have you tried to debug the problem? How did you install composer in the first place?Nico Haase

8 Answers

71
votes

As Waqleh said, you have to uninstall PHP Composer and install it again. First, execute:

sudo apt-get remove composer

Then, execute these commands. The checksum here is for Composer 1.10.13, but you'll get the newest Composer (2.0.4 at the moment of editing this answer) when running the first line, so be sure to check in https://getcomposer.org/download/:

php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php -r "if (hash_file('sha384', 'composer-setup.php') === '8a6138e2a05a8c28539c9f0fb361159823655d7ad2deecb371b04a83966c61223adc522b0189079e3e9e277cd72b8897') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
php composer-setup.php
php -r "unlink('composer-setup.php');"

Now move file composer.phar to a directory that is in your path (from Installation - Linux / Unix / macOS):

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

And execute composer from any directory. That's all!

PS: If you're using PhpStorm (or maybe other IDEs), you'll have to close it and open it again.

36
votes

Since I posted my answer, I have learnt a new easier way to install Composer programmatically: How do I install Composer programmatically?

Old Answer:


As per @JimL comment, I was able to self update Composer by:

Now it works as expected.

20
votes

Install the latest version:

Remove your current Composer version, for example Ubuntu/Debian:

sudo apt-get remove composer

Now, head to https://getcomposer.org/download/ and paste the script in your command line. This ensures that you get the latest version of Composer (as time of writing: v2.0.7).

Like this:

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

After some time passed, you can update and there isn't any need to use the sudo prefix:

composer self-update
4
votes

You can specify the installation directory and filename while setting up PHP Composer - php composer-setup.php like so:

sudo php composer-setup.php --install-dir=/usr/bin --filename=composer
3
votes

If you have an old version of Composer you need to follow these commands:

composer -V
sudo apt remove composer
cd /tmp
wget http://getcomposer.org/download/1.10.5/composer.phar

php composer.phar -V
sudo mv composer.phar /usr/bin/composer
sudo chmod 750 /usr/bin/composer
composer -V
1
votes

It worked for me (linux, Ubuntu 20.04):

sudo apt-get remove composer
sudo apt-get update
sudo apt-get install curl
sudo curl -s https://getcomposer.org/installer | php
sudo mv composer.phar /usr/bin/composer
0
votes

I've installed Homebrew and it saves me a lot.

Install brew and then brew install composer to install Composer.

-1
votes

Install the latest Composer by the following steps:

Uninstalling Composer

sudo apt-get remove composer

Run following commands

php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php -r "if (hash_file('sha384', 'composer-setup.php') === '8a6138e2a05a8c28539c9f0fb361159823655d7ad2deecb371b04a83966c61223adc522b0189079e3e9e277cd72b8897') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
php composer-setup.php

Install Composer in the /usr/bin directory to run Composer from anywhere

sudo php composer-setup.php --install-dir=/usr/bin --filename=composer

Remove the installer

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

To check or self update

composer self-update