Running multiple PHP versions on Ubuntu

Syed Sirajul Islam Anik
4 min readAug 14, 2021

--

If you don’t know this guy, you don’t know. Heavy relax. See you not for mind.

Even though I run all my web applications with Docker, I still run PHP & Composer on my local machine. It helps me to install application dependencies via composer, run a few commands of the frameworks I work with from my local machine rather than being in the container. People may say that’s not good, but I do it this way.

I mainly need the PHP’s CLI on my local machine. I had been using PHP 7.4 for years. But I thought to install PHP 8.0 too. So, I looked for some articles. I found one. I will provide the link below. He does the same. I added a few bash commands to make it easy to switch between those versions. Now, let’s go through the instructions.

Installation

I had installed my OS a long time ago. Thus a lots of commands might have been applied before and I may miss them in this article. If I miss any instruction that causes issues, please put a comment, I will update.

First, you’ll need to add Ondřej Surý’s PPA. Then press Enter to confirm.

sudo apt install python-software-properties
sudo add-apt-repository ppa:ondrej/php

Run system update to resync the package index files from their sources.

sudo apt-get update

Run the following command to install the required modules/extension for PHP8.0

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

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

If you also want to download PHP7.4 alongside, 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

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

Using different PHP version

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

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.0
sudo update-alternatives --set php /usr/bin/php8.0

By default, php should remain pointed to the last switched version. But if you want to switch 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
}
load_php80 () {
sudo update-alternatives --set php /usr/bin/php8.0
}
# aliases
alias load_php8='load_php80'
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_php80
load_php8

I have created the aliases because I don’t want to write load_php80 and also whenever I have PHP8.1 installed, the load_php8 will load the latest version of PHP8.x

So, pretty much that’s all.

Happy coding. ❤

--

--

Syed Sirajul Islam Anik

software engineer with "Senior" tag | procrastinator | programmer | !polyglot | What else 🙄 — Open to Remote