ProgramGuru

Set up and run Swift

Set up and run Swift

You need a Swift toolchain to compile a local program. A toolchain supplies the compiler and supporting tools. On a Mac, Xcode provides Swift and Apple-platform development tools; Swift.org also documents toolchains for supported platforms. Choose the installation instructions for your operating system instead of assuming every computer needs Xcode.

Confirm the installation with swift --version. Save the complete example as main.swift. Run swift main.swift for script execution, or compile with swiftc main.swift -o school and then run the resulting executable. Windows uses its platform-appropriate executable name. The compiler checks source before the executable runs.

What you’ll learn

  • Compile and run a complete Swift file.
  • Distinguish toolchain installation failures from source diagnostics.

Set up and run Swift in the example

  1. Install tools

    You need a Swift toolchain to compile a local program.

    Install tools
  2. Compile source

    Confirm the installation with <code>swift --version</code>.

    Compile source
  3. Run executable

    If the program prints the message, source execution is working.

    Run executable
If the program prints the message, source execution is working. A missing-command error concerns installation or the shell path. A syntax diagnostic refers to the source. Keeping those problems separate prevents you from changing correct Swift code to fix an environment problem.

Work through set up and run swift

Save this complete program as main.swift and run it with the Swift toolchain. No separate input file is needed.

Swift · complete program

print("Swift setup ready")

Expected output

Swift setup ready

Follow the execution

If the program prints the message, source execution is working. A missing-command error concerns installation or the shell path. A syntax diagnostic refers to the source. Keeping those problems separate prevents you from changing correct Swift code to fix an environment problem.

Try this program in the Swift compiler. Paste the complete source above.

Practice: Set up and run Swift

Your task: What should you check if swift is not found?

Swift · complete program

print("Swift setup ready")

Worked answer

Check the official installation steps and whether the toolchain command is on the shell path. Editing the printed message cannot make an absent compiler available.

Choose the environment for the task

Examples are checked in Swift 6 language mode. The single-file online compiler is useful for basic practice; package commands, local files and framework applications belong in a suitable local environment. A compiler version and its language mode are related but distinct settings.

Check your decision

What should you check if swift is not found?
Choose one answer. You can try again.

Answer and explanation

Check the official installation steps and whether the toolchain command is on the shell path.

Check the official installation steps and whether the toolchain command is on the shell path. Editing the printed message cannot make an absent compiler available.

Set up and run Swift in practice

Check the official installation steps and whether the toolchain command is on the shell path. Editing the printed message cannot make an absent compiler available. Examples are checked in Swift 6 language mode.

Further reading: official Swift language guide.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Hello World and console output →