A biogas upgrading method based upon PTSA and gas separation membranes, comprising the steps of: compressing a stream of biogas with a main compressor; feeding, to a PTSA unit, a PTSA feed gas stream withdrawn from an outlet of the main compressor; removing H 2 S from the PTSA feed gas stream with the PTSA unit, the PTSA unit comprising two or more adsorbent beds each of which is selective for water, VOCs, and H 2 S over CO 2 and for H 2 S over methane, each of said beds being subjected to a PTSA cycle comprising the phases of: adsorption of water, VOCs, and H 2 S from the PTSA feed gas stream; depressurization; thermal regeneration using a regeneration gas stream in which adsorbed water, VOCs, and H2S are desorbed; cool down using a cool down gas stream; and repressurization; withdrawing an H 2 S-depleted PTSA product stream from the PTSA unit; feeding the PTSA product stream to a first gas separation membrane stage comprising one or more gas separation membranes selective for CO 2 and O 2 over methane; withdrawing, from the first gas separation membrane stage, a first stage permeate stream enriched in CO 2 and O 2 and deficient in methane compared to the PTSA product stream and a first stage retentate stream deficient in CO 2 and O 2 and enriched in methane compared to the PTSA product stream; feeding the first stage retentate stream to a second gas separation membrane stage comprising one or more gas separation membranes selective for CO 2 and O 2 over methane; and withdrawing, from the second gas separation membrane stage, a second stage permeate stream enriched in CO 2 and deficient in methane compared to the first stage retentate stream and a second stage retentate stream deficient in CO 2 and enriched in methane compared to the first stage retentate stream, wherein the second stage retentate stream is a product natural gas stream, wherein the repressurization of the beds is performed with one or more of the PTSA feed gas stream, the PTSA product gas stream, the first stage retentate stream, and the second stage retentate stream. and wherein some or all of the second stage permeate stream is the cool down gas stream and the cool down gas stream is received from the PTSA unit by a suction inlet of the main compressor where it is combined with the compressed biogas stream. compressing a stream of biogas with a main compressor; feeding, to a PTSA unit, a PTSA feed gas stream withdrawn from an outlet of the main compressor; removing H 2 S from the PTSA feed gas stream with the PTSA unit, the PTSA unit comprising two or more adsorbent beds each of which is selective for water, VOCs, and H 2 S over CO 2 and for H 2 S over methane, each of said beds being subjected to a PTSA cycle comprising the phases of: adsorption of water, VOCs, and H 2 S from the PTSA feed gas stream; depressurization; thermal regeneration using a regeneration gas stream in which adsorbed water, VOCs, and H2S are desorbed; cool down using a cool down gas stream; and repressurization; withdrawing an H 2 S-depleted PTSA product stream from the PTSA unit; feeding the PTSA product stream to a first gas separation membrane stage comprising one or more gas separation membranes selective for CO 2 and O 2 over methane; withdrawing, from the first gas separation membrane stage, a first stage permeate stream enriched in CO 2 and O 2 and deficient in methane compared to the PTSA product stream and a first stage retentate stream deficient in CO 2 and O 2 and enriched in methane compared to the PTSA product stream; feeding the first stage retentate stream to a second gas separation membrane stage comprising one or more gas separation membranes selective for CO 2 and O 2 over methane; and withdrawing, from the second gas separation membrane stage, a second stage permeate stream enriched in CO 2 and deficient in methane compared to the first stage retentate stream and a second stage retentate stream deficient in CO 2 and enriched in methane compared to the first stage retentate stream, wherein the second stage retentate stream is a product natural gas stream, wherein the repressurization of the beds is performed with one or more of the PTSA feed gas stream, the PTSA product gas stream, the first stage retentate stream, and the second stage retentate stream. and wherein some or all of the second stage permeate stream is the cool down gas stream and the cool down gas stream is received from the PTSA unit by a suction inlet of the main compressor where it is combined with the compressed biogas stream.