EthicsComputerCourses
Join training +91 90532 10052

Install Xcode & Swift

Swift · Lesson 2 of 30 Beginner 8 min read Updated

Before you write real programs you need a way to compile and run Swift. On a Mac the answer is Xcode, Apple's free development app. On Windows and Linux you install the Swift toolchain from swift.org, which gives you the compiler and command-line tools.

This lesson walks you through each option, shows you around an Xcode app project, and explains playgrounds and Swift packages, the two quick ways to try small pieces of code.

Which setup do you need?

Your computerWhat you can doInstall
MacEverything in this course, including iPhone appsXcode from the Mac App Store
Windows PCThe Swift language lessons (up to concurrency)Swift toolchain from swift.org
Linux PCThe Swift language lessons (up to concurrency)Swift toolchain with swiftly
iPadSmall SwiftUI apps and practiceThe Swift Playground app

If you only have a Windows laptop, you can still learn the first half of this course properly. When you reach SwiftUI you will need access to a Mac, for example in a college lab.

Installing Xcode on a Mac

  1. Open the App Store on your Mac, search for Xcode and click Get. It is free but large, so use a good Wi-Fi connection.
  2. Open Xcode once it finishes. It may ask to install extra components and to download platform support such as the iOS simulator. Accept them.
  3. Xcode 27 needs macOS Tahoe 26.6 or later. If your Mac is older, the App Store will tell you which version you can install.

Xcode 27 includes the Swift 6.4 compiler, the iOS 27 SDK (the libraries for building iPhone apps), a simulator that runs iPhone apps on your Mac, and tools for testing and profiling. You can check the Swift version from the Terminal app:

Terminal
swift --version

The first line of the reply names the Swift version, such as Swift 6.4. If you see an error instead, open Xcode once, then open Xcode > Settings > Locations and make sure a version is chosen under Command Line Tools.

Installing Swift on Windows and Linux

On Windows, Swift needs some Microsoft C++ build tools and the Windows SDK, which come with Visual Studio. The Windows install page on swift.org gives one winget command for those parts; run it first. Then install Swift itself from PowerShell:

PowerShell
winget install --id Swift.Toolchain -e

On Linux, swift.org recommends swiftly, a small tool that installs Swift toolchains and switches between versions. The Linux install page gives a short set of commands that download swiftly and run swiftly init, which installs the latest Swift release and sets up your PATH. Later you can run swiftly install latest to update.

In both cases, close and reopen the terminal, then run swift --version to confirm it works. For an editor, Visual Studio Code with the official Swift extension gives you code completion, error highlighting, debugging and test support.

Your first Swift package

A Swift package is a folder of Swift code plus a Package.swift file that describes how to build it. The Swift Package Manager (the swift package, swift build, swift run and swift test commands) works the same on macOS, Windows and Linux. Create a small command-line program like this:

Terminal
mkdir HelloRohtak
cd HelloRohtak
swift package init --name HelloRohtak --type executable
swift run
Output
Hello, world!

swift package init creates Package.swift, a Sources folder with a Swift file that prints a greeting, and usually a Tests folder. swift run compiles the code and runs it. Before the greeting you will see a few lines such as "Building for debugging..." and "Build complete!"; they come from the build step, not from your program. Open the Swift file inside Sources, change the text in print(...), and run swift run again.

Creating an app project in Xcode

  1. Choose File > New > Project.
  2. Pick iOS at the top, then the App template, then Next.
  3. Fill in the options: Product Name (for example ChaiCounter), your Team (you can leave it as None for now), and an Organization Identifier in reverse-domain style such as com.priyasharma. Choose SwiftUI for the interface and Swift for the language. Leave the other options at their defaults.
  4. Choose a folder and click Create.

Xcode joins the organization identifier and product name into a bundle identifier like com.priyasharma.ChaiCounter, the unique ID of your app. The project contains two Swift files: one with @main that starts the app, and ContentView.swift with the first screen. Replace the code in ContentView.swift with this:

Swift
import SwiftUI

struct ContentView: View {
    var body: some View {
        VStack(spacing: 12) {
            Image(systemName: "cup.and.saucer")
                .font(.largeTitle)
            Text("Chai Counter")
                .font(.title)
        }
        .padding()
    }
}

#Preview {
    ContentView()
}

The #Preview block draws the screen in Xcode's canvas on the right of the editor: a cup-and-saucer icon above the words "Chai Counter" in title-sized text, in the middle of the screen. Choose an iPhone simulator from the device menu in the toolbar and press Cmd+R to build and run the app. The simulator opens and shows the same screen. Change the text to "Namaste, Priya!" and the canvas updates almost instantly.

Playgrounds: try code quickly

A playground runs code and shows results as you type, which is perfect for the language lessons.

  • In Xcode: in any Swift file, import Playgrounds and write a #Playground block. Xcode runs it and shows each result in the canvas.
  • On iPad or Mac: the free Swift Playground app (you may see its older name, Swift Playgrounds) teaches Swift with guided lessons and can build small SwiftUI apps.
Swift
import Playgrounds

#Playground {
    let price = 1299
    let gst = price * 18 / 100
    let total = price + gst
}

The canvas lists the value of each line: 1299, 233 and 1532. The GST is 233, not 233.82, because dividing two whole numbers in Swift gives a whole number. You will learn why in Operators.

Common mistakes

  • Installing Xcode on a slow connection or a full disk. Xcode and the simulators need many gigabytes. Free up space first.
  • Not opening Xcode once after installing. Command-line swift may fail until Xcode finishes its first-launch setup.
  • Using spaces or Hindi letters in the bundle identifier. Stick to letters, numbers, dots and hyphens.
  • Skipping the Visual Studio step on Windows. The Swift toolchain needs those C++ tools to link programs.

Key points

  • On a Mac, install Xcode 27 from the App Store; it includes Swift 6.4, the iOS SDK and the simulator.
  • On Windows use winget install --id Swift.Toolchain -e after the Visual Studio components; on Linux use swiftly.
  • swift package init --type executable and swift run create and run a command-line Swift package on any platform.
  • A new Xcode App project uses SwiftUI, and #Preview shows the screen in the canvas; Cmd+R runs it in the simulator.
  • #Playground blocks and the Swift Playground app are quick ways to experiment.

Exercise

Create a Swift package called FeeReminder (or an Xcode #Playground block if you are on a Mac). Make it print two lines: your name, and the sentence "Fees are due on the 10th of every month." Run it and check the output.

Show solution
Terminal
mkdir FeeReminder
cd FeeReminder
swift package init --name FeeReminder --type executable

Replace the code in the Swift file inside Sources with:

Swift
print("Meera Nair")
print("Fees are due on the 10th of every month.")

Then run swift run. After the build messages you see:

Output
Meera Nair
Fees are due on the 10th of every month.

Each print call writes one line, in the order the calls appear.

Quick Quiz

1. Which tool do you need to build an iPhone app?

2. What does swift run do in a Swift package folder?

3. What is a bundle identifier?

4. On Linux, which tool does swift.org recommend for installing Swift?

Learn Swift with a mentor

Free lessons get you started. Our Mobile App Development training adds real projects and code reviews from working developers.

  • Project-based learning
  • Mentor code reviews
  • Free counselling call