Kitware Packages on OS X with Homebrew
Homebrew is a package manager for OS X, which has become very popular in recent years. The homebrew-core repository provides more than 3,500 packages. There are multiple other repositories, like homebrew-science, where you can find tools from the Kitware ecosystem such as the Visualization Toolkit (VTK), the Insight Segmentation and Registration Toolkit (ITK), and Paraview.
This article describes the main principles of Homebrew and discusses its routine usage.
Installation
Installing Homebrew is easy. Just use the following:
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
This command will clone the homebrew-core Git repository to your /usr/local folder and set up Homebrew.
Each package is defined by a Ruby script, called a “formula.” A package repository is called a “tap.” The /usr/local/Library/Taps folder will contain different taps (homebrew-core, homebrew-science, etc.). Each tap contains different formulas. The full list of Homebrew repositories can be found here.
Installing Packages
First, let’s install an awesome tool: CMake.
brew install cmake
CMake will be installed at /usr/local/Cellar/cmake/3.5.2/bin/cmake and symlinked to /usr/local/bin/cmake. You can now run CMake from the command line. Most of the packages have pre-compiled binaries, called “bottles.” The bottles get poured to your computer, so you don’t have to recompile the package.
Now, let’s install ITK. You first need to “tap” the science repository. This will clone the homebrew-science repository to /usr/local/Library/Taps/homebrew/homebrew-science and prepare the formulas for use:
brew tap homebrew/homebrew-science
Then, you can run the following:
brew install vtk brew install insighttoolkit
VTK and ITK should now be installed on your system.
If you need Python wrappings, use the following commands:
brew install vtk --with-python brew install insighttoolkit --with-python
Useful Commands
The below example for CMake shows how you can get more information about a formula.
$ brew info cmake cmake: stable 3.5.2 (bottled), HEAD Cross-platform make https://www.cmake.org/ /usr/local/Cellar/cmake/3.5.2 (2,010 files, 27.5M) * Poured from bottle From: https://github.com/Homebrew/homebrew-core/blob/master/Formula/cmake.rb ==> Dependencies Build: sphinx-doc ==> Options --with-completion Install Bash completion (Has potential problems with system bash) --without-docs Don't build man pages --HEAD Install HEAD version ==> Caveats Emacs Lisp files have been installed to: /usr/local/share/emacs/site-lisp/cmake
Most formulas have install options. The list of options can be found using the brew info [package name] command. While the bottles come with the most sensible options pre-selected, you may want to recompile the package locally with other options. There are few package managers that allow such flexibility!
One of the most frequently used commands is the brew update command. This command will fetch the latest changes from the Git repositories and update all the formulas. The brew upgrade command will remove older packages and install the newest versions.
Of course, you can get more help using man brew or brew −−help. You can also reference the documentation.
Modifying a Formula
Writing or modifying a formula is very easy. As an example, here is a simplified version of the CMake formula. The full formula (with build options, dependencies, etc.) can be found here.
class Cmake < Formula desc "Cross-platform make" homepage "https://www.cmake.org/" url "https://cmake.org/files/v3.5/cmake-3.5.2.tar.gz" sha256 "92d8410d3d981bb881dfff2aed466da55a58d34c..." head "https://cmake.org/cmake.git" bottle do sha256 "2366e55b9466d7f8499c21784711e7006ed36..." => :el_capitan sha256 "b6211f8e35ea232c822da67b03fe76892d216c..." => :yosemite sha256 "0f1a4cf28813ee8f95f06267902f2d9fb58630..." => :mavericks end def install system "./bootstrap", *args system "make" system "make", "install" end test do (testpath/"CMakeLists.txt").write("find_package(Ruby)") system "#{bin}/cmake", "." end end
The syntax is quite self explanatory. There is a short description, a homepage URL, the URL for the source code, and it’s sha256. Then comes the address of the repository for the project. When building the package with brew install cmake –HEAD , the latest version is compiled and installed.
Next comes the bottle block, which defines the binaries that get automatically poured when you install the formula. Homebrew’s policy is to support the last three versions of OS X. At the end comes the install instructions and, finally, a small test block.
Once you are happy with your changes, you may want to share your work. To do so, just fork the right Homebrew Git repository, and submit a pull request. Make sure to read the contributing guidelines carefully before making a submission. There are no specific maintainers for the packages; anybody can make a change. That being said, changes do need to be merged by one of the main Homebrew maintainers.
Some thoughts
The first two main package managers on Mac were Fink (apt-based) and MacPorts. Homebrew was released in 2009, and it has grown at a steady pace since then. I will not make a comparison between the three package managers, as each of these has advantages and disadvantages. I will, however, list some advantages of Homebrew.
Homebrew has organically grown in a very open nature. All of the source code for Homebrew is hosted on GitHub, and everybody is free to participate. The success of Homebrew may also be linked to the success of GitHub. People are used to employing open pull requests on GitHub for their favorite open-source projects. This familiarity has facilitated the update of Homebrew, as the workflow is the same.
In addition, Homebrew does not need to run commands with Sudo. The logic here is that you should be able to install whatever application or library you want without compromising the security of your Mac. This is why Homebrew installs everything to /usr/local/ . (You can select a different location, however, if you choose). No files will be installed outside of the Homebrew prefix. Also, this folder is already in your PATH.
Another point is that there is quite a strict policy regarding patches. Patches are tolerated, but they need to be pushed upstream first. This has led to a huge effort to keep the formulas patch free. The packages are managed in a very dynamic way, as they tend to be updated quite rapidly, when new versions come out. The frequent nature of these updates has the disadvantage that you need to keep up with the new incoming versions. Nevertheless, you can “pin” a formula and keep the old version.
For example, the download statistics for CMake (August 2015 to May 2016) indicate that most people have an up-to-date system. This is also true for a lot of other packages.
You may also note that there is a very interesting project, which brings Homebrew to Linux, called Linuxbrew. It’s worth giving it a try.
Conclusion
Homebrew has proven to be a reliable way to install libraries on Mac OS X for years. The addition of CMake, VTK, ITK and Paraview was straightforward, and the community is keeping an eye out to make sure that they are up to date. One project, for example, aims to provide the ITK package with binaries that contain the wrapped interfaces for Python, so you don’t have to compile it from source.
Enjoy Homebrew and the Kitware packages!
Shouldn’t it be: brew install insighttoolkit ?
Yes, thanks for the note! The post has been updated to correct the package name.
I installed everything, but in python. Import vtk, do not work.
Hi. Please open an issue here: https://github.com/Homebrew/homebrew-science, and fill out the issue template, so we can help you debug this. Thanks.
Hello, the insighttoolkit seems to not exist, the command brew install insighttoolkit gives out:
Error: No available formula with the name “insighttoolkit”
VTK is working fine though
The tap https://github.com/Homebrew/homebrew-science is no longer active. Would be great if ITK could make it into the default tap (it seems VTK is in).
An attempt to port to homebrew-core is here:
https://github.com/Homebrew/homebrew-core/pull/18180
A contribution to update the formula and re-open the PR would be welcome.