Ocado has invested £17m in developing so-called "vertical" farms.
The firm, which delivers food bought online from Waitrose, is planning to grow herbs and leafy greens next to its distribution centres.
It has made two investments in indoor farming. One is a joint venture with 80 Acres, a US vertical farm business, and Priva, a Dutch firm that provides climate control technology.
It has also bought a stake in Jones Food in Scunthorpe.
Ocado's chief executive Tim Steiner said he wanted vegetables grown on the farms to be delivered to kitchens within an hour of being picked.
"We believe that our investments today in vertical farming will allow us to address fundamental consumer concerns on freshness and sustainability, and build on new technologies that will revolutionise the way customers access fresh produce," he said.
Ocado's distribution deal with Waitrose is due to come to an end in September 2020.
The firm will then start delivering to M&S customers as well as supplying its own-label products and big-name branded goods.
Vertical farming involves producing food indoors, with crops grown on a series of stacked levels in a controlled environment.
Produce from the indoor farms, which help save energy and conserve water, are expected to be delivered to M&S customers.
Jones Food, Europe's largest vertical farm, has more than 16,000 feet (5,000 metres) of indoor growing area, lit by 7.45 miles (12km) of LED lights, which means it can produce crops throughout the year.
It could grow more than 400 tons of food each year.
Meiny Prins, the boss of Priva, said the three-way joint venture with Ocado comes as the global population is growing and there is more of a focus on sustainability.
"We can develop optimal environments in which plants and food crops experience the best way to grow indoors, using leading-edge technology and solutions which result in substantially lower energy and water use, as well as reducing food waste," she said.