At this time, José was reunited with his childhood friend, Bernardo O'Higgins who had been forced to flee Chile following the defeat of the Battle of Rancagua. O'Higgins had with him thousands of men and the desire to go back to Chile, oust the royalists, and regain Chile's autonomous position. For the next three years, José developed strategy, laid his plans, recruited and trained an army composed of Chileans, Argentines, gauchos, mestizos, and slaves. His skills at strategy, his leadership, and his vision for a united force against Spain would earn him the title of Knight of the Andes.
At his own request, he was named governor of Cuyo, at the foot of the Andes in western Argentina. Men in government in Buenos Aires feared his growing army and popularity and remove him as governor. Provincial protests and a refusal to accept his successor got José de San Martín reinstated. He was then instrumental in getting representatives from all the provinces to meet in Tucuman in 1816, where they formed the Congreso de Tucuman. During the ensuing discussions, José de San Martín stressed the need for outright independence from Spain and the need for a liberal-constitutional monarchy. The Congress agreed and passed the declaration of independence in July of 1816.
His daughter Mercedes was born that year. José was offered a gift of land in her honor, but he declined, saying portions of it should be reserved for men who distinguished themselves in the battle for independence. His every action was focused on independence.
Next, José turned his attention to his longtime plans for Peru. Crossing the Andes was still necessary, but he now decided that he would cross into Chile, then go by sea to Peru. Along the way, he and Bernardo O'Higgins would retake Chile from the royalists.
In January 1817, after sending his wife and daughter to live with his in-laws in Buenos Aires, San Martin and O'Higgins led their combined armies over a pass nearly 15,000 feet (4,600 meters) high in the Andes Mountains. The march over the Andes has been compared to Hannibal's crossing the Alps. They met and defeated the royalists on February 12 at Casas de Chacabuco and took Santiago.
Following another victory at Maipo in north-central Chile in April 1818 which led to Chile's independence, José was offered the supreme dictatorship of Chile. He refused the honor, but did accept the title of Generalisimo of the Ejército Unido de los Andes y de Chile, the United Army of the Andes and Chile, and proceeded with his original plans. He initiated discussions with the royalists in Peru, suggesting they break away from Spain and form an independent monarchy. When these negotiations came to nothing, José de San Martín gathered his forces.
Bernardo O'Higgins with the help of Thomas Cochrane had created the Chilean navy and the accumulation of troop ships. Under San Martín's leadership, the Chilean navy sailed north from Valparaiso.
Cochrane had failed in an earlier attempt to take the port of Callao, Lima's access to the Pacific, and instead landed in Pisco, where José de San Martín and his army could take the overland route to Lima.

