In this paper, a new and comprehensive multigeneration system is developed exploiting biogas fuel. The system is examined from the perspective of energy, exergy, exergoeconomic, exergoenvironment, and safety for supplying the energy demands of large buildings such as modern hospitals located near industrial places. Multi-objective optimization is applied along with a new concept of flexibility in the system considering these five indicators. The Brayton cycle and closed Brayton cycle for power generation, dual-loop organic Rankine cycle and ejector refrigeration for power and cooling production, multi-effect distillation unit for freshwater production, carbon dioxide absorption unit by microalgae for biodiesel production, water heaters for domestic heating production, and PEM electrolyzer for hydrogen production are integrated into the devised system. From the environmental point of view, using biogas fuel and absorption of carbon dioxide leads to the best environmental efficiency in the proposed system. Moreover, the safety facet is analyzed to lower the level of damage and risk in the system. Based on the results of the optimization, the net output power, air conditioning refrigeration, freezing, and heating are reckoned at 1864, 286.6, 75.89, and 398.9 kW, and the production of freshwater, hydrogen, biodiesel, and glycerol is predicted to be 2.111, 0.00038, 0.0088, and 0.00091 kg/s, respectively. Also, the system performance ratio, unit cost of product, exergy efficiency, environmental effect of product, and overall system risk are equal to 1.828, 40.58 $/GJ, 31.11%, 6232 mPts/GJ, and 1656 $/year.