Homebrew: How to install a package offline (without internet connection)

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

Introduction

Homebrew is a popular package manager for macOS and Linux. It simplifies the process of installing, updating, and managing software. However, an active internet connection is typically required to download and install packages. This tutorial will cover steps to install a Homebrew package when you’re offline or lack internet connectivity.

Prerequisites

  • A computer running macOS or Linux with Homebrew installed.
  • The Homebrew package you want to install available as a formula or a bottle (a precompiled package).
  • An alternate means of downloading the necessary files through another computer with internet access, if the current one has none.

Part 1: Obtaining the Package

Assuming you have no direct internet access on the target computer, the first step is to use another device that does. Locate the Homebrew formula or a compatible bottle file for the software you wish to install. A Homebrew formula is a Ruby script that contains instructions for Homebrew on how to build software from source. In contrast, a bottle is a binary package that Homebrew can download and install without compilation.

# On the internet-enabled computer
# Find the formula or bottle link for the package
brew search PACKAGE_NAME

Copy the desired formula link or bottle URL.

Part 2: Transferring the Files

Download the necessary files using the copied link(s), using `curl` for example. Then, transfer them to the offline computer by using a USB drive or any other preferred transfer method:

# On the internet-enabled computer

# Download the bottle for offline installation
curl -O BOTTLE_URL

# Transfer the bottle file to the USB drive
# (assuming the drive is mounted at /Volumes/USB)</cp
cp PACKAGE_NAME.tar.gz /Volumes/USB/

Take the USB drive to the offline machine, and transfer the package files into its local storage.

Part 3: Setting Up Homebrew for Offline Installation

Before installing the package, set up the target computer by locating the `Homebrew` cache directory and transferring the downloaded bottle file there. The default cache directory for Homebrew is `~/Library/Caches/Homebrew` on macOS, and `~/.cache/Homebrew` on Linux.

# On the offline computer

# Copy the downloaded bottle to Homebrew’s cache directory

# For macOS
cp /path/to/usb/PACKAGE_NAME.tar.gz ~/Library/Caches/Homebrew/

# For Linux
cp /path/to/usb/PACKAGE_NAME.tar.gz ~/.cache/Homebrew/

Part 4: Installing the Package

With the installation files in place, proceed to install the package using Homebrew as if you were online:

# On the offline computer

# Run the installation command for Homebrew
brew install PACKAGE_NAME

If everything went smoothly, Homebrew will find the bottle in its cache and use it for installation instead of trying to download it. If it is a source installation (a ‘formula’), ensure that all dependencies are likewise available offline following the same method.

Part 5: Working With Dependencies

When installing packages offline, it’s crucial to account for dependencies. Repeat the download and transfer process for each dependency. If there are many, you may need to script this process.

# On the internet-enabled computer

# Inspect a package’s dependencies recursively
brew deps --tree --installed PACKAGE_NAME

Make sure you have all the dependencies in the Homebrew cache on your offline computer.

Part 6: Advanced Use: Using Homebrew’s Bottle Server

For advanced users or those dealing with many offline installations, running your local Homebrew bottle server might be beneficial. This allows mirroring Homebrew’s repositories and serving bottles within your local network.

You can clone the official Homebrew binary packages repository, and use a simple HTTP server like `nginx` or `apache`, hosting it locally. Then, set Homebrew to the local repository:

# Clone Homebrew’s binary packages (bottles) for offline access

# Set up your HTTP server to host the cloned repository

Eventually, point Homebrew to your local server by setting the `HOMEBREW_BOTTLE_DOMAIN` environment variable on the offline computer to your server’s address.

Part 7: Troubleshooting

Issues can arise from mismatches between the offline computer’s system and the bottle specs or from missing dependencies. Here’s how to troubleshoot common problems:

  • Check for mismatches between system versions and bottle requirements.
  • Ensure the formula/bottle file is not corrupt or the wrong version.
  • Verify that all dependencies are present in the Homebrew cache before installation.

Conclusion

Installing Homebrew packages offline requires a bit of preparation, but it’s certainly achievable. Whether you’re managing an offline server, dealing with strict network security, or just preparing for untethered development, following the steps in this guide will help you accomplish an offline installation seamlessly using the flexibility Homebrew offers.