Как изменить версию php mac os

I recently needed to switch between PHP versions on my macOS environment to work on a legacy project.

I recently needed to switch between PHP versions on my macOS environment to work on a legacy project.

As usual, I’d forgotten how to do this, so I’ve decided to publish the steps for my own and other’s reference.

The instructions below are for use with macOS 10.15 Catalina, and allow installation of PHP 5.6, 7.0, 7.1, 7.2, 7.3 & 7.4.

Using several versions of PHP on macOS via homebrew

1. Prerequisites

You’ll need both Xcode Command Line Tools and Homebrew installed.

1.1 Install XCode Command Line Tools

xcode-select --install

1.2 Install Homebrew

Homebrew is a package manager for macOS. It’s like apt on Ubuntu.

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Check that brew has installed:

$ brew --version
Homebrew 2.2.5

You can also run brew doctor to check everything is good to go.

2. Install Multiple PHP Versions

As of writing, only PHP 7.2, 7.3 and 7.4 are maintained and supported by Homebrew.

To install PHP 5.6, 7.0 & 7.1 we’ll need to ‘tap’ a repository for deprecated packages:

brew tap exolnet/homebrew-deprecated

Now, we can install all the available PHP versions:

brew install [email protected]
brew install [email protected]
brew install [email protected]
brew install [email protected]
brew install [email protected]
brew install [email protected]

This may take a little time to install. Go make yourself a brew ☕️.

Once installed, you can switch between PHP versions by ‘linking’ and ‘unlinking’ in brew:

# Switch from 7.4 to 5.6
brew unlink [email protected]
brew link [email protected] --force

Switching from PHP 7.4 to 5.6

You can combine brew unlink and brew link to swap between any installed version.

Open Source Alternatives

There are a few open source projects that aim to automate this for you, if you prefer:

  • phpbrew/phpbrew
  • philcook/brew-php-switcher

I’m a PHP Developer based in UK, building modern PHP web applications and writing about everything I learn.

# Upgrading with Homebrew

Start by making sure brew is up-to-date:

brew update

Next, upgrade PHP. You can either use the built-in php recipe, use tap shivammathur/homebrew-php. I’d recommend the second approach, since it allows you to easily install several PHP versions and switch between them.

# Normal upgrade

brew upgrade php

# Upgrade with shivammathur/homebrew-php

brew tap shivammathur/php
brew install shivammathur/php/php@8.0

To switch between versions, use the following command:

brew link --overwrite --force php@8.0

You can read more in the repository.

# Next steps

Check the current version by running php -v:

php -v

Restart Nginx or Apache:

sudo nginx -s reload
sudo apachectl restart

And make sure that your local web server also uses PHP 8 by visiting this script:

# index.php, accessible to your web server

phpinfo();

The version should show 8.0.x.

Note: if you’re using Laravel Valet, please keep on reading,
you need some extra steps in order for the web server to properly work.

This is where the ad would go. Instead though, I’d like to point you towards my
GitHub Sponsors page. If you’re a regular reader and my content is helping you, you can consider a one-time or monthly sponsorship.
If you’re a company looking for dedicated ad placements on this blog or my newsletter, you can email me at
brendt@stitcher.io

# Valet

If you’re using Laravel Valet, you should do the following steps to upgrade it:

composer global update

You can use valet use to switch between PHP versions:

valet use php@8.0
valet use php@7.4

Note that if you’re using an older Valet version (prior to v2.13.18), when switching from PHP 8 to PHP 7.4 there was a bug that didn’t properly update the changes. This was fixed in Valet 2.13.18 so that it now automatically removes the valet socket after having run valet use php@7.4. If you need to do this manually, you can run:

cd ~/.config/valet
rm valet.sock
valet restart

# Extensions

PHP extensions are installed using pecl. I personally use Imagick, Redis and Xdebug. They can be installed like so:

pecl install imagick
pecl install redis
pecl install xdebug

You can run pecl list to see which extensions are installed:

pecl list

# Installed packages, channel pecl.php.net:
# =========================================
# Package Version State
# imagick 3.4.4   stable
# redis   5.1.1   stable
# xdebug  2.8.0   stable

You can search for other extensions using pecl search:

pecl search pdf

# Retrieving data...0%
# ..
# Matched packages, channel pecl.php.net:
# =======================================
# Package Stable/(Latest) Local
# pdflib  4.1.2 (stable)        Creating PDF on the fly with the PDFlib library

Make sure to restart your web server after installing new packages:

sudo nginx -s reload
sudo apachectl restart

If you’re using Laravel Valet, you should restart it as well.

valet restart

Make sure all extensions are correctly installed and loaded by checking both your PHP webserver and CLI installs:

php -i | grep redis
var_dump(extension_loaded('redis'));

If extensions aren’t properly loaded, there are two easy fixes.

First, make sure the extensions are added in the correct ini file. You can run php --ini to know which file is loaded:

Configuration File (php.ini) Path: /usr/local/etc/php/7.4</hljs>
Loaded Configuration File:         /usr/local/etc/php/7.4/php.ini
Scan for additional .ini files in: /usr/local/etc/php/7.4/conf.d
Additional .ini files parsed:      /usr/local/etc/php/7.4/conf.d/ext-opcache.ini,
/usr/local/etc/php/7.4/conf.d/php-memory-limits.ini

Now check the ini file:

extension="redis.so"
extension="imagick.so"
zend_extension="xdebug.so"

Note that if you’re testing installed extensions via the CLI, you don’t need to restart nginx, apache or Valet when making changes to ini settings.

The second thing you can do, if you’re updating from an older PHP version which also used pecl to install extension; is to reinstall every extension individually.

pecl uninstall imagick
pecl install imagick

# Last step

Finally you should test and upgrade your projects for PHP 8 compatibility.

Update PHP Version in Mac

In this tutorial, we will introduce some methods to update to the latest stable version of PHP. As of current, the latest stable version of PHP is PHP 8.

Use Homebrew to Update to PHP 8 in Mac

We can utilize the Homebrew package manager to upgrade the current PHP version to the latest version in Mac. Homebrew is an open-source package management system that manages the installation of software in Mac and Linux. The package manager lets the user install and update the software according to the user. Installing PHP and upgrading it to the latest version is simplified by the use of Homebrew. It is the easiest way to install and upgrade PHP in Mac. We can use the following command to check the current version of PHP.

If brew is not previously installed in your system, type the code below in the macOS terminal to install brew.

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

After installing Homebrew, add it to the PATH using the following command.

echo "export PATH=/opt/homebrew/bin:$PATH" >> ~/.bash_profile
source ~/.bash_profile

To install PHP, we can use the command brew install php. After installing a fresh PHP, set PHP to PATH using the following command.

echo 'export PATH="/usr/local/opt/php8/bin:$PATH"' >> ~/.bash_profile
source ~/.bash_profile

To update the PHP version, update the brew using the command brew update. Then, use the command brew upgrade php. It upgrades the current version to the latest version of PHP. Then, restart the webserver to see the changes. Use the command sudo apachectl restart to restart the Apache server. If you use Nginx, use the command sudo nginx -s reload.

If we want to switch between PHP versions, we can use the following built-in command to update to PHP 8.

brew tap shivammathur/php
brew install shivammathur/php/php@8.0

It will allow us to switch back to the previous version of PHP. We can use the following command to switch the PHP versions.

brew link --overwrite --force php@8.0

The command above will switch the current PHP version to PHP 8. If we want to switch back to the previous version, we can replace the version in the above command.

Thus, we can upgrade to the latest version of PHP using Homebrew.

Понравилась статья? Поделить с друзьями:
  • Как изменить версию php apache2
  • Как изменить версию pdf
  • Как изменить версию office 2016
  • Как изменить версию npm пакета
  • Как изменить версию node js windows