You are using the appropriate command.
$length applied to a LineString feature which geometry has only two vertices, will measure the shortest curved distance between the vertices, along the ellipsoid.
You can define the reference ellipsoid and the units of measurements for each project, in the Project ~> Properties menu, General tab, Measurements section.
If you apply the same
$length expression to a LineString feature which geometry has more than two vertices, the returned value will be the addition of each shortest curved distance along the reference ellipsoid between each vertex.
$length returns the length of geodesic lines. (An ellipsoid of revolution has just one great circle: the equator. All other intersections between planes that contain the center of the ellipsoid and its surface, define ellipses.)
When you project vector data (i.e., coordinates of vertices) to a plane, there is no way to preserve the value of geodesic lengths between all points on a map measuring planimetric lengths in the plane of projection.
You can preserve the geodesic length along one line (maybe two), or between all points to one in particular, or between two (maybe three) points. But not between all points on a map with each other.
If you want to measure planimetric lengths in the plane of projection, you can use
length($geometry) expression. The units of measurements will be those defined in the CRS of the layer. The difference between that measured planimetric length and the ellipsoidal length may be greater or lesser depending on the projection used and the location of the vertices.
Also, you can select
None / Planimetric instead of an ellipsoid of reference, and
$length will return planimetric measurements in the CRS of the project.
There is no need for the data to be projected on a particular reference system, or for the map to be reprojected on a particular system: When you use
$length expression with a defined reference ellipsoid, the coordinates of the vertices are inversely projected to the reference ellipsoid and the expression returns the ellipsoidal distances between them.