Mostly this error occurs when you have installed a newer version of Xcode. And the command line tools are yet to be initialized.
To resolve this: Go under Xcode Preferences → Locations (tab) → And the bottom options is Command Line Tools. Please select the Xcode version for tools.
Example (Forfor Xcode 10.0 (10A255)): Go under Xcode Preferences → Locations (tab) → And the bottom options is Command Line Tools. Select Xcode 10.0 (10A255).
For more details, please see this image: