Skip to content

How to Easily Switch Between PHP Versions on Ubuntu

Managing multiple PHP versions on Ubuntu does not have to be painful. Learn how to install, set a default, and switch versions from the command line.

3 min read
A terminal window showing PHP 8.1 changing to PHP 8.3
On this page7 sections

If you’re a web developer, you’ve probably faced the challenge of managing multiple PHP versions on a single system. Whether you’re working on legacy projects or testing the latest PHP features, being able to switch seamlessly between PHP versions is crucial. In this guide, we’ll walk you through updating your packages, installing multiple PHP versions, setting a default PHP version, switching between them, and installing essential PHP extensions.

Step 1: Update Your Packages

Before diving into PHP-specific tasks, ensuring your system is up-to-date is essential. Fire up your terminal and run these commands:

Terminal window
sudo apt update
sudo apt upgrade

These commands will refresh the list of available packages and their versions, and install the newest versions of all packages currently installed on your system.

Step 2: Install Multiple PHP Versions

To juggle different PHP versions, we need a repository that provides them. Here’s how to get started:

Install the software-properties-common package:

Terminal window
sudo apt install -y software-properties-common

Add the Ondřej Surý PPA (Personal Package Archive) for PHP:

Terminal window
sudo add-apt-repository ppa:ondrej/php

Update the package list again:

Terminal window
sudo apt update

Install the PHP versions you need:

Terminal window
sudo apt install -y php5.6-cli php7.0-cli php7.1-cli php7.3-cli php7.4-cli php8.0-cli php8.1-cli

Step 3: Set the Default PHP Version

With multiple PHP versions installed, you’ll need to set one as the default. For example, if you want PHP 7.4 to be your default version, use:

Terminal window
sudo update-alternatives --set php /usr/bin/php7.4

Step 4: Switch Between PHP Versions

Switching between PHP versions is straightforward. To switch to PHP 8.1, for example, just run:

Terminal window
sudo update-alternatives --set php /usr/bin/php8.1

Step 5: List Available PHP Versions

Want to see which PHP versions are available on your system? Use:

Terminal window
sudo update-alternatives --list php

This command will list all installed PHP versions along with their paths, making it easy for you to switch between them.

Extra: Installing PHP Extensions

PHP extensions are often required to add functionality to your applications. Installing them is easy. For instance, to install several extensions for PHP 7.4, run:

Terminal window
sudo apt-get install php7.4-gd php7.4-mcrypt php7.4-curl php7.4-intl php7.4-xsl php7.4-mbstring openssl php7.4-zip php7.4-soap

This command installs a variety of useful extensions like GD for image processing, cURL for data transfer, and mbstring for multibyte string handling.

Conclusion

Managing multiple PHP versions on your Ubuntu system doesn’t have to be a hassle. By following these steps, you can easily update your packages, install various PHP versions, set a default, switch between versions with ease, and even install essential PHP extensions. With this flexibility, you can work on different projects without worrying about compatibility issues, making your development workflow smoother and more efficient. Happy coding!

Filed underPHPLinuxWeb Developmentmultiple php versions in linux
Share

Rajan Dangi

Senior Web Developer

Senior web developer at SathiSoft. I write about PHP, Python, Linux and the practical details of building for the web.

More from the blog

All articles