The move is largely symbolic, however, as France's oil and gas production represents just 1 percent of national consumption - the rest is imported.
Current drilling permits will not be renewed, according to the bill formally presented in a Cabinet meeting Wednesday. France currently has 63 oil and gas drilling projects on its territory.
The ban, which the government claims is a world first, is part of a larger plan to wean the country's economy from fossil fuels, encourage clean energy and fulfill France's commitments under the Paris Climate Agreement to curb global warming.
French President Emmanuel Macron praised it as "an important step to fulfill our international commitments."
The bill, which was described by Environment Minister Nicolas Hulot, also includes a definitive ban on all shale gas exploration and extraction. Until now, only hydraulic fracturing, a process known as fracking, was banned. All other potential methods are now to be prohibited as well.
Hulot had announced in July that France will stop producing power from coal - now 5 percent of its total output - by 2022.
France also wants to reduce the proportion of the power it gets from nuclear energy to 50 percent by 2025 from the current 75 percent.
Hulot said it's important for France to maintain its leadership on climate-related issues following the Paris Agreement signed in 2015.
"I have absolute faith in the capacity of renewable energies to become progressively a very important source to fulfill our energy needs," he said in a news conference.
Greenpeace France said the bill sets a "good goal" but doesn't go far enough. The environmental organization notes that the extension of an offshore exploration permit in French Guiana was maintained. In this case, Hulot said existing permits "will be maintained to avoid legal conflicts."
Francis Duseux, president of the French oil lobby UFIP, said it would have been better to keep producing oil and gas in France rather than rely exclusively on imports, because he says France's economy will still rely on fossil fuels in 2040.
"Energy transition will take time," he said on Europe 1 radio.
Duseux estimated that oil drilling on French territory supports 4,500 to 5,000 jobs.
Vermilion Energy France is the main oil producer in France. Jean-Pascal Simard, director of public relations for the group, told The Associated Press he thinks the 2040 deadline is too soon.
"We would like to have a longer period of time to properly manage, properly get the return on investment that we have been investing here in France," he said.
Simard also said producing oil in France would be better from the environmental point of view.
"By not producing it here, we will have to increase the oil coming from countries like Saudi Arabia or even South America," he said.
Copyright 2018 The Associated Press. All rights reserved. This material may not be published, broadcast, rewritten or redistributed.