The effect of a pressure gradient on the first mode of instability of compressible subsonic and supersonic boundary layers is investigated using linear stability theory. A pressure gradient is studied that generates potential-flow Mach number distributions at the edge of the boundary layer of the form Me=cxn, where c is a constant and x is the dimensionless streamwise distance. Variations are calculated for the maximum growth rates of three-dimensional first-mode waves with different edge Mach numbers and different levels of both adverse and favorable pressure gradients. A favorable pressure gradient is shown to have a stabilizing effect on first-mode waves. However, at high edge Mach numbers, a favorable pressure gradient becomes less effective in stabilizing first-mode waves. The frequencies and streamwise and spanwise wave numbers that correspond to the maximum growth rates of first-mode waves decrease as the pressure gradient becomes more favorable at all Mach numbers when the Reynolds number R=1500 and at Me≥2 when R=600. Setting the Prandtl number to unity significantly increases the maximum growth rates of first- and second-mode waves at high Mach numbers compared with setting it to the realistic value for air of 0.72. Predicted transition in flow over a flat plate using the N-factor criterion is found to be due to first-mode waves up to free-stream Mach numbers of 6–6.5.