Bring a large pot of salted water to a boil. Add the spaghetti and cook until al dente, about 8-10 minutes, then drain, reserving 1/2 cup of the pasta water.
While the pasta cooks, heat the olive oil in a large pan over medium heat. Add the garlic and red pepper flakes, cooking until the garlic is fragrant and golden, about 2 minutes, being careful not to burn it.
Add the drained spaghetti to the pan with the garlic and oil. Toss to coat, adding a little reserved pasta water if needed to help the sauce cling to the pasta.