The Andover and Wilmington Railroad was incorporated March 15, 1833, to build a branch from the Boston and Lowell Railroad at Wilmington, Massachusetts, north to Andover, Massachusetts. The line opened to Andover on August 8, 1836. The name was changed to the Andover and Haverhill Railroad on April 18, 1837, reflecting plans to build further to Haverhill, Massachusetts (opened later that year), and yet further to Portland, Maine, with the renaming to the Boston and Portland Railroad on April 3, 1839, opening to the New Hampshire state line in 1840.
The Boston and Maine Railroad was chartered in New Hampshire on June 27, 1835, and the Maine, New Hampshire and Massachusetts Railroad was incorporated March 12, 1839, in Maine, both companies continuing the proposed line to South Berwick, Maine. The railroad opened in 1840 to Exeter, New Hampshire, and on January 1, 1842, the two companies merged with the Boston and Portland to form a new Boston and Maine Railroad.
On February 23, 1843, the B&M opened to Agamenticus, on the line of the Portland, Saco and Portsmouth Railroad in South Berwick. On January 28 of that year, the B&M and Eastern Railroad came to an agreement to both lease the PS&P as a joint line to Portland.
The Boston and Maine Railroad Extension was incorporated March 16, 1844, due to a dispute with the Boston and Lowell Railroad over trackage rights rates between Wilmington and Boston. That company was merged into the main B&M on March 19, 1845, and opened July 1, leading to the abandonment of the old connection to the B&L (later reused by the B&L for their Wildcat Branch). In 1848 another original section was abandoned, as a new alignment was built from Wilmington north to North Andover, Massachusetts, in order to better serve Lawrence, Massachusetts.
A new alignment to Portland opened in 1873, splitting from the old route at South Berwick, Maine. The old route was later abandoned. This completed the B&M "main line" which would become known as the Western Route to distinguish it from the Eastern Route (described below) which also connected Boston and Portland.