24

When I try to install homebrew on Ubuntu 18.04

# Dockerfile
FROM ubuntu:18.04

RUN apt-get update && apt-get install build-essential curl file git -y
RUN sh -c "$(curl -fsSL https://raw.githubusercontent.com/Linuxbrew/install/master/install.sh)"

getting errors:

==> Add Ruby to your PATH by running: PATH=/root/.linuxbrew/Homebrew/Library/Homebrew/vendor/portable-ruby/current/bin:$PATH Don't run this as root!

2
  • Is there a specific reason you want this? The Ubuntu package repositories tend to be pretty complete and you can usually apt-get install whatever your application’s dependencies are without trouble. You don’t generally need an extra package manager. Commented Oct 8, 2019 at 21:05
  • 1
    @DavidMaze The reasons are easier to maintain and read Dockerfile. I can do what I need with one-line brew command instead of several commands with apt-get install like brew install composer Commented Oct 10, 2019 at 6:40

4 Answers 4

23

Is there a reason you can't use the official image (docker pull linuxbrew/linuxbrew)? It is based on Ubuntu 16.04 / Xenial.

If you must use Bionic (18.04), the correct way to install homebrew will be to follow the steps in the official Dockerfile.

But to get your Dockerfile working, you need to install ruby, create a non-root user and execute the installation script as that user. Like so,

FROM ubuntu:18.04

RUN apt-get update && \
    apt-get install build-essential curl file git ruby-full locales --no-install-recommends -y && \
    rm -rf /var/lib/apt/lists/*

RUN localedef -i en_US -f UTF-8 en_US.UTF-8

RUN useradd -m -s /bin/bash linuxbrew && \
    echo 'linuxbrew ALL=(ALL) NOPASSWD:ALL' >>/etc/sudoers

USER linuxbrew
RUN sh -c "$(curl -fsSL https://raw.githubusercontent.com/Linuxbrew/install/master/install.sh)"

USER root
ENV PATH="/home/linuxbrew/.linuxbrew/bin:${PATH}"

PS: I have added --no-install-recommends to ignore optional dependencies and rm -rf /var/lib/apt/lists/* to remove apt-get leftovers thus reducing the image size. Also, locales is added to install UTF-8 or brew will throw a warning when you run the command.

Sign up to request clarification or add additional context in comments.

1 Comment

The URL has changed slightly. Warning: Linuxbrew has been merged into Homebrew. Please migrate to the following command: /bin/bash -c "$(curl -fsSL raw.githubusercontent.com/Homebrew/install/master/install.sh)"
10

Gabriel's answer mostly worked for me, but was missing one step. I needed to chown the /home/linuxbrew/.linuxbrew folder to the user that would be running Homebrew:

RUN apt-get update && \
    apt-get install -y -q --allow-unauthenticated \
    git \
    sudo
RUN useradd -m -s /bin/zsh linuxbrew && \
    usermod -aG sudo linuxbrew &&  \
    mkdir -p /home/linuxbrew/.linuxbrew && \
    chown -R linuxbrew: /home/linuxbrew/.linuxbrew
USER linuxbrew
RUN /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"

USER root
RUN chown -R $CONTAINER_USER: /home/linuxbrew/.linuxbrew

1 Comment

Would you consider copy/pasting Gabriel's answer into your answer, so there is a complete list of the instructions? (It would be easy to see only the "new correct way" comment, and miss the chown command...) Thanks.
8

The above answers did not work for me on Ubuntu 22.04 Docker container.

I had to add couple of steps Here's the full code:

RUN apt-get update && \
    apt-get install -y -q --allow-unauthenticated \
    git \
    sudo
RUN useradd -m -s /bin/zsh linuxbrew && \
    usermod -aG sudo linuxbrew &&  \
    mkdir -p /home/linuxbrew/.linuxbrew && \
    chown -R linuxbrew: /home/linuxbrew/.linuxbrew
USER linuxbrew
RUN /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
USER root
RUN chown -R $CONTAINER_USER: /home/linuxbrew/.linuxbrew
ENV PATH="/home/linuxbrew/.linuxbrew/bin:${PATH}"
RUN git config --global --add safe.directory /home/linuxbrew/.linuxbrew/Homebrew
USER linuxbrew
RUN brew update
RUN brew doctor

1 Comment

For some weird reason, on one of my machines, using the official install script alone didn't work. With this workaround, by creating the linuxbrew user upfront got my image build over the line. Thank you
7

The new correct way is:

RUN apt-get update && \
    apt-get install -y -q --allow-unauthenticated \
    git \
    sudo
RUN useradd -m -s /bin/zsh linuxbrew && \
    usermod -aG sudo linuxbrew &&  \
    mkdir -p /home/linuxbrew/.linuxbrew && \
    chown -R linuxbrew: /home/linuxbrew/.linuxbrew
USER linuxbrew
RUN /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.