Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

Swift Dictionary

  1. Home
  2. Swift Dictionary
  3. [Setup] Swift Development Environment

[Setup] Swift Development Environment

This page walks you through setting up a Swift development environment. On macOS, installing Xcode is all you need to start using Swift.

Setting Up on macOS

Installing Xcode

Install Xcode from the App Store. Installing Xcode automatically includes the Swift compiler and debugger.

If you do not need a full Xcode installation, you can use Swift on the command line with just the Xcode Command Line Tools.

xcode-select --install

After installation, verify with the following command.

swift --version

If version information is displayed, the installation is complete.

Interactive Mode (REPL)

Typing swift in Terminal launches the interactive mode (REPL). You can try code one line at a time, making it handy for quick checks.

swift
Welcome to Swift!
  1> let message = "Hello, World!"
message: String = "Hello, World!"
  2> print(message)
Hello, World!
  3> 1 + 2
$R0: Int = 3
  4> :quit

Type :quit or press Ctrl + D to exit the REPL.

Creating and Running a File

1. Create the source file

Use a text editor to create a file named hello.swift with the following content.

print("Hello, World!")
print("The Swift environment is set up successfully.")

2. Run

swift hello.swift

If Hello, World! and The Swift environment is set up successfully. are displayed, you're all set.

Compiling to generate an executable

swiftc hello.swift -o hello
./hello

swiftc is the compiler command. swift filename.swift runs the file as a script, while swiftc generates an executable file.

Swift Playgrounds

Swift Playgrounds is a learning-focused app available for iPad and Mac. You can download it for free from the App Store.

Results are displayed instantly as you write code, making it ideal for experimenting with syntax as you learn. Since no environment setup is required, it is a great place to start for beginners.

Setting Up on Linux

Swift is also available on Linux.

  1. Download the package for your distribution from swift.org.
  2. Extract the downloaded file and configure the PATH.

Extract the downloaded archive.

tar xzf swift-*-linux.tar.gz

Configure the PATH (add the following to your .bashrc or similar file).

export PATH=/path/to/swift/usr/bin:$PATH

Verify the installation.

swift --version

If the Command Is Not Found

If your terminal displays swift: command not found or swiftc: command not found, follow the steps below to check and fix the issue.

For macOS

On macOS, installing Xcode or the Command Line Tools makes Swift available. Run the following command to install (or reinstall) the Command Line Tools.

Install Command Line Tools.

xcode-select --install

Check if already installed.

xcode-select -p

If Xcode is installed but the command is still not found, set the Xcode path with the following.

Select the Xcode toolchain.

sudo xcode-select --switch /Applications/Xcode.app/Contents/Developer

For Linux

If you downloaded and installed from swift.org, the PATH to the extracted location may be missing.

Check where the command is located.

which swift
which swiftc

If not found, check the extracted location.

ls /opt/swift/usr/bin/swift

Once you know the extracted location, add the PATH to your shell configuration file. First, check which shell you are using.

echo $SHELL

If /bin/zsh is shown, edit ~/.zshrc; if /bin/bash is shown, edit ~/.bashrc.

For macOS (zsh):

echo 'export PATH="/opt/swift/usr/bin:$PATH"' >> ~/.zshrc
source ~/.zshrc

For Linux (bash):

echo 'export PATH="/opt/swift/usr/bin:$PATH"' >> ~/.bashrc
source ~/.bashrc

Replace /opt/swift/usr/bin with the actual path where you extracted the archive.

If you find any errors or copyright issues, please .