Setup Homebrew on Windows

Homebrew, originally a package manager for macOS, now officially supports Linux from version 2.0 and is also formally compatible with WSL(Window Subsystem for Linux).

This article describes how to set up Homebrew on WSL.

目次

Updates of this Article

October 24, 2022

  • The 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)
  • Replaced old commands with new ones.
  • Added a note about the curl command error that occurs during installation.
  • Added more details about the installation process.
  • Added the section “Should the development environment be installed?”.

About Homebrew

Homebrew is a widely used package manager for macOS, offering the following benefits when used for installations.

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

On Windows, many pieces of software can be easily uninstalled from “Apps and Features” in the system settings, which might make the uninstallation benefits of Homebrew less apparent.

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

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

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

Is there any benefit to WSL?

Linux distributions on WSL have their 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 differ for each distribution. If via Homebrew, it is independent of the distribution, as Homebrew will resolve the issue.

System requirements of Homebrew for Windows (WSL)

Windows version of Homebrew runs on the WSL (Windows Subsystem for Linux). The WSL is a compatibility layer that can execute Linux binaries on Windows. Therefore, it is not true that a Windows version of Homebrew has appeared. Still, a Linux version has appeared, and it’s 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. Next, open the Homebrew official web page, copy the one-line script displayed in the “Install Homebrew” section, and execute it in the shell.

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

You will be asked for a password as the script runs 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 terrible, 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 ensure that the packages installed by the brew or Homebrew are included in the PATH environment variable, run the script 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 the source when a binary install is impossible. Therefore, an error will occur if the development environment is not installed on the WSL side.

Attempt to install the 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.

Homebrew also requires a development environment.

Install clang

Since we run 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 written above, Homebrew builds and installs from the source when the binary cannot be installed. It is displayed that installing gcc is recommended during installing Homebrew. The error did not occur on Ubuntu 20.04.4 LTS, but installing either gcc or clang would be better.

Run it again

Now that the development environment is set up let’s rerun it.

$ brew install hello

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

$ hello
Hello, world!

著書紹介

Authored Books

よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

Akira Hayashi (林 晃)のアバター Akira Hayashi (林 晃) Representative(代表), Software Engineer(ソフトウェアエンジニア)

アールケー開発代表。Appleプラットフォーム向けの開発を専門としているソフトウェアエンジニア。ソフトウェアの受託開発、技術書執筆、技術指導・セミナー講師。note, Medium, LinkedIn
-
Representative of RK Kaihatsu. Software Engineer Specializing in Development for the Apple Platform. Specializing in contract software development, technical writing, and serving as a tech workshop lecturer. note, Medium, LinkedIn

目次