Brew下php71升级为php@7.1后SNMP扩展丢失,如何安装?
How to Install PHP SNMP Extension After Homebrew Moved PHP to Core Repository
Hey there, I totally get this frustration—when Homebrew moved PHP into their Core repository, those handy php71-snmp and php72-snmp formulae got axed, leaving us stuck without the SNMP extension. Don't worry, though—here are two solid ways to fix this:
Method 1: Use PECL (Recommended)
This is the quickest and most straightforward approach for most users:
- First, install the required SNMP system library. Run this in your terminal:
brew install net-snmp - Double-check your PHP version to make sure you're targeting the right one (e.g., 7.1 or 7.2):
php -v - Install the SNMP extension via PECL. If you have multiple PHP versions installed, use the version-specific PECL binary (like
pecl7.1orpecl7.2) instead of justpecl:pecl install snmp - Once installation finishes, enable the extension in your PHP config. Find your php.ini file location with
php --ini, then add this line to the file:extension=snmp.so - Restart your web server (Apache, Nginx, etc.) and confirm the extension is loaded by running:
php -m | grep snmp
You should see "snmp" in the output if it worked.
Method 2: Compile the Extension Manually (If PECL Fails)
If PECL throws errors (sometimes happens with older PHP versions), compiling the extension yourself works reliably:
- First, install dependencies (adjust the PHP version to match yours):
brew install net-snmp php@7.1 - Grab the PHP source code that matches your installed version. You can either download it from the official PHP site, or use the source that Homebrew cached for your PHP installation.
- Extract the source archive, then navigate to the
ext/snmpdirectory inside the source folder. - Run these commands to compile and install the extension:
phpize ./configure --with-snmp=/usr/local/opt/net-snmp make make install - Just like in the PECL method, add
extension=snmp.soto your php.ini file, restart your web server, and verify withphp -m | grep snmp.
A quick note: If you switch between multiple PHP versions with Homebrew, make sure you enable the extension in the php.ini file for each version you use.
内容的提问来源于stack exchange,提问作者simPod
相关产品推荐
相关产品推荐

