Xcode is the free development environment for macOS distributed by Apple. If you already have Xcode installed on your computer, you can skip this step. If you don't, open a Terminal window and run the following command:
xcode-select --install
If you get an error message, then the command-line tools are already installed and you can go to the next step. Otherwise, a window will pop up asking for confirmation. Press the Install button and, when prompted, accept the license agreement.
To check that the command-line tools were correctly installed, run the following command in the Terminal:
gcc -v
This command prints information about the gcc compiler present in your computer, which will be similar to the output shown as follows:
Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 8.1.0 (clang-802.0.38)
Target: x86_64-apple-darwin16.4.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
If you get no error message, the command-line tools are properly installed.