php can be fun :)

Run multiple PHP versions in ubuntu nginx, z shell (Zsh) and valet.

Kenmutesh
3 min readJun 11, 2023

--

I’ve been working with Laravel for some time now. I had a challenge with the multiple versions each company was using basically most prefer laravel 8 (PHP 7.4) while the new ones are opting for laravel 10 (PHP 8.1). I operate on valet and z shell for ease of the process. I was forced to at times reset my php whenever I was working on each project.

To have all the php running and easy change between them, Here is a short process.

  1. Install the valet I already installed on my machine. You can verify by running
valet -v

Valet v2.3.2

Usage:
command [options] [arguments]

2. The next step we can start setting up PHP

$ sudo apt-get update
$ sudo apt -y install software-properties-common
$ sudo add-apt-repository ppa:ondrej/php
$ sudo apt-get update

with that set, we can now install our versions.

We will first install php8.1 and some extensions

    sudo apt-get install php8.1 php8.1-cli php8.1-common \
php8.1-zip php8.1-gd php8.1-mbstring php8.1-tokenizer \
php8.1-curl php8.1-xml php8.1-bcmath php8.1-xml \
php8.1-intl php8.1-sqlite3 php8.1-fpm

After they are installed successfully, you can run php -v on your terminal and get the output that PHP8.1 is installed successfully.

If you also want to download PHP7.4 you can run the following command.

    sudo apt-get install php7.4 php7.4-cli php7.4-common \
php7.4-zip php7.4-gd php7.4-mbstring \
php7.4-curl php7.4-xml php7.4-bcmath php7.4-xml \
php7.4-intl php7.4-sqlite3 php7.4-fpm

Now, all your PHP installations are under the /etc/php directory.

You can run the command to check the installations. ls -al /etc/php

Using different PHP version

Both the PHP8.1 and PHP7.4 executables are available from your terminal. You can use them with php7.4and php8.1.

But if you want to use php executable and want to point to any of the available concrete versions, then you’ll need to run the following version to switch between them.

# Switch to PHP 7.4
sudo update-alternatives --set php /usr/bin/php7.4

# Switch to PHP 8.1
sudo update-alternatives --set php /usr/bin/php8.1

By default, php should remain pointed to the last switched version. But if you want to switch from the current version to another version, you can use the above command.

Keeping the whole command in mind and typing can be tedious. Rather, I made two bash functions that do it for me. I just run the function name from the terminal. You can do the following.

load_php74 () {
sudo update-alternatives --set php /usr/bin/php7.4
valet use 7.4
valet restart
}
load_php81 () {
sudo update-alternatives --set php /usr/bin/php8.1
valet use 8.1
valet restart
}
# aliases
alias load_php8='load_php81'
alias load_php7='load_php74'

If you’re using bash, then add the above snippet in your ~/.bashrc or for zsh, you add them in your ~/.zshrc file. Source it with source ~/.bashrc or source ~/.zshrc and now you can switch between PHP versions using any of the following

load_php74
load_php7
load_php81
load_php8

I have created the aliases because they load_php8 will load the latest version of PHP8.x installed.

--

--