This week I had to get a Mac ready for a typical first-year course: C/C++ in Code::Blocks, plus MySQL and MySQL Workbench for the database module. I started from the usual setup guide that gets passed around: install the Xcode tools, install Rosetta, download Code::Blocks 13.12 from the official site, drag it to Applications, done.
That guide doesn't work. The Code::Blocks app downloaded fine, unzipped fine, and then I checked what kind of binary it actually was:
$ lipo -info /Applications/CodeBlocks.app/Contents/MacOS/CodeBlocks
Non-fat file: ... is architecture: i386
i386 means 32-bit Intel. macOS stopped running 32-bit apps entirely in Catalina (10.15, back in 2019), and Rosetta 2 only translates 64-bit Intel code. So the only Mac build on codeblocks.org won't open on any Mac sold in the last several years, Apple Silicon or Intel. Installing Rosetta for it, which half the guides out there tell you to do, does nothing.
The good news: MacPorts maintains a native 64-bit build of Code::Blocks 20.03, and their build servers show it building cleanly on macOS 26 on Apple Silicon. So here's the setup that actually works. I've also wrapped the whole thing in one script you can hand to students.
What you'll end up with
| Tool | What it's for |
|---|---|
| Apple clang | The C/C++ compiler (comes with Xcode Command Line Tools) |
| Code::Blocks 20.03 | The C/C++ IDE, native build via MacPorts |
| VS Code + C/C++ extension | A modern backup editor using the same compiler |
| MySQL Server 8.4 LTS | The database, running as a background service |
| MySQL Workbench | GUI for writing queries and designing tables |
You need an Apple Silicon Mac (M1 to M4) on macOS 13 or newer, roughly 10 GB of free space, 20 to 40 minutes, and your Mac login password.
The quick way: one script
The script installs everything in order. It skips anything that's already installed, so it's safe to run again, and it tests the compiler and the database at the end.
- Copy the whole script from the bottom of this post (the Full script section).
- Open Terminal (Cmd+Space, type Terminal, press Return) and run this. It saves whatever you just copied as a file:
pbpaste > ~/Downloads/setup-mac.sh
- Run it:
bash ~/Downloads/setup-mac.sh
- Answer the three things it asks:
- "Install Command Line Tools?" pop-up: click Install. You'll only see this on a Mac that's never had them.
- Password: your Mac login password. Nothing shows while you type. That's normal.
- New MySQL root password: make one up and write it down. Workbench asks for it every time.
The Code::Blocks step is the slow one, usually 5 to 15 minutes. If anything fails, fix it and run the same command again.
Want VS Code only, without Code::Blocks? Run bash ~/Downloads/setup-mac.sh --no-codeblocks.
What the script actually does (or: the manual way)
If you'd rather see each step, or the script trips over something on your machine, here it is by hand.
1. The compiler
xcode-select --install
Click Install in the pop-up. When it finishes, clang --version should print a version number. On a Mac, gcc is quietly just clang wearing a name tag, which matters later.
2. Homebrew
Homebrew is the package manager that installs MySQL, Workbench and VS Code. It asks for your Mac password:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
When it's done, it prints two lines under Next steps. Run them so the brew command works in new Terminal windows.
3. MySQL Server 8.4 LTS
I went with 8.4 because it's the long-term-support release, and coursework doesn't need anything newer.
brew install [email protected]
brew services start [email protected]
echo 'export PATH="/opt/homebrew/opt/[email protected]/bin:$PATH"' >> ~/.zshrc
source ~/.zshrc
mysql_secure_installation
Homebrew installs MySQL with no root password. mysql_secure_installation is where you set one. Answer Y to the remaining questions.
Why Homebrew instead of Oracle's DMG installer? Both work. Homebrew can be scripted end to end, and brew services start/stop [email protected] is easier to explain to a class than a System Settings panel.
4. MySQL Workbench and VS Code
brew install --cask mysqlworkbench visual-studio-code
In VS Code, open the Extensions tab (Cmd+Shift+X) and install C/C++ (Microsoft) and Code Runner.
5. Code::Blocks (the part that actually works)
Download the MacPorts installer for your macOS version from macports.org/install.php, run it, then open a new Terminal window:
sudo port selfupdate
sudo port install codeblocks
It lands in Applications → MacPorts → CodeBlocks. Homebrew and MacPorts live in separate folders (/opt/homebrew and /opt/local), so having both installed doesn't cause problems.
Checking it all works
Code::Blocks: Hello world
- Open Applications → MacPorts → CodeBlocks. If macOS complains, right-click → Open → Open.
- On the compiler pop-up, pick GNU GCC Compiler → Set as default. (Remember: gcc is clang here.)
- File → New → Project → Console application → Go → C++.
- Save it somewhere without spaces in the path, e.g.
Documents/CPrograms. Code::Blocks is fussy about spaces. - Open
main.cppunder Sources and press F9. A Terminal window should say Hello world!
MySQL from Terminal
Open a new Terminal window and run:
mysql -u root -p
Enter your root password. A mysql> prompt means the server is alive. Type exit to leave.
MySQL Workbench
Click + next to MySQL Connections and enter hostname 127.0.0.1, port 3306, username root. Click Test Connection, enter the password and tick Save password in keychain. Workbench 8.0 may warn that it doesn't support server 8.4. Click Continue Anyway; it's fine for coursework.
Then run this in a query tab (lightning-bolt icon, or Cmd+Shift+Return):
CREATE DATABASE college;
USE college;
CREATE TABLE students (id INT PRIMARY KEY, name VARCHAR(50));
INSERT INTO students VALUES (1, 'Asha');
SELECT * FROM students;
One row in the result grid and you're done.
Everyday commands
| Task | Command |
|---|---|
| Start the database | brew services start [email protected] |
| Stop the database | brew services stop [email protected] |
| Is it running? | brew services list |
| Compile and run C++ | clang++ main.cpp -o main && ./main |
| Compile and run C | clang main.c -o main && ./main |
MySQL starts on its own every time you log in. Stop it when you don't need it to save some battery.
Troubleshooting
| Problem | Fix |
|---|---|
| "App can't be opened" / "unidentified developer" | Right-click → Open → Open, or System Settings → Privacy & Security → Open Anyway |
command not found: brew or mysql |
Open a fresh Terminal window. Still missing? Re-run the script. |
| Code::Blocks: "compiler not found" | Run xcode-select --install, then Settings → Compiler → Reset defaults → GNU GCC Compiler |
| Code::Blocks builds but nothing runs | Settings → Environment → Terminal to launch console programs, set to osascript -e 'tell app "Terminal"' -e 'activate' -e 'do script "$SCRIPT"' -e 'end tell' |
| Code::Blocks path errors | Move the project to a folder with no spaces in its path |
| Workbench: "Can't connect to MySQL server" | brew services restart [email protected], and use 127.0.0.1 rather than localhost |
ERROR 1045 Access denied |
Wrong password. Check Caps Lock. |
| Forgot the root password (fresh install, nothing to keep) | brew services stop [email protected] && rm -rf /opt/homebrew/var/mysql && brew reinstall [email protected], then re-run the script. This wipes every database. |
Uninstalling at the end of term
brew services stop [email protected]
brew uninstall [email protected]
brew uninstall --cask mysqlworkbench visual-studio-code
sudo port uninstall --follow-dependencies codeblocks
Full script
Copy everything in the box below, then follow the steps in The quick way.
#!/bin/bash
# Mac setup for C/C++ and MySQL coursework (Apple Silicon, macOS 13+).
#
# Installs: Apple clang compiler, Homebrew, MySQL Server 8.4 LTS,
# MySQL Workbench, VS Code + C/C++ extensions,
# Code::Blocks 20.03 (native 64-bit build, via MacPorts).
#
# Run it from Terminal: bash setup-mac.sh
# Skip Code::Blocks: bash setup-mac.sh --no-codeblocks
# Safe to run again: steps that are already done are skipped.
set -euo pipefail
MYSQL_FORMULA="[email protected]"
INSTALL_CODEBLOCKS=1
[[ "${1:-}" == "--no-codeblocks" ]] && INSTALL_CODEBLOCKS=0
CODEBLOCKS_APP="/Applications/MacPorts/CodeBlocks.app"
bold() { printf '\n\033[1m==> %s\033[0m\n' "$1"; }
ok() { printf ' \033[32m✔\033[0m %s\n' "$1"; }
warn() { printf ' \033[33m!\033[0m %s\n' "$1"; }
fail() { printf '\n\033[31m✘ %s\033[0m\n' "$1"; exit 1; }
# ---------------------------------------------------------------- 0. checks
bold "Checking this Mac"
[[ "$(uname -s)" == "Darwin" ]] || fail "This script is for macOS only."
ARCH="$(uname -m)"
if [[ "$ARCH" == "arm64" ]]; then
BREW_PREFIX="/opt/homebrew"
ok "Apple Silicon (arm64), macOS $(sw_vers -productVersion)"
else
BREW_PREFIX="/usr/local"
warn "Intel Mac detected ($ARCH). The script will still work."
fi
FREE_GB=$(df -g / | awk 'NR==2 {print $4}')
(( FREE_GB >= 10 )) || fail "Need at least 10 GB free disk space (you have ${FREE_GB} GB)."
ok "${FREE_GB} GB free disk space"
# ------------------------------------------------------ 1. C/C++ compiler
bold "Step 1/6: C/C++ compiler (Xcode Command Line Tools)"
if xcode-select -p >/dev/null 2>&1 && clang --version >/dev/null 2>&1; then
ok "Already installed: $(clang --version | head -1)"
else
xcode-select --install || true
echo " A pop-up window has opened. Click 'Install' and wait for it to finish."
until xcode-select -p >/dev/null 2>&1 && clang --version >/dev/null 2>&1; do
sleep 10
done
ok "Installed: $(clang --version | head -1)"
fi
# ---------------------------------------------------------- 2. Homebrew
bold "Step 2/6: Homebrew (package manager)"
if [[ -x "$BREW_PREFIX/bin/brew" ]]; then
ok "Already installed"
else
echo " You will be asked for your Mac login password (nothing shows while you type)."
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
fi
eval "$("$BREW_PREFIX/bin/brew" shellenv)"
if ! grep -qs 'brew shellenv' ~/.zprofile; then
echo "eval \"\$($BREW_PREFIX/bin/brew shellenv)\"" >> ~/.zprofile
fi
ok "$(brew --version | head -1)"
# ------------------------------------------------------ 3. MySQL Server
bold "Step 3/6: MySQL Server 8.4 LTS"
if brew list "$MYSQL_FORMULA" >/dev/null 2>&1; then
ok "Already installed"
else
brew install "$MYSQL_FORMULA"
fi
MYSQL_BIN="$(brew --prefix "$MYSQL_FORMULA")/bin"
export PATH="$MYSQL_BIN:$PATH"
if ! grep -qs "$MYSQL_FORMULA/bin" ~/.zshrc; then
echo "export PATH=\"$MYSQL_BIN:\$PATH\"" >> ~/.zshrc
ok "Added the 'mysql' command to your Terminal (~/.zshrc)"
fi
# Start the server now and at every login.
brew services start "$MYSQL_FORMULA" >/dev/null 2>&1 || brew services restart "$MYSQL_FORMULA" >/dev/null 2>&1 || true
# (mysqladmin ping exits 0 whenever the server is up, even if the password is wrong.)
SERVER_UP=0
for _ in $(seq 1 30); do
if mysqladmin -u root --connect-timeout=2 ping >/dev/null 2>&1; then SERVER_UP=1; break; fi
sleep 1
done
(( SERVER_UP )) || fail "MySQL did not start. Run 'brew services restart $MYSQL_FORMULA' and try again."
ok "MySQL server is running on 127.0.0.1:3306"
# Set a root password if the account does not have one yet.
if mysql -u root -e "SELECT 1" >/dev/null 2>&1; then
echo
echo " Choose a password for the MySQL 'root' user."
echo " Write it down: you need it for Workbench and the terminal."
while true; do
read -r -s -p " New MySQL root password: " PW1; echo
read -r -s -p " Type it again: " PW2; echo
if [[ -z "$PW1" ]]; then warn "Password cannot be empty."; continue; fi
if [[ "$PW1" != "$PW2" ]]; then warn "Passwords do not match. Try again."; continue; fi
if [[ "$PW1" == *"'"* || "$PW1" == *"\\"* ]]; then warn "Please avoid ' and \\ in the password."; continue; fi
break
done
mysql -u root -e "ALTER USER 'root'@'localhost' IDENTIFIED BY '${PW1}'; FLUSH PRIVILEGES;"
MYSQL_ROOT_PW="$PW1"
unset PW1 PW2
ok "Root password set"
else
ok "Root password already set (skipping)"
fi
# -------------------------------------------- 4. Workbench and VS Code
bold "Step 4/6: MySQL Workbench and VS Code"
for cask in mysqlworkbench visual-studio-code; do
case "$cask" in
mysqlworkbench) APP="/Applications/MySQLWorkbench.app" ;;
visual-studio-code) APP="/Applications/Visual Studio Code.app" ;;
esac
if [[ -d "$APP" ]]; then
ok "$(basename "$APP" .app) already installed"
else
brew install --cask "$cask"
ok "$(basename "$APP" .app) installed"
fi
done
CODE_CLI="/Applications/Visual Studio Code.app/Contents/Resources/app/bin/code"
for ext in ms-vscode.cpptools formulahendry.code-runner; do
"$CODE_CLI" --install-extension "$ext" --force >/dev/null 2>&1 \
&& ok "VS Code extension: $ext" \
|| warn "Could not install VS Code extension $ext (install it from the Extensions tab)"
done
# -------------------------------------------------------- 5. Code::Blocks
# The official Code::Blocks Mac download (13.12) is a 32-bit app and cannot run
# on any Mac since macOS 10.15. MacPorts provides a native 64-bit build instead.
bold "Step 5/6: Code::Blocks IDE (via MacPorts)"
if (( ! INSTALL_CODEBLOCKS )); then
ok "Skipped (--no-codeblocks)"
elif [[ -d "$CODEBLOCKS_APP" ]]; then
ok "Already installed"
else
if [[ ! -x /opt/local/bin/port ]]; then
MACOS_MAJOR="$(sw_vers -productVersion | cut -d. -f1)"
PKG_URL="$(curl -fsSL https://api.github.com/repos/macports/macports-base/releases/latest \
| grep -oE "https://[^\"]+MacPorts-[0-9.]+-${MACOS_MAJOR}-[A-Za-z]+\.pkg" | head -1)" || true
[[ -n "$PKG_URL" ]] || fail "No MacPorts installer found for macOS $MACOS_MAJOR. See https://www.macports.org/install.php"
PKG_FILE="$(mktemp -d)/$(basename "$PKG_URL")"
echo " Downloading $(basename "$PKG_URL")..."
curl -fL --progress-bar -o "$PKG_FILE" "$PKG_URL"
echo " Installing MacPorts. Enter your Mac login password if asked (nothing shows while you type)."
sudo installer -pkg "$PKG_FILE" -target /
rm -f "$PKG_FILE"
fi
ok "MacPorts installed"
echo " Installing Code::Blocks (usually 5-15 minutes)..."
sudo /opt/local/bin/port -N selfupdate
sudo /opt/local/bin/port -N install codeblocks
[[ -d "$CODEBLOCKS_APP" ]] || fail "Code::Blocks did not install. Re-run this script, or use VS Code instead."
ok "Code::Blocks installed in Applications → MacPorts"
fi
# ----------------------------------------------------------- 6. verify
bold "Step 6/6: Testing everything"
TEST_DIR="$(mktemp -d)"
cat > "$TEST_DIR/hello.cpp" <<'EOF'
#include <iostream>
int main() { std::cout << "Hello world!" << std::endl; return 0; }
EOF
if clang++ -std=c++17 "$TEST_DIR/hello.cpp" -o "$TEST_DIR/hello" && [[ "$("$TEST_DIR/hello")" == "Hello world!" ]]; then
ok "C++ compile and run: Hello world!"
else
warn "C++ test failed. Re-run: xcode-select --install"
fi
rm -rf "$TEST_DIR"
if [[ -n "${MYSQL_ROOT_PW:-}" ]]; then
SQL_OUT=$(MYSQL_PWD="$MYSQL_ROOT_PW" mysql -u root -h 127.0.0.1 -N -e \
"CREATE DATABASE IF NOT EXISTS setup_check; USE setup_check;
CREATE TABLE t (id INT PRIMARY KEY, name VARCHAR(50));
INSERT INTO t VALUES (1, 'Asha'); SELECT name FROM t; DROP DATABASE setup_check;" 2>&1) || true
[[ "$SQL_OUT" == "Asha" ]] && ok "MySQL query test passed" || warn "MySQL query test failed: $SQL_OUT"
else
ok "MySQL test skipped (password set earlier). Test with: mysql -u root -p"
fi
bold "All done!"
cat <<EOF
Code::Blocks: Applications → MacPorts → CodeBlocks. On first launch choose
"GNU GCC Compiler" → Set as default (on a Mac this is Apple clang).
VS Code : open a folder, create main.cpp, press ▷ (top right) to run.
Terminal: clang++ main.cpp -o main && ./main
MySQL : in a NEW Terminal window: mysql -u root -p
Workbench: open MySQL Workbench → + next to "MySQL Connections"
Hostname 127.0.0.1, Port 3306, Username root → Test Connection.
Start/stop the database: brew services start $MYSQL_FORMULA
brew services stop $MYSQL_FORMULA
EOF
If you're a teacher handing this out: test it on one student Mac before the first lab. If you're a student and something in here breaks, the troubleshooting table covers what I've hit so far. VS Code is always a fine fallback, since it uses exactly the same compiler.
Sources: MacPorts: codeblocks · Homebrew: [email protected] · MySQL Workbench downloads · Code::Blocks binary downloads