linux下两个php版本怎么切换

我用的ubuntu

执行 php -v

PHP 7.2.24-0ubuntu0.18.04.8 (cli) (built: Jul  5 2021 17:41:32) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
    with Zend OPcache v7.2.24-0ubuntu0.18.04.8, Copyright (c) 1999-2018, by Zend Technologies

 whereis php
 php: /usr/bin/php /usr/bin/php7.2 /usr/lib/php /etc/php /usr/include/php /usr/local/php /usr/share/php7.2-xml /usr/share/php /usr/share/php7.2-common /usr/share/php7.2-json /usr/share/
php7.2-opcache /usr/share/php7.2-readline /usr/share/man/man1/php.1.gz

查看,果然有两个 php 版本,/usr/bin/php , /usr/local/php,为保证只运行 php7.4, 断然决定卸载 php7.2。

apt-get autoremove php7.2-common

DESKTOP-BHVUG0D# sudo apt-get autoremove php7.2-common
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages will be REMOVED:
  debhelper dh-autoreconf dh-strip-nondeterminism libarchive-cpio-perl libarchive-zip-perl libfile-stripnondeterminism-perl libsodium23 libtimedate-perl
  php-common php-dev php-pear php-xml php7.2-cli php7.2-common php7.2-dev php7.2-json php7.2-opcache php7.2-readline php7.2-xml pkg-php-tools shtool
0 upgraded, 0 newly installed, 21 to remove and 155 not upgraded.
After this operation, 23.7 MB disk space will be freed.

卸载完成查看

 whereis php
 php: /usr/lib/php /etc/php /usr/include/php /usr/local/php /usr/share/php
php -v
zsh: command not found: php


到此,基本把 php5 给卸载掉了,然后尝试执行 composer

/usr/bin/env: ‘php’: No such file or directory
由于把之前默认的版本删除掉,找不到 PHP 版本,加软连接

ln -s /usr/local/php/bin/php /usr/local/bin/php
 php -v

PHP 7.4.12 (cli) (built: May 20 2021 23:38:33) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies

到此就设置成功了