Indonesia's railway history began in the 19th century, when the first railway line on the city of Semarang was built. The construction of the railway line in Semarang was a personal request from King William I. It was intended for military usage, as well for the transportation of agricultural products within the region into warehouses in Semarang.

Currently, the majority of railway lines in Indonesia were constructed and inherited from the Dutch colonial rule. Following Indonesia's Independence in 1945, many lines fell into disuse or abandoned, although some were reactivated in the following decades with more being planned to be reactivated in the foreseeable future.

The current national rail operator, PT Kereta Api Indonesia (Persero), was founded on 28 September 1945, absorbing all of the previously established companies from the colonial era.

Pre-independence era

First railway line

The idea of building a railway in modern day Indonesia has emerged since the 1840s. Indonesia (then known as the Dutch East Indies) was the second country in Asia to establish a rail transport, after India. On 7 June 1864, Governor General Baron Sloet van den Beele initiated the first railway line in Java on Kemijen village, Semarang, Central Java. It began operations on 10 August 1867 in Central Java and connected the first built Semarang station to for 25 kilometers. By 21 May 1873, the line had connected to Solo, both in Central Java and was later extended to Yogyakarta. This line was operated by a private company, Nederlandsch-Indische Spoorweg Maatschappij (NIS or NISM) and used the gauge. Later construction by both private and state railway companies used the gauge.

The liberal Dutch government of the era was then reluctant to build its own railway, preferring to give a free rein to private enterprise. But private railways could not provide the expected return of investment (even NIS required some financial assistance from the government), and the Dutch Ministry of Colonies finally approved a state railway system, the Staatsspoorwegen (SS), extending from Buitenzorg (now Bogor) in the west, to Surabaya in the east. Construction began from both ends, the first line (from Surabaya) being opened on 16 May 1878, and both cities were connected by 1894.

By the 1920s the system in Java had reached its greatest extent, with most towns and cities connected by rail, with branches and tramways connecting sugar plantations to factories.

The Great Depression of the 1930s put laid to plans of constructing railway lines in Borneo, Celebes, connecting the lines in Sumatra and electrification of the lines in Java.

After the Dutch state started railway construction, private enterprises did not completely get out of the picture, and at least 15 light railway companies operated in Java. These companies operated as "steam tram companies", but despite the name, were better described as regional secondary lines.

Java

As befits a colonial enterprise, most railway lines in Indonesia had a dual purpose: economic and strategic. In fact, a condition for the financial assistance for the NIS was that the company build a railway line to Ambarawa, which connected to the one of an important military base named Fort Willem I for the Dutch king. The first state railway line was built through the mountains on the southern part of Java, instead of the flat regions on the north, for a similar strategic reason. The state railway in Java connected Anyer on the western coast of the island, to Banyuwangi on the eastern coast.

Only in the 1920s did one section, the Jakarta-Bogor line, be electrified with Dutch assistance, it would be the first ever electric rail system in Southeast Asia and the first electric commuter train service there as well. It was also the first in Southeast Asia to be operated by electric multiple units that would run for 4 straight decades, which would be defined with their European manufacture and their latter green-white livery schemes across these sets that would be adopted by the latter diesel locomotive fleet of its successors.

Sumatra

In Sumatra, railways were first used for military purposes, with a railway line connecting Banda Aceh and its port of Uleelhee in 1876. This railway, the Atjeh Staats Spoorwegen (ASS), first built to a gauge which was later regauged to and extended south. This line was only transferred to the Ministry of Colonies from the Ministry of War on 1 January 1916, following the relative pacification of Aceh.

The Western Sumatra's state railway in the Minangkabau area, the Staatsspoorwegen ter Sumatra's Westkust (SSS) transported coal from inland mines to the port at Padang and was built between 1891 and 1894.

The Southern Sumatra's state railway, the Staatsspoorwegen op Zuid-Sumatra (ZSS), was completed in the 1930s. It served a fertile plantation area and an important coal mine.

Another important private railway line was the Deli Spoorweg Maatschappij (DSM). This line served regions producing rubber and tobacco in Deli.

Sulawesi Between July 1922 and 1930, a -long railway line operated in South Sulawesi. This line was to be extended to North Sulawesi, as part of a massive project of railway construction in Borneo and Sulawesi, connection of separate railway systems in Sumatra and electrification of the main lines in Java. The Great Depression of 1929 put paid to these plans.

Japanese occupation During the Japanese occupation between 1942 and 1945, the different railway lines in Java were managed as one entity. The Sumatra systems, being under the administration of a different branch of the Japanese armed forces, remained separate.

The occupiers also converted the lines in Java into , thereby resolving the dual gauge issue. This was not an actual "problem" as there was not much transfer of materials between the systems, and much of the system had been fitted with a third rail by 1940, creating a mixed-gauge railway. Many locomotives were seized and transported to Malaya, Burma and elsewhere. The railway network was reduced from in 1939 to in 1950 to provide material for railway construction in Burma.

Independence era During the war for independence between 1945 and 1949, freedom fighters took over the railways, creating the first direct predecessor to today's PT Kereta Api, the Djawatan Kereta Api Repoeblik Indonesia (Railways Service of the Republic of Indonesia), which was officially created on 28 September 1945. This date, not the 1867 one, is regarded as the birth date of Indonesian railways and commemorated as Railway Day every year, on political grounds.

On the other hand, the Dutch by Netherlands Indies Civil Administration (NICA) formed their own combined railway system to manage the railway lines located on their occupied territory, the Staatsspoorwegen/Verenigd Spoorwegbedrijf (SS/VS or Combined Railways). This company consisted of a combined state-owned and many private-owned railway companies, except Deli Spoorweg Maatschappij (Deli Railway). By the time of Dutch recognition of Indonesian independence, the SS/VS had most railway lines under their management, though not all were in operation.

With Indonesia's full independence in 1949, the separate rail systems were fused to become part of DKARI's rail network (excepting the Deli Railway), which welcomed its first diesel locomotives in 1952, which slowly displaced the steam locomotives leading to their final removal from the network in the early 80s. Non-state railway systems in Java retained their paper existence until 1958, when all railway lines in Indonesia were nationalized, including the Deli Railway, under the brand Djawatan Kereta Api (DKA) adopted early in the decade. This resulted in the formation of the Perusahaan Negara Kereta Api (PNKA: National Railway Corporation) in September 1963 on the basis of DKARI, becoming thus a state-owned enterprise with a corporate structure similar to British Rail and its neighbour's KTMB.1963 Indonesian Government's Regulation No. 22 On 15 September 1971 the name of PNKA was changed to Perusahaan Jawatan Kereta Api (PJKA, the Indonesian Railway Service Corporation).1971 Indonesian Government's Regulation No. 61

The brief transition era from PNKA to PJKA was the time when the Greater Jakarta rail network, on orders by President Sukarno, had its electrical lines in Central Jakarta converted to steam and diesel which led to the electric trackage to be split, only for these lines be reinstated in the 1970s with the original electric locomotives remaining, which, together with their hauled stock, were replaced by a new generation of electric multiple units imported from Japan, themselves the replacements of the originals which were converted to DMUs or passenger cars on loco-hauled services in the 60s. In addition, the PNKA gained the first ever passenger overnight trains in independent Indonesia - the Bintang Fadjar, launched in 1961-62 and was replaced by the long running Bima in 1967, running the Jakarta-Surabaya route via Yogyakarta (not covered by the former, which stopped at Semarang Tawang station instead) and Surakarta, and the first true diesel multiple units, the Kuda Putih imported from East Germany, which until the early 1980s became the primary lifeline of the southern main line between Yogyakarta and Surakarta, evolving into today's two KAI Commuter lines, the diesel Prambanan Express Commuter Line and the electric KAI Commuter Yogyakarta Line, which opened in 2022. The Bima, with its East German made passenger coach and dining cars and sleeper cars, also made history as one of the first to adopt air conditioning. PNKA, however, converted two Jakarta EMU motor passenger-cab combine cars into a diesel railbus called the Railcar Perintis (one each in Jakarta and Yogyakarta) during the 1960s, one of many attempts to develop a national rail production industry.

1982 would see Industri Kereta Api - founded the year before - finally present its completed first passenger and freight car orders to the PJKA system, which began an era of Indonesian-made passenger and freight cars dominating the railway network, which continues to this day.

Later then, on 2 January 1991, PJKA was changed its name and status becoming the Perusahaan Umum Kereta Api (Perumka, the Indonesian Railways Public Corporation LLC),1990 Indonesian Government's Regulation No. 57 and since 1 June 1999, this company was changed to a joint-stock company, named PT Kereta Api (Persero) (PT KA).1998 Indonesian Government's Regulation No. 19, 1999 Presidential Decree No. 39, and Imas Fatimah's Notarial Document The Greater Jakarta commuter network was operationally separated from PT KA in 2008 by the formation of a dedicated subsidiary under the company to ensure its continued revenue operations, with PT KA itself limiting its Jakarta passenger operations to long distance and express train services. In May 2010, the company was rebranded again into PT Kereta Api Indonesia (Persero) (PT KAI, Indonesian Railways Company Ltd JSC), the name used till the present.2010 The Indonesian Railways Directors' Decree No. 16/OT.203/KA 2010

The headquarters of the state railway system, since Dutch colonial days, had been located in Bandung, West Java. Before the nationalization of the private railway networks in 1958, these private sector railway companies were headquartered elsewhere in the country, in Semarang, Tegal, Surabaya and Medan.