This is actually a very difficult question. Besides being able to last for years without deteriorating or rotting, the food must be available in sufficient quantity to sustain the crew for that duration, and be palatable enough for the astronauts to choke down when they’re hungry.
Michele Perchonok from NASA Johnson Space Center and her colleagues have written a report detailing what kinds of foods a mission to Mars might take. Currently, each astronaut is provided with 3000 calories weighing 1.8 kg (food plus packaging) per day. That food is mostly ‘thermostabilized’ (heated to kill germs before packaging) or freeze-dried. Food scientists are continually fiddling with fat content and moisture levels in foods order to lower the weight of those items while preserving the number of calories. After all, any decrease in cargo weight also affects the weight of the fuel needed to lift that payload and lowers the cost of the whole mission.
Shelf life is another issue. Most foods that you see on supermarket shelves are not expected to last longer than two years (though some do). For a deep space mission, food might need to remain edible for at least five years without refrigeration. Therefore, NASA has been conducting shelf-life studies on a variety of foods. Of the 13 thermostabilized products they left out to rot, meat products fared the best and eggs the worst. They are also looking into the possibility of storing food outside the space craft, and using the cold vacuum of space to preserve the food.
Finally, the foods must be of sufficient variety and provide necessary nutrition for long missions, neither of which is easily accomplished.
I recently read Mary Roach’s book Packing for Mars: The Curious Science of Life in the Void, and it contains what is almost certainly more than you’d want to know about life in space. One of the sections deals with exactly this topic, how to stock a spacecraft’s pantry. One tip: use bite sized morsels. Any crumbs you create during your meal will float around you for the duration of your trip.
Astronauts on the International Space Station.