Minecraft wiki entry for Setting_up_a_server, associated text: Check the system requirements for CPU, RAM, and disk space.
Install Java. Use the OS-specific instructions below for this.
Download your server jar file from the download page.
Make a new folder for the jar file and move it there. This will be where all the configuration and the world files will be stored, so you don't want these to just sit in "Downloads".
Open a command prompt or a terminal interface. (On windows: search for cmd. On macOS: open terminal in launchpad."
Check again if Java is available. Type java -version.
Type cd (change directory), followed by the path to the folder where you placed your server jar file. You can drag the folder into the terminal window to get the path, if you have a GUI open.
Run the server for the first time by typing java -jar minecraft_server.jar --nogui (replacing the jar name (minecraft_server.jar in this case) by whatever you named the jar file to be).
A file called eula.txt will be generated after you run the server for the first time. Open it in a text editor and change eula=false to eula=true. It signifies that you have read and understood the end user license agreement that you'll follow when using the software. If you don't do this, the server will shut down immediately when you try to start it.
Now the server has been set up, and you can simply run it with java -jar minecraft_server.jar. If you don't want a GUI for typing commands, add a space and --nogui to the command. (Some people say it makes the server much much faster.) You can also use a few other switches described below.