5 Ways to Manage Multiple Versions of PHP
Original Source: https://www.hongkiat.com/blog/manage-multiple-php-versions/
Managing multiple PHP versions is a common challenge when developing PHP applications, where applications often require different versions due to varying framework dependencies and compatibility requirements. While switching between PHP versions can be daunting, especially at the system level, several tools can streamline this process.
In this article, we’ll explore effective solutions for managing multiple PHP versions, helping you choose the right tool to simplify your development workflow. So, without further ado, let’s get started.
How to Upgrade PHP to Latest Version
.no-js #ref-block-post-47222 .ref-block__thumbnail { background-image: url(“https://assets.hongkiat.com/uploads/thumbs/250×160/how-to-upgrade-php.jpg”); }
How to Upgrade PHP to Latest Version
PHP7.4 has been released with a handful of new features — like the arrow function array_map(fn (Foo $foo)… Read more
1. Using Homebrew
Pros:
Easy installation and management of multiple PHP versions.
Quick switching between versions with simple commands.
Regularly updated and well-maintained.
Works seamlessly on macOS and Linux.
Cons:
Only available on macOS and Linux.
Updating can be slow and rather confusing if you’re not familiar with working with the CLI.
Homebrew, the popular package manager for macOS and Linux, simplifies PHP version management. After installing Homebrew from their official website, follow these steps to set up and switch between PHP versions:
Installing Different PHP Versions
To manage multiple PHP versions with Homebrew, we’ll first tap into Shivam Mathur’s widely-used PHP repository. This repository provides access to various PHP versions that you can install:
brew tap shivammathur/php
Once the repository is tapped, you can install your desired PHP versions. Here’s how to install PHP 7.4, 8.2, and the latest version (currently 8.3):
brew install shivammathur/php/php@7.4
brew install shivammathur/php/php@8.2
brew install shivammathur/php/php
Feel free to install any combination of versions that your projects require. Each version will be stored separately on your system.
Switching Between PHP Versions
While Homebrew allows you to install multiple PHP versions simultaneously, your system can only use one version at a time through its PATH. Think of it like having multiple PHP versions installed in your toolbox, but only one can be your active tool.
Let’s assume you are currently running PHP 8.3, but now you need to switch to PHP 7.4. First, unlink the current version to “disconnect” the currently active PHP version from PATH.
brew unlink php
After unlinking the current version, you can link the other version using the brew link command:
brew link php@7.4
Now, when you run php -v, it will show the active PHP version as 7.4, as you can see below.
Homebrew makes it easy to use multiple PHP versions on macOS and Linux through the CLI. But it also comes with its own set of pros and cons. So consider the following when deciding if Homebrew is the right choice for you.
2. Using PHP Monitor
Pros:
Intuitive and user-friendly interface.
Easy installation and management of PHP versions with only a few clicks.
Quick switching between versions with a single click.
Cons:
Only available on macOS.
Requires and depends on Homebrew to manage PHP installations.
Requires Laravel Valet to handle the PHP version per project.
PHP Monitor is a lightweight macOS application designed to help developers manage and switch between different PHP versions easily. It offers a familiar and intuitive UI that appears at the top of your screen, allowing you to switch between PHP versions with a single click. This app integrates with Homebrew, making it easier to manage your PHP setup without using the terminal.
As we can see above, you can view which PHP versions are installed on your machine, the current version active globally, access the PHP configuration file, view the memory limit, and more.
The app also provides a simple way to install and update PHP versions from the Manage PHP Installations… menu.
3. Using PHPCTL
Pros:
Platform-independent and portable.
Provides additional CLI tools for new projects, interactive shells, and other popular tools in PHP like PHPCS, PHPUnit, RectorPHP, etc.
Cons:
Requires Docker to be installed on your system.
Requires manual configuration of the .phpctlrc file to switch between PHP versions.
Docker may consume more resources than other solutions.
PHPCTL is a tool designed to help developers easily switch between different PHP versions by leveraging Docker containers. This makes PHPCTL portable and platform-independent, allowing you to manage PHP versions on any operating system that supports Docker. It also provides additional CLI tools, such as phpctl create for new projects, phpctl repl for interactive shells, and phpctl init for configuration setup, among other handy features.
Install PHPCTL
Before getting started, you’ll need Docker installed on your system. Docker Desktop works great, or if you’re on macOS, you might prefer OrbStack.
Once you have Docker installed, you can install PHPCTL using the following command:
/bin/bash -c “$(curl -fsSL https://phpctl.dev/install.sh)”
Or, if you have Homebrew installed, you can run:
brew install opencodeco/phpctl/phpctl
This will download the PHPCTL binary to your system and make it executable, allowing you to use the tool right away. The script automatically installs PHPCTL and sets up the necessary paths, so no manual configuration is required.
After installation, you can check if it was successfully installed by running:
phpctl list
This command will list all the subcommands and other information about the current PHP installation, as you can see below.
You can also run the php and composer commands directly.
php -v
composer -v
These two commands will actually run inside a Docker container. PHPCTL will automatically mount the current directory to the container, so you can work on your project as if you were working on your local machine.
Switching Between PHP Versions
Unlike with Homebrew or PHP Monitor, where you need to run a command or click on the UI to switch to the PHP version, with PHPCTL, you will need to create a file .phpctlrc and specify which PHP version you’d like to run within the given directory.
PHP_VERSION=83
When you run php or composer in the directory, PHPCTL will automatically switch to the PHP version specified in the .phpctlrc file.
That’s all. It’s very convenient and provides a seamless development experience once it is fully configured. However, it also comes with its own set of pros and cons.
4. Using PVM
Pros:
Easy installation and management of PHP versions on Windows.
Very similar to nvm, making it quick and easy to get familiar with.
Cons:
Only available on Windows.
Installation is a bit of a manual process.
PVM simplifies PHP version management on Windows. Similar to Node Version Manager (nvm) but specifically for PHP, PVM eliminates common Windows PATH variable headaches and streamlines switching between different PHP versions.
Install PVM
Download the latest PVM release from the official Github repository. Then, create a folder at C:UsersYourUsername.pvmbin and place the downloaded pvm.exe in this folder.
Lastly, add the .pvmbin folder to your system’s PATH variable through System Properties > Environment Variables.
Once installed, you can use PVM to switch between PHP versions quickly and easily. Since it is heavily inspired by nvm, the commands are similar. Here are some commands to get you started:
Installing PHP with PVM
PVM makes it easier to install multiple PHP versions on Windows. If you need a version that’s not currently installed on your computer, you can use the install command:
pvm install 8.2
…which will download and install PHP 8.2 on your computer.
Switching PHP Versions with PVM
If you want to switch to a specific PHP version, use the use command. You must specify at least the major and minor version, and PVM will choose the latest available patch version if it’s not provided.
pvm use 8.2
If you want to switch to a specific patch version, include the patch number as well:
pvm use 8.2.3
That’s all. PVM is a great tool for managing PHP versions on Windows, but it also comes with its own set of pros and cons.
5. Using Valet
Pros:
Easy installation and management of PHP versions on macOS.
Quick switching between PHP versions for different projects.
Works seamlessly with Laravel projects and supports other types of projects like WordPress, Symfony, etc.
Cons:
Only available on macOS.
Requires and depends on Homebrew to manage PHP installations.
Laravel Valet is a lightweight development environment designed specifically for macOS that makes PHP development a breeze. What makes Valet particularly convenient is its built-in PHP version management that allows you to switch between PHP versions for different projects without complex configurations.
Install Valet
To get started, install Valet using Composer as a global package:
composer global require laravel/valet
After installation, run the Valet installation command:
valet install
Switching PHP Versions with Valet
Valet makes PHP version switching simple with the valet use php@version command. For example:
valet use php@8.2
It automatically installs the version via Homebrew if it’s currently missing.
For project-specific PHP versions, you can create a .valetrc file in your project’s root directory with the line php=php@8.2. Then, simply run:
valet use
…and Valet will automatically switch to the PHP version specified in the .valetrc file.
Wrapping Up
With the right tools, managing multiple PHP versions becomes effortless across macOS, Linux, or Windows. Hopefully, this article helps you pick the solution that matches your workflow.
The post 5 Ways to Manage Multiple Versions of PHP appeared first on Hongkiat.
Leave a Reply
Want to join the discussion?Feel free to contribute!