PHP, being a versatile scripting language, often requires developers to install different PHP versions to run different web applications for development purposes or for running legacy applications that require older PHP versions.
This article will guide you through the process of installing multiple PHP versions on Ubuntu using the phpbrew tool.
Install phpbrew in Ubuntu
To install multiple PHP versions on a Linux system, you need to install the “phpbrew” tool that simplifies the process of installing, managing, and switching between different PHP versions on your Ubuntu system.
To install “phpbrew“, you need to install the following required libraries on your system using apt commands.
sudo apt update sudo apt install -y php-cli php-xml curl build-essential libxml2-dev php-xml pkg-config libssl-dev libbz2-dev libcurl4-gnutls-dev libonig-dev
Once the required libraries are installed, you can use the following curl command to install phpbrew as shown.
curl -L -O https://github.com/phpbrew/phpbrew/raw/master/phpbrew chmod +x phpbrew sudo mv phpbrew /usr/local/bin/phpbrew
Once installed, init a bash script for your shell environment as shown.
phpbrew init
Next, add the following line(s) to the end of your ~/.bashrc
or .zshrc
file and start a new shell, phpbrew should be up and fully functional from there.
[[ -e ~/.phpbrew/bashrc ]] && source ~/.phpbrew/bashrc
Install Multiple PHP Versions in Ubuntu
To install different PHP versions, first, you need to list all of the PHP versions that are available for installation with phpbrew.
phpbrew known
To install a specific PHP version (e.g. 7.4 and 8.3), you can use the phpbrew install command.
phpbrew install 7.4 +default phpbrew install 8.3 +default
On Ubuntu 22.04, I encountered the following error, which is because the PHP 7.4 or PHP 8.3 version requires the use of libssl1.1 during the compilation of the openssl extension. However, Ubuntu 22.04 comes with OpenSSL 3.
/usr/include/openssl/rsa.h:289:29: note: expected ‘RSA *’ {aka ‘struct rsa_st *’} but argument is of type ‘const struct rsa_st *’ make: *** [Makefile:638: ext/openssl/openssl.lo] Error 1
To fix this error, you need to compile OpenSSL 1.1 from the source on Ubuntu 22.04 and set the PKG_CONFIG_PATH environment variable to indicate the path for OpenSSL 1.1.
Compile OpenSSL 1.1 from the Source
cd $HOME wget https://www.openssl.org/source/openssl-1.1.1i.tar.gz tar xzf $HOME/openssl-1.1.1i.tar.gz cd openssl-1.1.1i ./Configure --prefix=$HOME/openssl-1.1.1i/bin -fPIC -shared linux-x86_64 make -j 8 make install
Now try to install PHP 7.4 and PHP 8.3 with phpbrew as shown.
export PKG_CONFIG_PATH=$HOME/openssl-1.1.1i/bin/lib/pkgconfig && phpbrew --debug install 7.4 +default export PKG_CONFIG_PATH=$HOME/openssl-1.1.1i/bin/lib/pkgconfig && phpbrew --debug install 8.3 +default
Switch Between PHP Versions
Once php versions are installed, you can use phpbrew list
command to list the installed version.
phpbrew list
Now you can use the following phpbrew switch
command to switch between different PHP versions that have been installed with phpbrew.
phpbrew use php-7.4.33 OR phpbrew use php-8.3.0
You can verify the installed PHP version by running.
php -v
Set Specific PHP Version as Default
To set a specific PHP version as the default, use:
phpbrew switch php-7.4.33 phpbrew use php-7.4.33 phpbrew app get
Uninstall Specific PHP Version
To uninstall a specific PHP version, use the following command which will remove PHP from your system, along with any associated extensions or modules.
phpbrew uninstall php-7.4.33 OR phpbrew uninstall php-8.3.0
In conclusion, the ability to switch between PHP versions is a crucial skill for developers and system administrators that allows efficient testing, debugging, and ensuring compatibility across diverse projects.