Current Earth-like numerical dynamo simulations are able to reproduce many characteristics of the observed geomagnetic field. One notable exception is the geomagnetic octupolar component. Here we investigate whether a stably stratified layer at the top of the core, a missing ingredient in standard dynamo simulations, can explain the observed geomagnetic octupole. Through numerical simulations, we find that the existence of a stable layer has significant influence on the octupolar-to-dipolar ratio of the magnetic field. Particularly, we find that a 60-km stable layer with relatively strong stability or a 130-km layer with relatively weak stability are compatible with the observations, but a 350-km stable layer, as suggested by recent seismological evidence, is not compatible with Earth's octupole field over the past 10,000 years. Plain Language Summary Current computer models of Earth's magnetic field generation do a good job of reproducing the observed features of the field except for one large-scale component of the field: the octupole. In this paper we investigate whether including a layer at the top of the core that is stable to convection could help explain the observed octupole component of the field. We add such a layer to computer models of the geodynamo and find that it has significant influence on the octupolar component of the magnetic field. Particularly, we find that a 60-km thick stable layer that is relatively strongly stratified or a 130-kmthick layer with relatively weak stratification are compatible with the observations, but a 350-km stable layer, as suggested by recent seismic evidence, is not compatible with Earth's octupole field over the past 10,000 years.