94

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@

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Waqleh
  • 9,741
  • 8
  • 65
  • 103
  • 10
    I'd suggest to just uninstall it and install [according to the official docs](https://getcomposer.org/doc/00-intro.md#installation-linux-unix-osx) (ie not using apt or any package manager) – JimL Jun 12 '16 at 09:49
  • What have you tried to debug the problem? How did you install composer in the first place? – Nico Haase Apr 06 '20 at 10:20
  • sudo -H composer self-update This command perfectly works in Ubuntu to upgrade composer latest version. – Rakesh Jan 21 '22 at 16:49

11 Answers11

113

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Manolo
  • 24,020
  • 20
  • 85
  • 130
  • 6
    After that last unlink step, i did: sudo php composer-setup.php --install-dir=/usr/bin --filename=composer – jasonflaherty Nov 22 '19 at 02:03
  • 6
    The checksum is incorrect for the latest version of composer. – pmagunia Dec 21 '19 at 22:27
  • The second command deletes composer-setup.php and the third one fails as a result – crafter Feb 18 '21 at 10:41
  • 3
    You really need to follow the commands from the documentation because each new release will have a different hash (line two of the commands) and the commands from this answer will fail – wranvaud Feb 18 '21 at 12:49
  • To editors and reviewers: *"[PHP Composer](https://en.wikipedia.org/wiki/Composer_(software))"* (capital "C") is a proper noun, not a common noun (in this context). – Peter Mortensen Apr 21 '21 at 15:17
  • most recent hash as of 09/09/2021 `756890a4488ce9024fc62c56153228907f1545c228516cbf63f885e036d37e9a59d27d63f46af1d4d07ee0f76181c7d3` so replace the long string in line 2 with this hash and unless there has been another release since my comment it will verify installer – Liam O'Toole Sep 09 '21 at 17:21
  • After the unlink step, in some cases, you may have to run `source ~/.bashrc` to make it work in your same terminal. – Jean-Roch B. Jan 04 '22 at 17:23
  • `alias composer='/usr/local/bin/composer'` This command was also needed at the end to have the composer work from any directory. – Jay Teli Jan 05 '22 at 12:49
  • 3
    **Quick tip**: Copy the commands from the [documentation](https://getcomposer.org/download/), instead of the answer's. – testing_22 Jan 06 '22 at 16:23
  • just ignore the hash. if you are beeing man-in-the-middled, you're having other problems anyway. so just don't use this line – clockw0rk May 12 '23 at 12:11
  • It might be needed to reboot computer after all steps. And yes use commands from docs as @wranvaud stated. https://getcomposer.org/download/ – some_guy May 21 '23 at 22:08
  • I thought sudo mv composer.phar /usr/local/bin/composer should move the file into the directory composer, so I created that directory in advance. But composer is the renamed phar file, not a directory. – ESP32 Jun 14 '23 at 21:50
42

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:

  • Uninstalling Composer from the package manager (apt).
  • I installed it according to the official documentation

Now it works as expected.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Waqleh
  • 9,741
  • 8
  • 65
  • 103
  • 7
    This worked for me, although the "official documentation" is a bit confusing. – Jerome Wiley Segovia Oct 10 '17 at 16:28
  • 1
    If you have Ubuntu 18.04 you may find some problems with the official installation method. Checkout [this issue](https://github.com/composer/composer/issues/7730) – SaidbakR Oct 20 '18 at 10:46
  • The problem with the official documentation is that the process doesn't allow for Composer to be updated automatically with regular OS updates. – colan Feb 18 '19 at 22:56
  • `alias composer='/usr/local/bin/composer'` This command was also needed at the end to have the composer work from any directory. – Jay Teli Jan 05 '22 at 12:49
37

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/local/bin/composer
Ryan
  • 22,332
  • 31
  • 176
  • 357
pgrono
  • 740
  • 7
  • 5
  • https://getcomposer.org/download/ says to move it instead to `/usr/local/bin/composer`. I will update your answer. – Ryan Aug 25 '21 at 18:48
  • this worked for me: curl -sS https://getcomposer.org/installer | php sudo mv composer.phar /usr/bin/composer – Imran Hossain Aug 29 '21 at 08:58
  • 4
    In my case you need to move it to ``sudo mv composer.phar /usr/bin/composer`` Instead – Fahad Dec 30 '21 at 15:29
  • 2
    Thanks! it's worked for me except the last command. I use this command instead `sudo mv composer.phar /usr/bin/composer` – arhakim Jul 28 '22 at 02:18
35

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.5.8).

Like this:

php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php -r "if (hash_file('sha384', 'composer-setup.php') === '55ce33d7678c5a611085589f1f3ddf8b3c52d662cd01d4ba75c0ee0459970c2200a51f492d557530c71c15d8dba01eae') { 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

Note: The up-to-date hash can be found at getcomposer.org/download

A1Gard
  • 4,070
  • 4
  • 31
  • 55
8

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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
archana bahadur
  • 111
  • 1
  • 3
4

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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Sumit Wadhwa
  • 2,825
  • 1
  • 20
  • 34
2

Already there are many answers, Would like to share my experience with this.

I am using Ubuntu 20.04, and My previous Composer version was Composer 2.0.11 and my Project requirement was Composer 2.1.15.

I didn't had to remove anything, I simply used the following command and it worked fine for me :)

Composer Update

Imran Rafiq Rather
  • 7,677
  • 1
  • 16
  • 35
1

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

Install brew and then brew install composer to install Composer.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
M. Inam
  • 301
  • 3
  • 7
0

composer 2 on debian :

cd /usr/src

sudo apt-get install curl php7.2-cli

curl -sS https://getcomposer.org/installer | sudo php -- --install-dir=/usr/local/bin --filename=composer

chmod 777 composer

nano ~/.bashrc

add : PATH=$PATH/usr/local/bin/

source ~/.bashrc

verify :composer -v

0

Composer self-update may not work for others and using the documentation from the composer may be confusing for some. The following steps may help reinstall the composer to solve the problem.

#update the package manager

sudo apt update

#install the required packages

sudo apt install php-cli unzip

#retrieve the installer

curl -sS https://getcomposer.org/installer -o /tmp/composer-setup.php

#obtain the latest hash from the Composer page

HASH=`curl -sS https://composer.github.io/installer.sig`
echo $HASH #verify the obtained value

#verify that the installation script safety

php -r "if (hash_file('SHA384', '/tmp/composer-setup.php') === '$HASH') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"

#installing composer globally

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

#test your installation

composer

That's it. Credit: Erika Heidi

-2

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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
lokender singh
  • 829
  • 9
  • 18
  • 4
    Please do not duplicate existing answers - the given steps were posted exactly like that more than a year ago, and your answer does not add any new information – Nico Haase Nov 25 '20 at 08:27
  • that was not duplicate they have a different method to perform it but they were doing this incorrect way – lokender singh Nov 26 '20 at 04:13
  • Please share more details about this - as far as I see, A1Gard uses the same commands. If there's something wrong about the other answers, you should explain why yours is better – Nico Haase Nov 26 '20 at 07:18
  • they were not installing it in /usr/bin before deleting composer-setup that's why it will not make it global – lokender singh Nov 27 '20 at 11:21
  • Why should such a file be present in `/usr/bin`? According to https://unix.stackexchange.com/questions/8656/usr-bin-vs-usr-local-bin-on-linux, `/usr/bin` should not be used for binaries that you install on any other way than through the package manager – Nico Haase Nov 27 '20 at 11:29
  • because it is the composer and we used this in every project and that power is given to us by the composer team itself sudo php composer-setup.php --install-dir=/usr/bin --filename=composer, you can check there official documentation – lokender singh Nov 27 '20 at 11:42
  • And is there anything wrong or incorrect in using `/usr/local/bin`, as suggested in other answers? Please add all such explanation to your answer such that others can see what you are doing differently, and why your different approach is more "correct" than any other – Nico Haase Nov 27 '20 at 12:51
  • in other answers, finally, you have to use `php composer.pha` instead of use `composer`. Personally, I add an alias but this solution seems ok (but a warning to use instructions from https://getcomposer.org/dowload/ to have well the last hash is important too ! – bcag2 Apr 12 '21 at 10:05