We investigate the buckling under compression of a slender beam with a distributed lateral elastic support, for which there is an associated cost. For a given cost, we study the optimal choice of support to protect against Euler buckling. We show that with only weak lateral support, the optimum distribution is a delta-function at the centre of the beam. When more support is allowed, we find numerically that the optimal distribution undergoes a series of bifurcations. We obtain analytical expressions for the buckling load around the first bifurcation point and corresponding expansions for the optimal position of support. Our theoretical predictions, including the critical exponent of the bifurcation, are confirmed by computer simulations.