How to Install/Upgrade OpenSSL with Homebrew

Updated: January 28, 2024 By: Guest Contributor Post a comment

OpenSSL is an open-source cryptographic library and tool that is widely used for secure communication over networks. Ensuring that it’s up-to-date is crucial for security. macOS users can easily install and upgrade OpenSSL using Homebrew, the popular package manager for macOS (and now Linux). In this tutorial, I will guide you through the process of installing and upgrading OpenSSL on your macOS machine using Homebrew, with step-by-step instructions and code examples.

Prerequisites

Before beginning this tutorial, you should have the following:

  • A macOS system
  • Access to terminal
  • Homebrew installed (installation instructions can be found at the official Homebrew website)

Installing Homebrew

If you haven’t already installed Homebrew, you can do it by pasting the following command into your terminal:

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

Homebrew makes it straightforward to install and keep OpenSSL up-to-date. To proceed, please ensure Homebrew is installed by running:

brew --version

You should see an output similar to:

Homebrew 3.X.Y
Homebrew/homebrew-core (git revision abcde; last commit 2023-04-01)

Installing OpenSSL

To install OpenSSL with Homebrew, execute:

brew install openssl

Homebrew will download and install the latest OpenSSL version along with its dependencies. Upon completion, you should see a summary like:

==> Downloading https://homebrew.bintray.com/bottles/openssl-X.Y.Z.mojave.bottle.tar.gz
==> Pouring openssl-X.Y.Z.mojave.bottle.tar.gz
🍺  /usr/local/Cellar/openssl/X.Y.Z: 1,793 files, 12MB

Note: X.Y.Z is a placeholder for the installed version number.

Configuring OpenSSL Path

Once installed, you may need to adjust your PATH environment variable to ensure your system uses the Homebrew-installed version of OpenSSL. To accomplish this, add the following line to your ‘.bash_profile’ or ‘.zshrc’ (depending on your shell):

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

Reload your profile or open a new terminal session to apply these changes.

Upgrading OpenSSL

To ensure you have the latest OpenSSL version installed via Homebrew, first update Homebrew’s formulae:

brew update

Then, upgrade OpenSSL:

brew upgrade openssl

Homebrew will update your OpenSSL installation to the latest version and show you an output like:

==> Upgrading openssl
==> Downloading https://homebrew.bintray.com/bottles/openssl-X.Y.Z_1.mojave.bottle.tar.gz
==> Pouring openssl-X.Y.Z_1.mojave.bottle.tar.gz
🍺  /usr/local/Cellar/openssl/X.Y.Z_1: 1,793 files, 12MB

If no upgrade is necessary, Homebrew will tell you so:

Warning: openssl X.Y.Z_1 already installed

Verifying the Installation

After the installation or upgrade, you can verify that the correct version of OpenSSL is installed by running:

openssl version

You should see something akin to:

OpenSSL 1.1.1k  25 Mar 2021

Advanced Usage

Advanced users may need to link applications against the Homebrew version of OpenSSL due to version requirements. Make sure to consult documentation relevant to your development environment or application for specific instructions on how to handle these dependencies. Here’s an example of fixing a compile issue with a version mismatch:

export LDFLAGS="-L/usr/local/opt/openssl/lib"
export CPPFLAGS="-I/usr/local/opt/openssl/include"

Some users may also want to set the PKG_CONFIG_PATH environment variable to help ‘pkg-config’ find the OpenSSL library:

export PKG_CONFIG_PATH="/usr/local/opt/openssl/lib/pkgconfig"

Homebrew also enables you to switch between different OpenSSL versions when necessary, which can be particularly useful if you are testing against multiple versions:

brew switch openssl X.Y.Z

Always be aware that OpenSSL updates are often related to security fixes. Avoid using outdated versions if not necessary for compatibility reasons.

Conclusion

In this tutorial, we’ve demonstrated how to install and keep OpenSSL up-to-date using Homebrew. This process simplifies the management of essential security tools on a macOS environment, enabling you to focus on development with confidence.