Setup Homebrew on Windows

Homebrew is a package manager originally for macOS. Homebrew now officially supports Linux from version 2.0. At the same time, WSL (Windows Subsystem for Linux) is also officially supported.

This article describes that how to setup Homebrew on WSL.

TOC

Updates of this Article

October 24, 2022

  • English version of the article was published.

July 24, 2022

  • We have confirmed that it works on Windows 11 Pro 21H2. (At the time of writing this article, it was Windows 10)
  • Replace old commands with new ones.
  • Added note about the error curl error that occurs during installation.
  • Added more details about the installation process.
  • Added “Should the development environment be installed?”.

About Homebrew

Homebrew is a widely used package manager on macOS. Installing via Homebrew has the following goodies.

  • Software with dependencies can be installed at the same time.
  • Easy to update or uninstall installed softwares.

On Windows, many software can be easily uninstalled from “Apps and Features” in the system settings, so you may not see much benefit in the uninstallation.

However, I think they would benefit from updating software and resolving dependencies.

Some people also think that it is unnecessary because the installer makes it easy to install. With Homebrew, you just run one command in the console and it does the following for you. This would be easier than downloading and installing manually.

  1. Download the specified package.
  2. Download the packages have dependencies with main package.
  3. Install downloaded packages in order.
  4. Generate the needed script and execute it.

Is there any benefit on WSL?

Linux distributions on WSL have their own package manager. So, is there any benefit? Yes! For example, the following points.

  • You can use packages independent of the Linux distribution.
  • Packages are installed under the home directory, so it does not contaminate the system environment.

Linux distribution on WSL has a system level package manager such as apt, but installable packages are different for each distributions. If via Homebrew, it does not depend on the distribution as Homebrew will resolve the issue.

System requirements of Homebrew for Windows (WSL)

Windows version of Homebrew runs on the WSL (Windows Subsytem for Linux). The WSL is a compatibility layer that can execute Linux binaries on Windows. Therefore, it is not exactly true that a Windows version of Homebrew has appeared, but rather a Linux version has appeared and its officially supported on WSL.

Since you will need a WSL, you will need to set up your WSL first, see the following article for instructions on how to set up your WSL.

Install the Homebrew

Launch the shell of the WSL. Open the Homebrew official web page, copy the one line script is displayed at the “Install Homebrew” section, and execute it in the shell.

For example, if you installed according to the related article, you can start Ubuntu, right-click in the shell, and paste the copied script.

You will be asked for a password as the script run sudo on the way out, enter the password for UNIX that you set up during WSL setup.

==> Checking for `sudo` access (which may request your password)...
==> Select a Homebrew installation directory:
- Enter your password to install to /home/linuxbrew/.linuxbrew (recommended)
- Press Control-D to install to /home/akira/.linuxbrew
- Press Control-C to cancel installation
[sudo] password for akira:

In addition, if the timing is bad, an error message stating that the host cannot be found may appear as shown below. In that case, please execute again.

curl: (6) Could not resolve host: raw.githubusercontent.com

Press the RETURN key when the following appears.

Press RETURN/ENTER to continue or any other key to abort

Download and installation of Homebrew will be performed.

When running on macOS, this is all that is required, but on WSL, a little more work is needed.

To make sure that the packages installed by brew or Homebrew are included in the PATH environment variable, run the script written in the “Install” section of the following page. (Copy and execute one line at a time)

test -d ~/.linuxbrew && eval "$(~/.linuxbrew/bin/brew shellenv)"
test -d /home/linuxbrew/.linuxbrew && eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"
test -r ~/.bash_profile && echo "eval \"\$($(brew --prefix)/bin/brew shellenv)\"" >> ~/.bash_profile
echo "eval \"\$($(brew --prefix)/bin/brew shellenv)\"" >> ~/.profile

Once completed, do the following.

$ brew install hello

Error because the development environment is not installed

I got an error in my environment.

Error: patchelf must be installed: brew install patchelf
Warning: Bottle installation failed: building from source.
Error: The following formula
  patchelf
cannot be installed as binary package and must be built from source.
Install Clang or run `brew install gcc`.

Homebrew builds and installs from source when a binary install is not possible. Thefore, if the development environment is not installed on the WSL side, an error will occur.

Attempt to install gcc as instructed.

$ brew install gcc

However, this also resulted in an error. The error message is similar. It shows that a development environment is required.

A development environment is also required for Homebrew itself to work.

Install clang

Since we runs Ubuntu on WSL, it can be installed with apt. I mentioned that one of the advantages of Homebrew is that it does not pollute the system, but we needed a development environment.

You can use gcc, but I decided to install clang for my preference. Run as follows.

$ sudo apt update
$ sudo apt install clang

Looking at the dependencies, it seems that some gcc packages were also installed.

Should the development environment be installed?

As write above, Homebrew builds and installs from source when the binary cannot be installed. It is displayed that the installing gcc is recommended during installing Homebrew. The error did not occur on Ubuntu 20.04.4 LTS, but it would be better to install either gcc or clang.

Run it again

Now that the development environment is set up, let’s run it again.

$ brew install hello

Dependency packages will be built and installed. Once completed, run the program.

Let's share this post !

Author of this article

Akira Hayashiのアバター Akira Hayashi Representative, Software Engineer

I am an application developer loves programming. This blog is a tech blog, its articles are learning notes. In my work, I mainly focus on desktop and mobile application development, but I also write technical books and teach seminars. The websites of my work and books are here -> RK Kaihatsu.

TOC