In rural territories, the communities use energy sources based on fossil fuels to supply themselves with electricity, which may address two main problems: greenhouse gas emissions and high fuel prices. Hence, there is an opportunity to include renewable resources in the energy mix. This paper develops an optimization model to determine the optimal sizing, the total annual investment cost in renewable generation, and other operating costs of the components of a hybrid microgrid. By running a k-means clustering algorithm on a meteorological dataset of the community under study, the hourly representative values become input parameters in the proposed optimization model. The method for the optimal design of hybrid microgrid is analyzed in six operating scenarios considering: ① 24-hour continuous power supply; ② load shedding percentage; ③ diesel power generator (genset) curtailment; ④ the worst meteorological conditions; ⑤ the use of renewable energy sources including battery energy storage systems (BESSs); and ⑥ the use of genset. A mathematical programming language (AMPL) tool is used to find solutions of the proposed optimization model. Results show that the total costs of microgrid in the scenarios that cover 100% of the load demand (without considering the scenario with 100% renewables) increase by over 16% compared with the scenario with genset operation limitation. For the designs with power supply restrictions, the total cost of microgrid in the scenario with load shedding is reduced by over 27% compared with that without load shedding.