The ocean is the main source of thermal inertia in the climate system1. During recent decades, ocean heat uptake has been quantified by using hydrographic temperature measurements and data from the Argo float program, which expanded its coverage after 20072,3. However, these estimates all use the same imperfect ocean dataset and share additional uncertainties resulting from sparse coverage, especially before 20074,5. Here we provide an independent estimate by using measurements of atmospheric oxygen (O2) and carbon dioxide (CO2)-levels of which increase as the ocean warms and releases gases-as a whole-ocean thermometer. We show that the ocean gained 1.33 ± 0.20 × 1022 joules of heat per year between 1991 and 2016, equivalent to a planetary energy imbalance of 0.83 ± 0.11 watts per square metre of Earth's surface. We also find that the ocean-warming effect that led to the outgassing of O2 and CO2 can be isolated from the direct effects of anthropogenic emissions and CO2 sinks. Our result-which relies on high-precision O2 measurements dating back to 19916-suggests that ocean warming is at the high end of previous estimates, with implications for policy-relevant measurements of the Earth response to climate change, such as climate sensitivity to greenhouse gases7 and the thermal component of sea-level rise8.