22

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.

sotirov
  • 4,379
Čamo
  • 849

5 Answers5

43

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.

matigo
  • 24,752
  • 7
  • 50
  • 79
13

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.

mshakeel
  • 773
2

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

-1

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/