This is an interesting question with several mutually possible ways of answering.
Although some elements of the Law appear to have already been in place with the patriarchs Abraham, Isaac and Jacob, the 10 Commandments and the detailed Law were given only from the time in the wilderness onwards. The following are explanations that the Bible offers:
1. So that the Jews would be a light to the Gentile nations, a witness to the LORD
“See, I have taught you statutes and rules, as the Lord my God commanded me, that you should do them in the land that you are entering to take possession of it. Keep them and do them, for that will be your wisdom and your understanding in the sight of the peoples, who, when they hear all these statutes, will say, ‘Surely this great nation is a wise and understanding people.’ For what great nation is there that has a god so near to it as the Lord our God is to us, whenever we call upon him? And what great nation is there, that has statutes and rules so righteous as all this law that I set before you today?” (Deuteronomy 4:5-8)
2. As a foreshadow of Christ
These three passages explain the Law as a foreshadow of Christ:
16 Therefore let no one pass judgment on you in questions of food and drink, or with regard to a festival or a new moon or a Sabbath. 17 These are a shadow of the things to come, but the substance belongs to Christ. (Colossians 2:16–17)
This is echoed in Paul’s warning against Greeks adopting the Law
8 Formerly, when you did not know God, you were enslaved to those that by nature are not gods. 9 But now that you have come to know God, or rather to be known by God, how can you turn back again to the weak and worthless elementary principles of the world, whose slaves you want to be once more? 10 You observe days and months and seasons and years! (Galatians 4:8–10)
And then in Hebrews
10 For since the law has but a shadow of the good things to come instead of the true form of these realities, it can never, by the same sacrifices that are continually offered every year, make perfect those who draw near. 2 Otherwise, would they not have ceased to be offered, since the worshipers, having once been cleansed, would no longer have any consciousness of sins? 3 But in these sacrifices there is a reminder of sins every year. 4 For ait is impossible for the blood of bulls and goats to take away sins (Hebrews 10:1–10)
3. To convict men of sin and demonstrate the futility of law
This is the argument that Paul provides to the Galatians describing his own Jewish people as under a schoolmaster (KJV) or more correctly a slave tutor, until adulthood came in Christ.
19 Why then the law? It was added because of transgressions, until the offspring should come to whom the promise had been made, and it was put in place through angels by an intermediary. 20 Now an intermediary implies more than one, but God is one. 21 Is the law then contrary to the promises of God? Certainly not! For if a law had been given that could give life, then righteousness would indeed be by the law. 22 But the Scripture imprisoned everything under sin, so that the promise by faith in Jesus Christ might be given to those who believe. 23 Now before faith came, we were held captive under the law, imprisoned until the coming faith would be revealed. 24 So then, the law was our guardian until Christ came, in order that we might be justified by faith. 25 But now that faith has come, we are no longer under a guardian, (Galatians 3:19-24)
The ‘we’ here means Jews, since Greeks were never under this guardian tutor.
And there will be other reasons too.