How to change PHP version in Ubuntu 20.04 console?

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.

4 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.

1

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.

2

i just try in Ubuntu20.04 and it works.

// to show the list php installed: sudo update-alternatives --config php

// choose number version that you wanna switchscreenshoot switch php version in ubtuntu

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.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like