You do not need the whole of Xcode to compile things on a Mac. If all you want is a compiler and the usual build tools for the terminal, Apple has a much smaller package, and one command fetches it.
xcode-select --install
This opens a dialog that downloads and installs the Command Line Tools. Apple describes them as a small, self-contained package with the macOS SDK and command-line tools such as Clang, installed in /Library/Developer/CommandLineTools. Homebrew mentions it in its install requirements too. On Apple silicon you only need the tools to build formulae from source; bottles and casks install without them.
To see which developer folder your tools use right now:
xcode-select -p
With only the Command Line Tools you get /Library/Developer/CommandLineTools. With Xcode installed and selected, it points inside Xcode.app. That matters because the developer tools in /usr/bin are shims that run the real tool from whichever folder is selected.
If you do have Xcode and want the terminal tools to use it, or you keep a beta next to it:
sudo xcode-select --switch /Applications/Xcode.app
The caveat with that one: --switch and --reset need sudo and change the setting for every user on the Mac. For one shell or one script, set the DEVELOPER_DIR environment variable instead, which the man page lists as an override that leaves the system-wide choice alone.
Further reading: Apple TN2339: Building from the Command Line with Xcode FAQ, xcode-select man page (macOS)
Going deeper: Guide: keep your Mac apps in a Brewfile, so a new MacBook is one command away