This paper focuses on the design of the optimal coupling gain for a consensus protocol applied to single integrator agents. An LQR-based cost function is proposed, with modifications that are suited for the consensus problem and aim at obtaining the coupling gain that performs better in average. It is shown that this cost function is convex with respect to the coupling gain, thus enabling the use of tools from convex optimization, such as the gradient descent method, to compute the optimal coupling gain. An exact expression for the gradient of the cost is also provided. Moreover, under some simplifying assumptions, a closed-form expression for the optimal coupling gain is also derived. Finally, a numerical example is given to illustrate the results presented in the paper.