What is a brew/homebrew?

If you used a Linux before, you had to meet apt-get on Debian/Ubuntu system or yum on Centos/Red Hat. Brew as well as these tools is a package manager that allows you to install (most of) Linux ‘s libs and tools on your Mac! You can also create your own packages to easily install them on other Macs.

It can be interesting that even Microsoft noticed the values behind packages managers. They introduced it to Windows. I was really surprised when I read it.

Let’s leave Windows, and focus and MacOs/OSX. It’s worth to mention, that the following information and instructions are valid on High Sierra, Mojave, and Catalina.

[toc]

Install

So, it’s very easy. Everything that you need is the Terminal. When you ran it, copy and paste the following lines:

/bin/bash -c \
   "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"

What does that code do?

1. Fetch a shell script from the following URL: https://raw.githubusercontent.com/Homebrew/install/master/install
2. Execute it

How to use?

Brew provides a lot of commands that you can use, honestly, you should know only a few of them, which you will be using in common work. Let’s start with brew install and brew update. By using it you can install some package and update it, for example:

brew install apache2 

What does it do? Install apache2 on your Mac of course.

If you just want to upgrade apache, you have to run similar command, look at this:

brew update apache2

And that’s it! The newest version of apache2 has been installed and is ready to use.

Another useful command is brew services. Because a lot of tools work as a daemon (on the background) you can look at what is exactly working and turn it on or turn it off.

How to remove the not used library?

Sometimes we need to install some libraries, but we need them only temporarily. When we finish our job, we can do remove them from our Mac.

Brew shared with you a set of different commands (aliases) to do that. Let’s stick to our apache2 example.

brew rm --force apache2
// or
brew remove --force apache2 
// or
brew uninstall --force apache2

When you do that, it’s worth to run the following command to remove the rubbish as well.

brew cleanup

Alternatives

They do not exist! Yes, I’m just kidding, before “the brew ages” there was something called macport. Those tools have still more available packages but brew is easier and more popular now.

Keep brew always up to date

The software contains bugs, security vulnerabilities, or can be just incompatible with your version of macOs. This is the reason why it’s worth keeping all libraries, and brew as well, updated.

To keep brew and libraries always updated, you have to use the following commands:

brew update 
brew upgrade

Brew and different versions of PHP

brew loves php

How to install PHP 7.4 with brew

brew install php@7.4

The newest version of PHP. It includes support for PDO, MySQL, PostgreSQL, phpdbp, mbstring, soap, sqlite and much more.

How to install PHP 7.3 with brew

brew install php@7.3

Be aware! If you have to support a current project without a possibility to upgrade to PHP 7.4 you can install it, but active support is planned until the 6th of December 2020.

How to install PHP 7.2 with brew

brew install php@7.2

Important! You shouldn’t install this version of PHP. There is no active support anymore. The security support is planned utill the 30th of November 2020.

How to use different version of PHP at the same time?

As a developer, you have many different projects. Probably each of them requires different versions of PHP. With brew it’s really easy to use all of them without switching configuration, restarting notebook, or touching any services. You need aliases.

To make sure, that your aliases are always available you have to edit file .bash_profile in your home directory. In my case it’s /Users/marcin/.bash_profile. Open this file and add there following lines:

alias php@74='/usr/local/Cellar/php/*/bin/php'
alias php@73='/usr/local/Cellar/php@7.3/*/bin/php'
alias php@72='/usr/local/Cellar/php@7.2/*/bin/php'

Voila, to verify is it work you have to restart terminal andy try to execute next commands.

php@72 --version
php@73 --version
php@74 --version

If everything works fine, you should see something similar to:

PHP 7.2.31 (cli) (built: May 29 2020 02:00:47) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
PHP 7.3.19 (cli) (built: Jun 12 2020 00:29:59) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.19, Copyright (c) 1998-2018 Zend Technologies
    with Xdebug v2.7.1, Copyright (c) 2002-2019, by Derick Rethans
    with Zend OPcache v7.3.19, Copyright (c) 1999-2018, by Zend Technologies
PHP 7.4.7 (cli) (built: Jun 12 2020 00:04:10) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.7, Copyright (c), by Zend Technologies

Brew + PHP + Composer

brew install composer

If you are interested in extra methods of installing Composer you can check my other note. You will find there brew, docker, and direct method to install Composer.

You can also check things I didn’t know about Composer.

Problems

Do you have a problem? Just run another command brew doctor which shows you potential problems and propositions on how to fix them.

bash: brew: command not found

Have you got this message “command not found”? It means you have to install brew. How to do that? Just scroll up to section “Install”.