It isn't always better for the environment to buy food locally. How it was farmed and how it was imported matter too. Most of the carbon footprint of groceries is from how they were grown and not how they were transported.
If it's in season where you live, then locally grown is best. If it's out of season, you need to consider a more nuanced point of view.