Score:0

How to change PHP version in Ubuntu 20.04 console?

in flag

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.

Score:1
ws flag

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
waltmagic avatar
mq flag
Couldn't find any package by glob 'php7.4-cli'
DavidLyonsGarcia avatar
ar flag
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!
Score:1
in flag

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.

in flag
Ok 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.
Score:0
de flag

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.

in flag
Ok 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.
mshakeel avatar
de flag
@Čamo it means you don't have multiple versions installed. You can install other versions before switch to other version.
Score: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 the

sudo nano ~/.bashrc

#After the above command just run this command

a2enmod php<Your Desired Version> # like, a2enmod php7.4

Now you can run command "setphp" from your terminal.

Score:0
jo flag

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 switch screenshoot switch php version in ubtuntu

Score:0
td flag

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/

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.