Create development environment on MacOS using Homebrew

I prefer MacOS to develop, because it’s much more stable than Windows 10 – last issues with October update are the best evidence, that something is not ok with this OS development. It’s similar to Linux in many ways, but not exactly Linux like Debian, Ubuntu, RHEL etc. In effect, we can’t just use build-in repositories to install some required software, services and servers. But there is very nice option called Homebrew. Using this, we can make MacOS even closer to Linux system and configuration will be simpler.

You can ask me, why don’t use Docker or Vagrant? Yes, it’s very nice option, the best in many cases. But sometimes I need more “comfortable” and more elastic solution, faster way to run or test some code, without separate environment, without containers for that. Then we can use XAMPP or MAMP. It very nice solution, but also have some important issues. These ready packages are often difficult to modify, to extend using for example additional PHP modules. I tried both and can’t recommend for longer period of time, because it will be real nightmare. Here is solution: Homebrew. What’s that? It’s a “package manager” for MacOS. Using that, we get access to nice, big repository with a lot of useful packages.

Homebrew uses git, also doesn’t modify any system files (like buil-in Apache on MacOS), but creates separate directory for all libs and configurations, then uses symlinks. User can manage all installed services using manager in terminal, also install, check or remove packages also using terminal – fast and comfortable as in Linux systems. This post is not about whole configuration, because you can find some other articles about that on my blog – many of them are about VPS configuration, but you can also use these tutorials to create local environment after installing some services from Homebrew. First step is to install Homebrew on MacOS. I suggest to always use official website, but also paste here code to do this from terminal:

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

If everything will be ok, you will can use “brew” command in terminal. And in real, it’s all you need to do and improve your possibilities in MacOS a lot. After that, you can browse Homebrew repository using brew search PACKAGE command, and install it using: 

brew install PACKAGE

brew install PACKAGE. Of course many packages have different names than on Linux repositories, but differences aren’t huge. Also, many packages are only in one version, without option to add extensions. For example search PHP by using brew search PHP. In time when I write this post, there is only [email protected], [email protected] and [email protected] versions, without any extensions.

How can we for example add additional modules like mongodb support in PHP? You should use pecl to do this. This repository provides a lot of PHP extensions, and installation is also very simple – just type:

pecl install EXTENSION

Some of them need additional packages in system to compile and build – then we back to Homebrew and can install these packages. What about configuration files? You will find all on /usr/local/etc/php/your-version. You can then modify php.ini, modify PHP-FPM settings and add new pools. Also, installed PHP is available on system terminal. In the same way, you can install Nginx, new Apache version, several databases and other services.

One additional thing is brew services command. After install any service running in background (for example, MySQL server, Nginx), there will be instruction how to add it to autostart daemons. You can use this build-in instructions, and also manage all services using brew services – with list show all available services with statuses, restart, start, stop and also cleanup. And the last tip – there is additional repository to Homebrew called Cask. You can install it (and also, add other repositories in that way) by running from terminal:

brew tap caskroom/cask

All repositores are “taps”, and you can add this to your base. You can find list of interesting taps on official Homebrew website. What about Cask? It repository that allows you to install many popular applications in simpler, faster way than going to websites, download file and move into application folder. With Cask you can just type brew install chromium, or brew install thunderbird. All packages are based on official sources, so, it’s secure. Feel free to write about nice taps or Homebrew formulas. If you want to configure your services, read other tutorials for each of them:

Nginx installation and first configuration

Nginx virtual host configuration

Use PHP-FPM with Nginx

Describe and change php.ini settings

PHP-FPM – config improvements

Use strong Nginx encryption settings

Nginx optimizations, tips and tricks