If you need to do local development of PHP projects on Mac, you’ll need PHP, probably multiple versions, even.
So, the first thing you’ll want to do, is installing MacPorts from the official website.
Once you did that, you fire up a Terminal and run:
sudo port selfupdate
sudo port install php74 php74-mysql php74-gd php74-curl php74-intl php74-iconv php74-mbstring php74-openssl php74-zip php74-exif php74-imagick
sudo port install php80 php80-mysql php80-gd php80-curl php80-intl php80-iconv php80-mbstring php80-openssl php80-zip php80-exif php80-imagick
sudo port install php81 php81-mysql php81-gd php81-curl php81-intl php81-iconv php81-mbstring php81-openssl php81-zip php81-exif php81-imagick
sudo port install php82 php82-mysql php82-gd php82-curl php82-intl php82-iconv php82-mbstring php82-openssl php82-zip php82-exif php82-imagick
sudo port install php83 php83-mysql php83-gd php83-curl php83-intl php83-iconv php83-mbstring php83-openssl php83-zip php83-exif php83-imagick
sudo port install php84 php84-mysql php84-gd php84-curl php84-intl php84-iconv php84-mbstring php84-openssl php84-zip php84-exif php84-imagick
This will install PHP 7.4 – 8.4 with multiple common extensions, which should give you an OK base to work with, especially if you need to develop for WordPress.
Note from 2024-12-24: php84-zip and php84-imagick are not yet available on MacPorts, so if you get an error try again in the future. I recommend using PHP 8.3 for now.
More database drivers
If you also need SQLite, run:
sudo port install php74-sqlite php80-sqlite php81-sqlite php82-sqlite php83-sqlite php84-sqlite
If you also need PostgreSQL (which will also install PostgreSQL):
sudo port install php74-postgresql php80-postgresql php81-postgresql php82-postgresql php83-postgresql php84-postgresql
If you also need Oracle DB (this does not currently work on arm64 (Apple Silicon), and instead only x86_64 and i386 (Intel Macs)!):
sudo port install php74-oracle php80-oracle php81-oracle php82-oracle php83-oracle
If you also need MongoDB:
sudo port install php74-mongodb php80-mongodb php81-mongodb php82-mongodb php83-mongodb
Caching
If you also need Memcached:
sudo port install php74-memcached php80-memcached php81-memcached php82-memcached php83-memcached
sudo port load memcached
If you also need OPcache:
sudo port install php74-opcache php80-opcache php81-opcache php82-opcache php83-opcache php84-opcache
Math
If you want to work with arbitrary-length integers using the GNU multiprocessing library:
sudo port install php74-gmp php80-gmp php81-gmp php82-gmp php83-gmp php84-gmp
Cryptography
If you want to do cryptographic operations with a known to be safe library (libsodium):
sudo port install php74-sodium php80-sodium php81-sodium php82-sodium php83-sodium php84-sodium
Xdebug
To install Xdebug for debugging:
sudo port install php74-xdebug php80-xdebug php81-xdebug php82-xdebug php83-xdebug
GeoIP
If you want to map IP addresses to geographic places (Make sure to obtain a MaxMind API key):
sudo port install php74-maxminddb php80-maxminddb php81-maxminddb php82-maxminddb php83-maxminddb
sudo port install php74-geoip php80-geoip php81-geoip php82-geoip php83-geoip
sudo port install geoipupdate
sudo mkdir -p /opt/local/share/GeoIP
sudo nano /opt/local/etc/GeoIP.conf
sudo geoipupdate
Updating all installed PHP versions
sudo port selfupdate
sudo port upgrade outdated
sudo port reclaim -N
Respond to all questions with yes.
Setting the default PHP version
If you want to have PHP 8.3 be the default version when simply running php
then do:
sudo port select php php83
Starting a local development server
To start a local development server, navigate to the “public” or “htdocs” (or whatever it’s called) directory of your project and run one of these:
php74 -S localhost:8080
php80 -S localhost:8080
php81 -S localhost:8080
php82 -S localhost:8080
php83 -S localhost:8080
php84 -S localhost:8080
Choose the command depending on the PHP version, then navigate to http://localhost:8080
in your web browser.
Conclusion
Once you know how, it’s straightforward to set up PHP. However, writing this article (and testing it), was not! I run this blog in my free time, consider donating a cup of coffee if I helped you out.
Update 2024-12-24
I updated the article to:
- Clarify how to set the default PHP version
- Added PHP83 and PHP84 ports as available
- Added Xdebug
Note that as of the time of this writing, PHP 8.4 on MacPorts is only RC1 and not all extensions are available. In some future article update I’ll add them as they become available, and I might remove PHP 7.4/8.0 from the guide.
Leave A Comment