Tools: How To Install and Use Homebrew on macOS
Source: DigitalOcean
By Brian Hogan, Anish Singh Walia, Manikandan Kurup and Vinayak Baranwal The command line interface is a non-graphical tool that allows users to interact with their computer by typing text-based commands. It is an essential tool for software developers, letting them automate many daily tasks. While the macOS command line offers much of the functionality found in other Unix systems, it does not ship with a native package manager. Homebrew is a popular free and open-source package manager for macOS that fills this gap, allowing you to install developer tools like Python, Ruby, and Node.js directly from your terminal. In this tutorial, you will install and use Homebrew on your Mac. The guide will walk you through installing prerequisites like Xcode’s Command Line Tools and then running the Homebrew installation script. You will learn how to install, upgrade, and remove packages from the command line. Additionally, this article covers how to use Homebrew Cask to install desktop applications and provides the steps for uninstalling Homebrew if you no longer need it. You will need a macOS computer running Catalina or higher with administrative access and an internet connection. While older versions of macOS may work, they are not officially supported. To access the command line interface on your Mac, you’ll use the Terminal application provided by macOS. Like any other application, you can find it by going into Finder, navigating to the Applications folder, and then into the Utilities folder. From here, double-click the Terminal application to open it up. Alternatively, you can use Spotlight by holding down the COMMAND key and pressing SPACE to find Terminal by typing it out in the box that appears. To get more comfortable using the command line, take a look at [An Introduction to the Linux Terminal] (https://www.digitalocean.com/community/tutorials/an-introduction-to-the-linux-terminal). The command line interface on macOS is very similar, and the concepts in that tutorial are directly applicable. Now that you have the Terminal running, let’s install some additional tools that Homebrew needs. Xcode is an integrated development environment (IDE) that is comprised of software development tools for macOS. You won’t need Xcode to use Homebrew, but some of the software and components you’ll want to install will rely on Xcode’s Command Line Tools package. Execute the following command in the Terminal to download and install these components: You’ll be prompted to start the installation, and then prompted again to accept a software license. Then the tools will download and install automatically. You can now install Homebrew. To install Homebrew, you’ll download an installation script and then execute the script. First, download the script to your local machine by typing the following command in your Terminal window: The command uses curl to download the Homebrew installation script from Homebrew’s Git repository on GitHub. Let’s walk through the flags that are associated with the curl command: Before running a script you’ve download from the Internet, you should review its contents so you know what the script will do. Use the less command to review the installation script so you understand what it will do: Once you’re comfortable with the contents of the script, execute the script with the bash command: The installation script will explain what it will do and will prompt you to confirm that you want to do it. This lets you know exactly what Homebrew is going to do to your system before you let it proceed. It also ensures you have the prerequisites in place before it continues. You’ll be prompted to enter your password during the process. However, when you type your password, your keystrokes will not display in the Terminal window. This is a security measure and is something you’ll see often when prompted for passwords on the command line. Even though you don’t see them, your keystrokes are being recorded by the system, so press the RETURN key once you’ve entered your password. Press the letter y for “yes” whenever you are prompted to confirm the installation. Once the installation process is complete, you will want to put the directory Homebrew uses to store its executables at the front of the PATH environment variable. This ensures that Homebrew installations will be called over the tools that macOS includes. The file you’ll modify depends on which shell you’re using. ZSH is the default shell on macOS Mojave and higher. The Bash shell is a popular shell that older versions of macOS used as the default, and if you’ve upgraded your OS, you may still be using Bash. Execute the following command to determine your shell: You’ll see either bash or zsh. If you’re using ZSH, you’ll open the file ~/.zshrc in your editor: If you’re using the Bash shell, you’ll use the file ~/.bash_profile: Once the file opens up in the Terminal window, add the following lines to the end of the file: The first line is a comment that will help you remember what this does if you open this file in the future. To save your changes, hold down the CTRL key and the letter O, and when prompted, press the RETURN key. Then exit the editor by holding the CTRL key and pressing X. This will return you to your Terminal prompt. To activate these changes, close and reopen your Terminal app. Alternatively, use the source command to load the file you modified. If you modified .zshrc, execute this command: If you modified .bash_profile, execute this command: Once you have done this, the changes you have made to the PATH environment variable will take effect. They’ll be set correctly when you log in again in the future, as the configuration file for your shell is executed automatically when you open the Terminal app. Now let’s verify that Homebrew is set up correctly. Execute this command: If no updates are required at this time, you’ll see this in your Terminal: Otherwise, you may get a warning to run another command such as brew update to ensure that your installation of Homebrew is up to date. Follow any on-screen instructions to fix your environment before moving on. Now that Homebrew is installed, use it to download a package. The tree command lets you see a graphical directory tree and is available via Homebrew. Install tree with the brew install command: Homebrew will update its list of packages and then download and install the tree command: Homebrew installs files to /usr/local by default, so they won’t interfere with future macOS updates. Verify that tree is installed by displaying the command’s location with the which command: The output shows that tree is located in /usr/local/bin: Run the tree command to see the version: The version prints to the screen, indicating it’s installed: Occasionally, you’ll want to upgrade an existing package. Use the brew upgrade command, followed by the package name: You can run brew upgrade with no additional arguments to upgrade all programs and packages Homebrew manages. When you install a new version, Homebrew keeps the older version around. After a while, you might want to reclaim disk space by removing these older copies. Run brew cleanup to remove all old versions of your Homebrew-managed software. To remove a package you’re no longer using, use brew uninstall. To uninstall the tree command, execute this command: The output shows that the package was removed: You can use Homebrew to install desktop applications too. You’re not restricted to using Homebrew for command-line tools. Homebrew Cask lets you install desktop applications. This feature is included with Homebrew, so there’s nothing additional to install. While the standard Homebrew packages, known as formulae, typically handle command-line software, casks extend this capability to graphical user interface (GUI) applications. A cask is a script that automates the process of downloading a macOS application, such as its .dmg or .zip file, and moving it to your Applications folder. This gives you a way to manage both command-line tools and desktop apps using the same simple brew commands. Before installing an application, you may want to check if it’s available as a cask. You can do this with the brew search command. For example, to find out if the Firefox browser is available, you would run: Homebrew will return a list of matching formulae and casks. Casks are usually clearly marked. Let’s test it out by using Homebrew to install Visual Studio Code. Execute the following command in your terminal: The application will install: You’ll find the application in your Applications folder, just as if you’d installed it manually. While the command above works, the current recommended syntax is to include the --cask flag for clarity. This helps distinguish between installing a formula and a cask, especially when they share similar names. The updated command would be: Homebrew is smart enough to figure out your intent in most cases, but using the --cask flag is a good practice to adopt. You can manage your cask installations with a few simple commands. To see a list of all the cask applications you’ve installed, run: To get more detailed information about a specific cask, such as its version and installation date, use the brew info --cask command: To remove it, use brew uninstall: Homebrew will remove the installed software: It performs a backup first in case the removal fails, but once the program is fully uninstalled, the backup is removed as well. For a more thorough removal that also deletes configuration files associated with the application, you can add the --zap flag: Be careful with the --zap flag, as it will permanently delete all data associated with the application. If you no longer need Homebrew, you can use its uninstall script. Download the uninstall script with curl: As always, review the contents of the script with the less command to verify the script’s contents: Once you’ve verified the script, execute the script with the --help flag to see the various options you can use: The options display on the screen: Use the -d flag to see what the script will do: The script will list everything it will delete: When you’re ready to remove everything, execute the script without any flags: This removes Homebrew and any programs you’ve installed with it. Many developer tools, such as databases or web servers, need to run continuously in the background. Homebrew provides a simple command, brew services, to manage these background processes directly from the command line. This utility integrates with launchd, the standard macOS service manager, to start, stop, and list your services. Let’s see how this works with a practical example: installing and managing the PostgreSQL database. First, install PostgreSQL using the standard brew install command: Once the installation is complete, you can use brew services to start it. This command starts the PostgreSQL server immediately and configures it to launch automatically every time you log in. To see a list of all services that Homebrew is currently managing, use the list subcommand: The output shows the service name, its status, and the user it’s running as. If you need to stop the service, you can use the stop subcommand: This stops the service and prevents it from launching at login. The following table summarizes the most common brew services commands for quick reference. Using brew services provides a consistent way to manage your development-related daemons without manually configuring launchd. When setting up a new computer or onboarding a team member, ensuring a consistent development environment is important. A Brewfile is a plain text file that defines all your required Homebrew packages, including command-line formulae, desktop applications (casks), and even Mac App Store apps. This allows you to install all your dependencies with a single command. You can create a Brewfile that captures every package currently installed on your system. This is a good way to back up your existing setup. Navigate to a directory where you want to save the file, such as your home directory or a dotfiles repository, and run the bundle dump command: This creates a file named Brewfile in your current directory. Its contents will look something like this: When you move to a new machine, you can place this Brewfile in your home directory and run the bundle install command to set up your environment: Homebrew will read the Brewfile, install any missing packages, and skip any that are already installed. This makes the process repeatable and safe to run multiple times. This is also useful for project-specific dependencies. You can include a Brewfile in a project’s repository to ensure every developer has the required tools. The default Homebrew installation includes a core set of formulae, but there are thousands of other packages available through third-party repositories. In Homebrew, these repositories are called taps. The brew tap command allows you to add (or “tap”) these external repositories, making their packages available for installation. A popular example is the homebrew/cask-fonts tap, which previously contained a large collection of open-source fonts. This tap has since been deprecated and its fonts merged directly into the main Homebrew Cask repository. You can now install fonts without adding any additional tap. For example, to install the Fira Code font, you can run: Taps greatly expand the universe of software that Homebrew can manage, giving you access to specialized tools and applications maintained by the community. While brew doctor is the primary command for diagnosing issues with your Homebrew installation, a few other commands can provide deeper information. brew config: This command displays a summary of your system’s configuration as Homebrew sees it. This information is very useful when you are filing a bug report, as it provides maintainers with the context they need. brew deps --tree <formula>: This command shows you the dependency tree for a specific package. It helps you understand why certain packages were installed and how they relate to each other. For example, running brew deps --tree wget shows all the libraries wget depends on: These commands give you greater visibility into your Homebrew environment, making it easier to manage dependencies and resolve problems. Homebrew is generally reliable, but you might encounter issues from time to time. Most problems are easy to fix. This section covers common mistakes and the steps to resolve them. A good first step for any issue is to run Homebrew’s built-in diagnostic tool: This command checks your system for potential problems and often provides the exact commands needed to fix them. If brew doctor reports Your system is ready to brew, but you still have problems, check out the following common issues. Many installation errors for formulae or casks happen because your local copy of Homebrew is out of date. You might see errors like Error: Cask 'some-cask' is unreadable or fetching failures. Before assuming a package is broken, get the latest version of Homebrew and update the core formulae: It’s also good practice to upgrade your installed packages to their latest versions: Running these two commands regularly can prevent many common problems. Sometimes, after installing a tool, your shell reports command not found when you try to use it. This is almost always a PATH configuration issue. The PATH is an environment variable that tells your shell where to look for executable programs. First, find where Homebrew is installed. The executables you install will be in the bin subdirectory of this path (e.g., /opt/homebrew/bin). Display your current PATH to see if the correct Homebrew directory is included. Look for /opt/homebrew/bin (Apple Silicon) or /usr/local/bin (Intel) in the output. It should be near the beginning of the PATH string. If the path is missing, you need to add it to your shell’s configuration file. For Zsh, which is the default shell on modern macOS, this file is ~/.zshrc. Open the file in a text editor like nano: Add the correct line for your system at the end of the file. Save the file (CTRL+O, Enter) and exit nano (CTRL+X). Then, apply the changes to your current terminal session: The command you installed should now work correctly. Sometimes, brew install fails midway through the process. Here are two common causes. If a formula needs to be compiled from source, it requires Apple’s Command Line Tools. If they are missing, you will see an error related to xcrun or a missing compiler. To fix this, install the tools with this command: A dialog box will appear. Follow the prompts to complete the installation, and then try your brew install command again. On older Intel-based Macs, you might see permission errors related to the /usr/local/ directory. This happens when the current user does not have write permissions for Homebrew’s directories. An example error might look like this: Error: /usr/local/Cellar is not writable. You can fix this by changing the ownership of the Homebrew directory to your current user. This issue is uncommon on Apple Silicon Macs because Homebrew installs into /opt/homebrew/, a directory that the installer configures with the correct permissions from the start. When a Homebrew installation fails or brew doctor reports a warning, the resulting error messages can be difficult to understand. Instead of searching through forums for a solution, you can use an AI language model to diagnose the problem and get a direct solution. For instance, imagine you run brew install libvips and receive the following error: This error can be frustrating. However, you can copy this entire output and paste it into an AI assistant with a simple prompt. An AI assistant can analyze the log, identify the root cause—in this case, the missing libintl.h file points to a dependency on gettext—and provide a clear, step-by-step solution. The error fatal error: 'libintl.h' file not found indicates that a required dependency, gettext, is not properly linked in your system. Homebrew usually handles this, but sometimes it needs to be done manually. Here’s how to fix it: Install or relink gettext: First, ensure gettext is installed and up to date. Force the link: Next, force-link the formula to make its libraries available to other packages. Retry the installation: Once the link is created, try installing your original package again. This approach transforms a potential roadblock into a quick fix and a chance to understand how package dependencies work. Sometimes you have a problem to solve but don’t know which command-line tool is right for the job. An AI assistant can act as a discovery engine, recommending the best Homebrew packages for your specific task. The AI can suggest a suitable tool and even provide the commands to install and use it. The best tool for this is ImageMagick, a powerful command-line image processing suite. Install ImageMagick: You can install it with Homebrew by running: Convert your images: Navigate to the folder containing your .HEIC files and run the following command to convert all of them to JPEG format: This command will create a JPEG version of each HEIC file in the same directory. This interaction saves you from having to search for tools and read through documentation, allowing you to solve your problem in minutes. You can use AI to write shell scripts that automate your Homebrew workflows. This is useful for creating custom maintenance routines or checks that go beyond standard brew commands. For example, you might want to be notified only when a critical package has a security update pending. An AI can generate a functional script based on this request. Here is a shell script that checks for an openssl update. You can save it as check_updates.sh and run it periodically. To make the script executable, run chmod +x check_updates.sh. You can then run it anytime with ./check_updates.sh. By generating custom scripts, you can create automated, personalized workflows that fit your exact needs. Homebrew is a free and open-source package manager for macOS that allows you to install software through the command line. A package manager is a set of tools that automates the installation, configuration, and upgrading of software. You can use Homebrew to install developer tools such as Python, Ruby, and Node.js. Installing Homebrew involves a few steps from the macOS Terminal. You can use the brew install command to install both command-line tools and desktop applications. Homebrew is used to install command-line software, while Homebrew Cask is a feature within Homebrew used to install graphical desktop applications. Cask is included with the main Homebrew installation, so you don’t need to install anything extra to use it. You can find available packages using the brew search command followed by the name of the software you’re looking for. Homebrew will search its list of both command-line tools (formulae) and desktop applications (casks). For example, to search for the wget utility, you would run: This command will return a list of packages that match your search term. To remove a package, you use the brew uninstall command followed by the package name. This works for both command-line tools and desktop applications. Homebrew will remove the program and its related files from your system. To uninstall Homebrew, you must first download the official uninstall script. This command will remove Homebrew and all the software you installed with it from your system. You have now successfully installed and configured Homebrew, allowing you to manage both command-line tools and graphical desktop applications directly from your terminal. With this powerful package manager, you can streamline your development workflow by managing background services with brew services and creating reproducible environments using a Brewfile. You can also expand the available software by adding third-party repositories with brew tap. You are now equipped to diagnose and resolve common issues with brew doctor, giving you full control over your development environment. Go ahead and explore the thousands of packages now available to you with a single command. Homebrew has many packages you can install. Visit Homebrew Formulae to search for your favorite programs. To continue setting up your development tools, you can follow our guides for installing a specific programming language like Ruby on macOS or for setting up a package manager on a Linux server: Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases. Learn more about our products This textbox defaults to using Markdown to format your answer. You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link! Step 3 is giving me the following error - curl: (7) Failed to connect to raw.githubusercontent.com port 443: Connection refused What if I merely have macOS Sierra? What difference will that make? What can I do to make this work with Sierra? Update needed for Step 5: brew cask install <program> got updated. Now you install Desktop Programms with brew install --cask <program>. I hope the article may be updated, because it is super helpful for a System setup. Thank you very much for the guide. Really a clear explanation. Done with a MacBook Pro M1 on MacOS Monterey. This comment has been deleted for M1 map add instead export PATH=/opt/homebrew/bin:$PATH I’ve followed this guide till: brew installation script defaulted to the following path if it’s your case make sure to take note when brew prompt it during installation. Please complete your information! Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation. Full documentation for every DigitalOcean product. The Wave has everything you need to know about building a business, from raising funding to marketing your product. Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter. New accounts only. By submitting your email you agree to our Privacy Policy Scale up as you grow — whether you're running one virtual machine or ten thousand. From GPU-powered inference and Kubernetes to managed databases and storage, get everything you need to build, scale, and deploy intelligent applications.