Skip to content

Instantly share code, notes, and snippets.

@Jswizzy
Last active October 28, 2024 20:04
Show Gist options
  • Save Jswizzy/408af5829970f9eb18f9b45f891910bb to your computer and use it in GitHub Desktop.
Save Jswizzy/408af5829970f9eb18f9b45f891910bb to your computer and use it in GitHub Desktop.
Swift Install Instruction 20.04

Installing Swift on Ubuntu 20.04

1. Install Depencies

“clang”[ˈklæŋ] is the compiler based on LLVM for C, C++, Objective-C, and Objective-C++. clang is needed to install in order to Swift. Run the following code in Ubuntu terminal.

Before installing swift “libpython2.7” and “libpython2.7-dev” are needed to get Swift running. Run the following code.

sudo apt install clang libpython2.7 libpython2.7-dev

2. Download Swift tar for Ubunutu 20.04

There is a source code in Swift documentation page. Swift.org — Download Swift

By using “wget” command, download the Swift tar file for Ubuntu20.04 from the above page. (Noted swift-5.3 is the newest version when this article is published.) Before running the below code, change to the directory which you want to download by using “cd” command. And then, run the following code.

wget https://swift.org/builds/swift-5.3-release/ubuntu2004/swift-5.3-RELEASE/swift-5.3-RELEASE-ubuntu20.04.tar.gz

3. Extract the downloaded "tar" file

By using “tar” command, expand the downloaded archive file.

x = extract/unzip z= gunzip format f= file

tar xzf swift-5.3-RELEASE-ubuntu20.04.tar.gz

4. Move the extracted files

Move the extracted file to the user’s “share” directory. A good place to put applications.

sudo mv swift-5.3-RELEASE-ubuntu20.04 /usr/share/swift

5. Add to system PATH enviroment variable

Set the Swift path on the system’s PATH environment variable.

note: If using bash change ~/.zshrc to ~/.bashrc

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

6. Check Install successful

Check Version

swift -v

if successful

Swift version 5.3 (swift-5.3-RELEASE)
Target: x86_64-unknown-linux-gnu

run the REPL

swift

if successful the swift REPL will run

to quit the REPL, run the ":exit" command

> :exit
@LucasDarie
Copy link

LucasDarie commented Dec 5, 2021

Thanks for the tutorial, I noticed some mistakes that can be correct this way :

3. Extract the downloaded "tar" file

.tar.gz at the end is missing and the name is ubunutu and not ubuntu

tar xzf swift-5.3-RELEASE-ubuntu20.04.tar.gz

4. Move the extracted files

a / is missing between share and swift and the name is again ubunutu

sudo mv swift-5.3-RELEASE-ubuntu20.04 /usr/share/swift

@Jswizzy
Copy link
Author

Jswizzy commented Dec 29, 2021

Thanks

@rafaelclaycon
Copy link

I created a Brazilian Portuguese version here. Also updated it for Ubuntu 22.04 LTS and Swift 5.7.1. Hope you don't mind (credit at the very end).
https://gist.github.com/rafaelclaycon/276e3b70e3f265851a5ec695e0933e50

@DeboBurro
Copy link

I would probably do sudo mv swift-5.3-RELEASE-ubuntu20.04 /usr/local/swift since most of the time, other packages would install the executables in /usr/local/ while putting related documentation, metadata and images into /usr/share.

Also echo "export PATH=/usr/local/swift/usr/bin:\$PATH" >> ~/.zshrc. Put an \ before the second PATH so it's not expended when running this command.

@leonardsangoroh
Copy link

I wish there was a way this would appear as a top search. Good stuff!
Easy to follow, thanks!

@tylermilner
Copy link

This guide worked great for me to install current Swift 5.7.3 to an Ubuntu 20.04.5 virtual machine using UTM on M2 MacBook Pro. I used the suggestions by @DeboBurro and the bash setup since I haven't switched to zsh yet on the VM. I also needed to use swift repl to enter the REPL at the end to test. Here's what my commands looked like:

sudo apt install clang libpython2.7 libpython2.7-dev
wget https://download.swift.org/swift-5.7.3-release/ubuntu2004-aarch64/swift-5.7.3-RELEASE/swift-5.7.3-RELEASE-ubuntu20.04-aarch64.tar.gz
tar xzf swift-5.7.3-RELEASE-ubuntu20.04-aarch64.tar.gz
sudo mv swift-5.7.3-RELEASE-ubuntu20.04-aarch64 /usr/local/swift
echo "export PATH=/usr/local/swift/usr/bin:\$PATH" >> ~/.bashrc
source ~/.bashrc
swift -v
swift repl
:exit

@martin-thoma
Copy link

Swift 5.8:

sudo apt install clang libpython2.7 libpython2.7-dev
wget https://swift.org/builds/swift-5.8-release/ubuntu2004/swift-5.8-RELEASE/swift-5.8-RELEASE-ubuntu20.04.tar.gz
tar xzf swift-5.8-RELEASE-ubuntu20.04.tar.gz
sudo mv swift-5.8-RELEASE-ubuntu20.04 /usr/share/swift
echo "export PATH=/usr/share/swift/usr/bin:$PATH" >> ~/.zshrc
source ~/.zshrc

Check if it worked with swift --version

@Hritik14
Copy link

Hritik14 commented Aug 3, 2023

I was getting

swift: error while loading shared libraries: libtinfo.so.5: cannot open shared object file: No such file or directory

had to install nucrses as per this

sudo apt install libncurses5

@JanisZhang
Copy link

Thanks!

@tzengshinfu
Copy link

Thanks!

@SantiiRepair
Copy link

In case it happens to someone, I'll show you the following, I was getting:

santiirepair@ubuntu:~$ swift
Swift version 5.3 (swift-5.3-RELEASE)
Target: x86_64-unknown-linux-gnu
/usr/share/swift/usr/bin/lldb "--repl=-disable-objc-interop -color-diagnostics"
warning: (x86_64) /usr/lib/x86_64-linux-gnu/ld-2.31.so Unable to initialize decompressor for section '.debug_abbrev': zlib is not available
warning: (x86_64) /usr/lib/x86_64-linux-gnu/ld-2.31.so Unable to initialize decompressor for section '.debug_str': zlib is not available
warning: (x86_64) /usr/lib/x86_64-linux-gnu/ld-2.31.so Unable to initialize decompressor for section '.debug_info': zlib is not available
warning: (x86_64) /lib/x86_64-linux-gnu/libdl.so.2 Unable to initialize decompressor for section '.debug_abbrev': zlib is not available
warning: (x86_64) /lib/x86_64-linux-gnu/libdl.so.2 Unable to initialize decompressor for section '.debug_str': zlib is not available
warning: (x86_64) /lib/x86_64-linux-gnu/libdl.so.2 Unable to initialize decompressor for section '.debug_info': zlib is not available
warning: (x86_64) /lib/x86_64-linux-gnu/libc.so.6 Unable to initialize decompressor for section '.debug_abbrev': zlib is not available
warning: (x86_64) /lib/x86_64-linux-gnu/libc.so.6 Unable to initialize decompressor for section '.debug_str': zlib is not available
warning: (x86_64) /lib/x86_64-linux-gnu/libc.so.6 Unable to initialize decompressor for section '.debug_info': zlib is not available
warning: (x86_64) /lib/x86_64-linux-gnu/libm.so.6 Unable to initialize decompressor for section '.debug_abbrev': zlib is not available
warning: (x86_64) /lib/x86_64-linux-gnu/libm.so.6 Unable to initialize decompressor for section '.debug_str': zlib is not available
warning: (x86_64) /lib/x86_64-linux-gnu/libm.so.6 Unable to initialize decompressor for section '.debug_info': zlib is not available
Welcome to Swift version 5.3 (swift-5.3-RELEASE).
Type :help for assistance.
  1> :exit

The solution:

santiirepair@ubuntu:~$ sudo apt remove libc6-dbg
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages will be REMOVED:
 libc6-dbg
0 upgraded, 0 newly installed, 1 to remove and 5 not upgraded.
After this operation, 30,0 MB disk space will be freed.
Do you want to continue? [Y/n] y
(Reading database ... 223502 files and directories currently installed.)
Removing libc6-dbg:amd64 (2.31-0ubuntu9.14) ...

Now:

santiirepair@ubuntu:~$ swift
Welcome to Swift version 5.3 (swift-5.3-RELEASE).
Type :help for assistance.
  1> :exit

Source, or forum thread: link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment