How can I install Swift on Ubuntu 19.04?

According to Swift's official website its supported platforms are:

  • Ubuntu 14.04
  • Ubuntu 16.04
  • Ubuntu 18.04

Is there any way to install it on a machine running Ubuntu 19.04?


Ubuntu has a swift snap package that is buggy and cannot be run at all. What "cannot be run at all" means is that not only does the swift snap package not run at all, but it can't be hacked to run at all without rebuilding the swift snap package. Hopefully this bug will be fixed soon, so that swift can be installed the nice way with sudo snap install swift

For the time being, the alternative is to install the contents of the swift .tar.gz file for Ubuntu 18.04 to your own home directory. Normally it's anathema to mix packages of different releases, but if swift is installed without sudo in your home directory, it will run by itself and it will not interfere with anything else that is already installed in 19.04 and later.

  1. Go to https://swift.org/download/#snapshots and download the Swift 4.2 Development archive: swift-4.2-DEVELOPMENT-SNAPSHOT-2018-10-30-a-ubuntu18.04.tar.gz to your desktop.

  2. Extract the contents of swift-4.2-DEVELOPMENT-SNAPSHOT-2018-10-30-a-ubuntu18.04.tar.gz.

  3. Copy the entire swift-4.2-DEVELOPMENT-SNAPSHOT-2018-10-30-a-ubuntu18.04 directory that you extracted to your own home directory, so that you can run it locally. swift can be run by a normal user from your home directory, without needing to be installed with root privileges.

    cd ~/Desktop/
    cp swift-4.2-DEVELOPMENT-SNAPSHOT-2018-10-30-a-ubuntu18.04 ~/swift/
    
  4. Add the path to swift to your $PATH variable so that the path to swift looks like this: /home/your-username/swift/swift-4.2-DEVELOPMENT-SNAPSHOT-2018-10-30-a-ubuntu18.04/usr/bin, only replace your-username with your own username.

    export PATH="/home/your-username/swift/swift-4.2-DEVELOPMENT-SNAPSHOT-2018-10-30-a-ubuntu18.04/usr/bin:$PATH"  
    source ~/.bashrc

Swift programs can be run interactively from the terminal in the form of an interactive Read Eval Print Loop, or REPL as shown in the below example:

$ swift
Welcome to Swift version 4.2.3 (swift-4.2.3-RELEASE). Type :help for assistance.
  1> import Swift 
  2. print("Hello, World!")
Hello, World!
  3>