We identify the optimal operating conditions of an entangling two-qubit gate realized by capacitive coupling of two superconducting charge qubits in a transmission line resonator (the so-called ‘transmons’). We demonstrate that the sensitivity of the optimized gate to 1/f flux and critical current noise is suppressed to leading order. The procedure only requires a preliminary estimate of the 1/f noise amplitudes. No additional control or bias line beyond those used for the manipulation of individual qubits is needed. The proposed optimization is effective also in the presence of relaxation processes and of spontaneous emission through the resonator (Purcell effect).