How to Fix “pkg-config Command Not Found” Error on macOS

When installing certain packages or libraries on macOS, you may encounter the error “pkg-config command not found.” This occurs when a build script or configuration tool tries to call the pkg-config program but can’t locate it on your system.

The pkg-config program is used to retrieve information about installed libraries in Unix-based systems. It provides a standard way for builds and scripts to detect where libraries are located, their compile options, and associated dependencies.

So running into the “pkg-config not found” error usually means a library or program you’re trying to install has a build dependency on pkg-config which is not present.

In this guide, we’ll explain the common causes of this error on macOS and how to properly fix it by installing pkg-config. We’ll cover:

  • What is pkg-config and why the error occurs
  • How to install pkg-config on macOS
  • Additional steps for Homebrew, MacPorts, and Conda
  • Verifying pkg-config install and resolving other issues
  • Alternative solutions without using pkg-config

By the end, you’ll be able to troubleshoot and resolve the “pkg-config command not found” error for smooth library and package installations. Let’s dig in!

Understanding the pkg-config Command Not Found Error

The pkg-config program is a standard tool in Unix-based environments like Linux and macOS for managing library installations. It gets leveraged in many builds and scripts to:

  • Check if a required library is installed
  • Determine the correct compiler and linker flags needed to use the library
  • Find the library’s version number and other metadata

For example, a Python package being installed via setuptools may run pkg-config during its build to check for an external C library dependency. Or a Makefile build script might call pkg-config to get the right flags to compile source code against a library.

So if pkg-config itself can’t be found when these tools invoke it, the “command not found” error occurs, breaking the build or installation process.

On macOS, pkg-config is not installed by default. So this error commonly appears when trying to build projects that depend on it. Let’s look at how to properly install it.

Installing pkg-config on macOS

The easiest way to install pkg-config on macOS is via the Homebrew package manager.

To install Homebrew if you don’t already have it:

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

Then install pkg-config:

brew install pkg-config

This will download, compile, and install pkg-config onto your system.

To verify it installed properly, run:

pkg-config --version

You should see the version number printed out.

Now any build scripts, Makefiles, Python packages etc. that call pkg-config during compilation will be able to locate it, fixing the “command not found” error.

Using pkg-config with MacPorts

If you have MacPorts installed rather than Homebrew, you can also use it to install pkg-config:

sudo port install pkgconfig

Be sure to add the MacPorts bin directory to your PATH environment variable if needed so commands like gcc and pkg-config can be found:

export PATH=/opt/local/bin:$PATH

Using pkg-config with Conda

For Conda environments, pkg-config can be installed via:

conda install -c pkgs/main pkgconfig

This adds it to your active Conda environment. Be sure to reactivate the environment or reload your shell so the pkg-config command gets detected.

Verifying the Install and Other Solutions

To verify pkg-config is working after install, try running it manually:

pkg-config --version
pkg-config --modversion <library_name>

This should print out version info without “command not found” errors.

If it still isn’t found, ensure your PATH includes the directory where pkg-config got installed like /usr/local/bin or /opt/local/bin.

Finally, some additional solutions for “pkg-config not found” without installing it:

  • Pass compiler flags manually rather than relying on pkg-config.
  • Disable any pkg-config or compilation steps if not strictly required.
  • Use shared libraries instead of those needing compilation.
  • Update the project’s build scripts to remove pkg-config reliance.

But installing pkg-config properly is the best and standard resolution in most cases.

Conclusion

The “pkg-config command not found” error commonly appears during builds and installations on macOS because the tool isn’t included by default.

By following this guide, you now understand the purpose of pkg-config and how to reliably install it using Homebrew, MacPorts, or Conda.

With pkg-config present on your system, you can eliminate this error and successfully build libraries, programs, Python packages, and more. Be sure to properly add it to your PATH and verify the installation if issues persist.

Now you can breathe easy next time a build script fails with the notorious “pkg-config not found” – you’ve got the knowledge to fix it quickly and get back to work!

Leave a Comment