How can I change the PHP version used in console on Ubuntu 20.04? I have all versions in the /etc/php folder, but I don't know where the configuration for the command line version is.
5 Answers
You can do this with update-alternatives. If you would like to do this interactively, you can do this:
sudo update-alternatives --config php
If you like to specifically choose the PHP version (via an alias or whatnot), you can do this:
sudo update-alternatives --set php /usr/bin/php7.4
Of course, be sure to change php7.4 to the actual version you want to switch between.
- 24,860
Please use following command which will ask you to select a number against your required PHP version
sudo update-alternatives --config php
Then select your choice and press enter.
- 773
-
1Ok but it shows me this message:
There is only one alternative in link group php (providing /usr/bin/php): /usr/bin/php7.2 Nothing to configure. I can see the usr/bin folder and theare really only php 7.2. Dont know what need to add there. – Čamo Nov 05 '21 at 14:18 -
@Čamo it means you don't have multiple versions installed. You can install other versions before switch to other version. – mshakeel Nov 10 '21 at 11:17
Did you install the cli? Do this first:
$ sudo apt install php7.4-cli
After that, you should see it listed under update-alternatives
$ update-alternatives --list php
/usr/bin/php7.4
/usr/bin/php8.1
- 21
-
-
Thank you this answer work great for me, of course now in 2023 I've adapt it for PHP 8.2: apt install php8.2-cli And now when I do a "php -v" I get the following answer: PHP 8.2.3 (cli) (built: Feb 14 2023 16:57:30) (NTS) Copyright (c) The PHP Group Zend Engine v4.2.3, Copyright (c) Zend Technologies with Zend OPcache v8.2.3, Copyright (c), by Zend Technologies
Thank you!
– DavidLyonsGarcia Mar 07 '23 at 11:49
alias setphp="sudo update-alternatives --config php;sudo update-alternatives --config phar; update-alternatives --config phar.phar; a2dismod php*.*;systemctl restart apache2"
Put the above alias in ~/.bashrc:
sudo nano ~/.bashrc
Then just run this command:
a2enmod php<Your Desired Version> # like, a2enmod php7.4
Now you can run the command setphp from your terminal.
There are two methods to switch php versions on the command line ubuntu.
Run each command one by one and input the number of the php version you want to change.
Method 1
sudo update-alternatives --config php
sudo update-alternatives --config phar
sudo update-alternatives --config phar.phar
sudo service apache2 restart
Method 2
If you want to change PHP 5.6 to 7.4 just run the below codes.
sudo a2dismod php5.6
sudo a2enmod php7.4
sudo service apache2 restart
If you want to change from PHP 7.4 to 5.6 run the below codes.
sudo a2dismod php7.4
sudo a2enmod php5.6
sudo service apache2 restart
For more information follow this link https://slaford.com/laravel/how-to-switch-php-versions-on-command-line-ubuntu/
- 109
There is only one alternative in link group php (providing /usr/bin/php): /usr/bin/php7.2 Nothing to configure. I can see the usr/bin folder and theare really only php 7.2. Dont know what need to add there. – Čamo Nov 05 '21 at 14:18