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.<\/p>\n
In this article, we\u2019ll explore effective solutions for managing multiple PHP versions, helping you choose the right tool to simplify your development workflow. So, without further ado, let\u2019s get started.<\/p>\n
.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”); }<\/p><\/div>\n
\n\t\t\t\t\t\tPHP7.4 has been released with a handful of new features \u2014 like the arrow function array_map(fn (Foo $foo)…\t\t\t\t\t\tRead more<\/span><\/p>\n<\/div>\n<\/div>\n
To manage multiple PHP versions with Homebrew, we\u2019ll first tap into Shivam Mathur\u2019s widely-used PHP repository<\/a>. This repository provides access to various PHP versions that you can install:<\/p>\n
\r\nbrew tap shivammathur\/php\r\n<\/pre>\nOnce the repository is tapped, you can install your desired PHP versions. Here\u2019s how to install PHP 7.4, 8.2, and the latest version (currently 8.3):<\/p>\n
\r\nbrew install shivammathur\/php\/php@7.4\r\nbrew install shivammathur\/php\/php@8.2\r\nbrew install shivammathur\/php\/php\r\n<\/pre>\nFeel free to install any combination of versions that your projects require. Each version will be stored separately on your system.<\/p>\n
Switching Between PHP Versions<\/h5>\n
While Homebrew allows you to install multiple PHP versions simultaneously, your system can only use one version at a time through its
PATH<\/code>. Think of it like having multiple PHP versions installed in your toolbox, but only one can be your active tool.<\/p>\n
Let\u2019s assume you are currently running PHP 8.3, but now you need to switch to PHP 7.4. First, unlink the current version to \u201cdisconnect\u201d the currently active PHP version from
PATH<\/code>.<\/p>\n
\r\nbrew unlink php\r\n<\/pre>\n
After unlinking the current version, you can link the other version using the
brew link<\/code> command:<\/p>\n
\r\nbrew link php@7.4\r\n<\/pre>\n
Now, when you run
php -v<\/code>, it will show the active PHP version as 7.4<\/strong>, as you can see below.<\/p>\n
<\/figure>\n
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.<\/p>\n
<\/div>\n2. Using PHP Monitor<\/h4>\n
<\/figure>\n
\nPros:<\/div>\n\n\n
- Intuitive and user-friendly interface.<\/li>\n
- Easy installation and management of PHP versions with only a few clicks.<\/li>\n
- Quick switching between versions with a single click.<\/li>\n<\/ul>\n<\/div>\n<\/div>\n
\nCons:<\/div>\n\n\n
- Only available on macOS.<\/li>\n
- Requires and depends on Homebrew to manage PHP installations.<\/li>\n
- Requires Laravel Valet<\/a> to handle the PHP version per project.<\/li>\n<\/ul>\n<\/div>\n<\/div>\n
PHP Monitor<\/a><\/strong> 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.<\/p>\n
<\/figure>\n
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.<\/p>\n
The app also provides a simple way to install and update PHP versions from the Manage PHP Installations\u2026<\/strong> menu.<\/p>\n
<\/figure>\n
<\/div>\n3. Using PHPCTL<\/h4>\n
<\/figure>\n
\nPros:<\/div>\n\n\n
- Platform-independent and portable.<\/li>\n
- Provides additional CLI tools for new projects, interactive shells, and other popular tools in PHP like PHPCS, PHPUnit, RectorPHP, etc.<\/li>\n<\/ul>\n<\/div>\n<\/div>\n
\nCons:<\/div>\n\n\n
- Requires Docker to be installed on your system.<\/li>\n
- Requires manual configuration of the
.phpctlrc<\/code> file to switch between PHP versions.<\/li>\n
Docker may consume more resources than other solutions.<\/li>\n<\/ul>\n<\/div>\n<\/div>\n
PHPCTL<\/a><\/strong> is a tool designed to help developers easily switch between different PHP versions by leveraging Docker containers<\/a>. 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<\/code> for new projects,
phpctl repl<\/code> for interactive shells, and
phpctl init<\/code> for configuration setup, among other handy features.<\/p>\n
Install PHPCTL<\/h5>\n
Before getting started, you\u2019ll need Docker installed on your system. Docker Desktop<\/a> works great, or if you\u2019re on macOS, you might prefer OrbStack<\/a>.<\/p>\n
Once you have Docker installed, you can install PHPCTL using the following command:<\/p>\n
\r\n\/bin\/bash -c \"$(curl -fsSL https:\/\/phpctl.dev\/install.sh)\"\r\n<\/pre>\nOr, if you have Homebrew installed, you can run:<\/p>\n
\r\nbrew install opencodeco\/phpctl\/phpctl\r\n<\/pre>\nThis 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.<\/p>\n
After installation, you can check if it was successfully installed by running:<\/p>\n
\r\nphpctl list\r\n<\/pre>\nThis command will list all the subcommands and other information about the current PHP installation, as you can see below.<\/p>\n
<\/figure>\n
You can also run the
php<\/code> and
composer<\/code> commands directly.<\/p>\n
\r\nphp -v\r\ncomposer -v\r\n<\/pre>\n
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.<\/p>\n
Switching Between PHP Versions<\/h5>\n
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<\/code> and specify which PHP version you\u2019d like to run within the given directory.<\/p>\n
\r\nPHP_VERSION=83\r\n<\/pre>\n
When you run
php<\/code> or
composer<\/code> in the directory, PHPCTL will automatically switch to the PHP version specified in the
.phpctlrc<\/code> file.<\/p>\n
That\u2019s all. It\u2019s 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.<\/p>\n
<\/div>\n
4. Using PVM<\/h4>\n
\nPros:<\/div>\n\n\n
- Easy installation and management of PHP versions on Windows.<\/li>\n
- Very similar to nvm, making it quick and easy to get familiar with.<\/li>\n<\/ul>\n<\/div>\n<\/div>\n
\nCons:<\/div>\n\n\n
- Only available on Windows.<\/li>\n
- Installation is a bit of a manual process.<\/li>\n<\/ul>\n<\/div>\n<\/div>\n
PVM simplifies PHP version management on Windows. Similar to Node Version Manager (nvm)<\/a> but specifically for PHP, PVM eliminates common Windows
PATH<\/code> variable headaches and streamlines switching between different PHP versions.<\/p>\n
Install PVM<\/h5>\n
Download the latest PVM release from the official Github repository<\/a>. Then, create a folder at
C:UsersYourUsername.pvmbin<\/code> and place the downloaded
pvm.exe<\/code> in this folder.<\/p>\n
Installing PHP with PVM<\/h5>\n
PVM makes it easier to install multiple PHP versions on Windows. If you need a version that\u2019s not currently installed on your computer, you can use the install command:<\/p>\n
\r\npvm install 8.2\r\n<\/pre>\n\u2026which will download and install PHP 8.2 on your computer.<\/p>\n
Switching PHP Versions with PVM<\/h5>\n
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\u2019s not provided.<\/p>\n
pvm use 8.2<\/pre>\nIf you want to switch to a specific patch version, include the patch number as well:<\/p>\n
pvm use 8.2.3<\/pre>\nThat\u2019s all. PVM is a great tool for managing PHP versions on Windows, but it also comes with its own set of pros and cons.<\/p>\n
<\/div>\n5. Using Valet<\/h4>\n
<\/figure>\n
\nPros:<\/div>\n\n\n
- Easy installation and management of PHP versions on macOS.<\/li>\n
- Quick switching between PHP versions for different projects.<\/li>\n
- Works seamlessly with Laravel projects and supports other types of projects like WordPress, Symfony, etc.<\/li>\n<\/ul>\n<\/div>\n<\/div>\n
\nCons:<\/div>\n\n\n
- Only available on macOS.<\/li>\n
- Requires and depends on Homebrew to manage PHP installations.<\/li>\n<\/ul>\n<\/div>\n<\/div>\n
Install Valet<\/h5>\n
To get started, install Valet using Composer as a global package:<\/p>\n
\r\ncomposer global require laravel\/valet\r\n<\/pre>\nAfter installation, run the Valet installation command:<\/p>\n
\r\nvalet install\r\n<\/pre>\nSwitching PHP Versions with Valet<\/h5>\n
Valet makes PHP version switching simple with the
valet use php@version<\/code> command. For example:<\/p>\n
\r\nvalet use php@8.2\r\n<\/pre>\n
It automatically installs the version via Homebrew if it\u2019s currently missing.<\/p>\n
For project-specific PHP versions, you can create a
.valetrc<\/code> file in your project\u2019s root directory with the line
php=php@8.2<\/code>. Then, simply run:<\/p>\n
\r\nvalet use\r\n<\/pre>\n
\u2026and Valet will automatically switch to the PHP version specified in the
.valetrc<\/code> file.<\/p>\n
<\/div>\n
Wrapping Up<\/h4>\n
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.<\/p>\n
The post 5 Ways to Manage Multiple Versions of PHP<\/a> appeared first on Hongkiat<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"
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\u2019ll explore effective solutions for managing multiple […]<\/p>\n","protected":false},"author":1,"featured_media":718,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[],"class_list":["post-716","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-social-commerce"],"_links":{"self":[{"href":"https:\/\/upprofits.net\/index.php\/wp-json\/wp\/v2\/posts\/716"}],"collection":[{"href":"https:\/\/upprofits.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/upprofits.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/upprofits.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/upprofits.net\/index.php\/wp-json\/wp\/v2\/comments?post=716"}],"version-history":[{"count":3,"href":"https:\/\/upprofits.net\/index.php\/wp-json\/wp\/v2\/posts\/716\/revisions"}],"predecessor-version":[{"id":728,"href":"https:\/\/upprofits.net\/index.php\/wp-json\/wp\/v2\/posts\/716\/revisions\/728"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/upprofits.net\/index.php\/wp-json\/wp\/v2\/media\/718"}],"wp:attachment":[{"href":"https:\/\/upprofits.net\/index.php\/wp-json\/wp\/v2\/media?parent=716"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/upprofits.net\/index.php\/wp-json\/wp\/v2\/categories?post=716"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/upprofits.net\/index.php\/wp-json\/wp\/v2\/tags?post=716"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}