A boy and his robot companion live in a large walled city. All the people in the city are adults and he is the only boy. Everyone knows and likes the boy as he is the only young person. The boy discovers that there are places outside the city like perhaps another forbidden city. With the help of his robot, he finds an abandoned railway and makes his way to the other city. I think I read this story about 1960.
The story follows Alvin, the last child born to a race of immortal humans (check). He lives in the walled city of Diaspar (check). He finds an abandoned railway-like transportation system from a time when humanity occupied more than one city (check). He uses it to travel to another city, Lys, which is also occupied by humans (check). The only real disparity with your description is the robot companion, which Alvin doesn't pick up until after he arrives in Lys, and which isn't involved in his finding the railway.