Agustín Gamarra, named president by Congress, was anxious to once again attempt to unite Peru with Bolivia, however this time under Peruvian governorship rather than by confederation. He invaded Bolivia in 1841 with the Battle of Ingavi commencing a war between the two countries. In the process he died during the Battle of Ingavi. At his death, a state of discord ran through Peru, with various military caudillos claiming themselves president.
April 20, 1845, Ramon Castilla assumed the presidency. His government represented one of the greatest events in the republican history of the nation, for the reason that Peru entered a stage of internal and external peace, progress, power, and international prestige. From 1845 to 1862, Castilla exercised influence over the daily life of his citizens. His first government ended on 20 April 1851 allowing the general Jose Rufino Echenique assume power. Echenique a prestigious military officer, had been one of the top advisors of Castilla's government. Using his extensive military influence, Echenique continued on the progress of Castilla's government to further advance the social and economic aspirations of Peru. Echenique's government, although emphasizing a policy of progress, was made up of mainly political conservatives, and suffered much opposition from liberals who eventually rebelled against his government. Ramon Castilla eventually became involved in the rebellion, soon becoming its leader. In the battle of La Palma, in the outskirts of Lima, Echenqiue was defeated and Ramon Castilla assumed the presidency for a second time. The reinstating of Castilla as president brought institutional stability; management of the national economy by establishing fixed budgets; economic prestige abroad by the repayment of foreign debt; internal security; international position on the continent; intellectual and material progress; as well as the initiation of the development of the Amazonia.
The economic aspect of the era was expressed by the management of state finances through the implementation of budgets. During this time the economy was experiencing a boom due to guano being sold to European markets. This allowed the government to repay its external debt, earning it international economic prestige. Before Castilla, state expenses were calculated in a disorderly fashion: Castilla implemented the budget system as well as organized the sale of guano by the system of appropriations. Using the guano money, he was able to settle international debt with various nations which had existed since the time of independence.
Communications to the interior began to improve with the construction of new highways and the implementation of railroads. The first railroad that was constructed was during Castilla's first term between Lima and Callao. During Echenique's government, the Tacna-Arica railroad was constructed and in Castilla's second term, the Lima-Chorrillos railroad was implemented.
Two important provisions were made during the liberal revolution against the government of General Echenique, with the aim of making all Peruvians equal before the law. One of these such provisions was the abolition of the tribute which the indigenous population was forced to pay only for the reason of their ethnicity. Another, was the abolition of the slavery of the small population of blacks in Peru. To compensate for the lack of workers on the haciendas of the coast, the government of Castilla in 1849 authorized the import of Chinese people to devote to agricultural work. It thus opened the door to Chinese immigration that more diversified the races of the nation. During this time, European immigration was also encouraged, and thousands of Europeans sailed from Europe to the ports of Trujillo, Huacho, Callao, Ilo, and Arica. Most of them settled along the coast or slightly inland. Not many settled into the interior. An exception to this would be a German population which arrived at the port of Huacho and traveled inland to colonize the Pozuzo and Oxapampa regions.
Castilla in his first government was concerned about bettering the education of the nation. He created the First Regulation of Public Education. During his second government, he created a second regulation defining the levels of Primary, Middle, and superior education.
The task of replacing Spanish legislation laws for another, appropriate to modern republican times, had been almost forgotten. The country was governed by laws which were established by the King of Spain as they were old laws, functioning since before independence. Castilla took the first step in reforming the Judicial System. Partly finished, the first of these laws, named the "Civil Codes" were put into place in 1852 and lasted until 1936. During this time period, they were accompanied by the "Prosecution Codes" and the "Commerce Codes".
At this time, Peru was viewed from the exterior as a non-aggressive country. Its international policy was based on the ideas of Continentalism and solidarity between countries of the Western Hemisphere. With this objective, in 1848 the First American Congress met in Lima with the goal of to ensure the bases of future peace and security for the nations of South America. During the government of Echenique, Peru began to fix its international borders with neighboring countries. The first treaty convienied in this time period was that with Brazil, signed in 1851, establishing borders between the two nations.
Ramon Castilla clearly realized the deficiencies of the Armed Forces and sought to fix them. Because Peru was geographically situated in the central part of western South America, with an immense coast (at that time extending from Machala to the Loa River) and with borders with four nations (currently with five), there was a necessity for self-defense.
Castilla sought to expand and well-equip the Peruvian Navy. His naval policy was that if Chile constructed one ship, he should construct two, and he went on to turn the Peruvian Navy into to the most powerful of South America. He acquired the frigates "Mercedes", "Guisse", "Gamarra", "Amazonas", and "Apurimac" as well as the Schooners "Tumbes" and "Loa". He also built the naval ports of Paita and Bellavista. Hew also acquired the first steam-powered warship of any South American country and named it the "Rimac". To better educate the officers of these new ships, he sent them to study with the various navies of European nations. For the defense of the Amazonia, Castilla began to develop an Amazonian fleet with the purchase of the ships "Morona", "Pastaza", "Napo", and "Putumayo", which were based in the port of Iquitos.
Also during this time was the invasion of Ecuador which caused the Ecuadorian-Peruvian War of 1858-1860 in which Peru emerged victorious.
The 1870s was for Peru's economy "a decade of crisis and change". Nitrate extraction rose while guano extraction declined and sugar cane dethroned cotton as the main cash crop. Guano exports dropped from 575,000 tons in 1869 to less than 350,000 tons in 1873 and the Chincha Islands and other guano islands were depleted or close to be so. Deposits elsewhere were of poor quality.
When in 1873 Peru imposed an estanco, a sales monopoly of nitrate, most larger nitrate firms opposed it. As the economic situation deteriorated and Peru held large overseas debts, the estanco was later replaced by a full state monopoly on production and exports. To uphold the monopoly, Peru bought in 1876 the nitrate concessions Henry Meiggs had in Bolivia. Chile was not considered a serious competitor due to the high costs of its nitrate deposits, which were too far from ports. However, The Antofagasta Nitrate and Railway Company, controlled by Chilean Agustín Edwards and operating in Bolivia, was a serious competitor.