Agriculture is the backbone of a country and plays a vital role in shaping its economic performance. Factors such as natural disasters, extreme weather changes, pests, and soil quality significantly impact productivity, often leading to economic losses. Accurate predictions in agricultural practices, particularly crop recommendations, can substantially boost productivity and resource management. This research aims to develop a robust crop recommendation system using ensemble learning (EL), which integrates multiple machine learning (ML) models for improved performance. This study utilizes two datasets: a real-time dataset available on Kaggle, collected using IoT sensors, and a synthetic dataset generated using CTGAN. These datasets provide crop recommendations for 22 different crops, based on key features like nitrogen, phosphorus, potassium, soil pH, humidity, and rainfall. The performance of various ML models—such as linear regression (LR), support vector machine (SVM), decision tree (DT), naïve Bayes (NB), K-nearest neighbor (KNN), random forest (RF), extra tree classifier, XGBoost, and gradient boost—is compared with that of EL models, including voting, bagging, boosting, and stacking ensemble techniques. The stacking ensemble model achieved the highest accuracy at 99.36% across all ensemble techniques. By further optimizing this model using the Optuna hyper-parameter tuning technique, the accuracy was improved to 99.43%.