How to Install and Run Swift Language in Visual Studio Code Editor in Linux Ubuntu

Learn how to set up Swift Programming Language in Ubuntu, set up the code editor for compiling the Swift code in Visual Studio and set up the PATH .
How to Install and Run Swift Language in Visual Studio Code Editor in Linux Ubuntu - www.pdfcup.com

Step 1 : Download the Swift latest release (.tar) file and (.sig) file, is the digital signature, from the official site: "www.swift.org/download/"


Check the downloaded files.
thomas@dev:~$ ls Downloads/swift*
swift-5.9.2-RELEASE-ubuntu22.04.tar.gz
swift-5.9.2-RELEASE-ubuntu22.04.tar.gz.sig

Step 2: Run the below commands in terminal and install the dependencies

thomas@dev~$ apt-get install \
          binutils\
          git\
          libc6-dev\
          libcurl4\
          libedit2\
          libgcc-5-dev\
          libpython2.7\
          libsqlite3-0\
          libstdc++-5-dev\
          libxml2\
          pkg-config\
          tzdata\
          zlib1g-dev
thomas@dev~$ apt-get install\
          binutils\
          git\
          gnupg2\
          libc6-dev\
          libcurl4\
          libedit2\
          libgcc-9-dev\
          libpython2.7\
          libsqlite3-0\
          libstdc++-9-dev\
          libxml2\
          libz3-dev\
          pkg-config\
          tzdata\
          uuid-dev\
          zlib1g-dev 
  
thomas@dev~$ apt-get install\
          binutils\
          git\
          gnupg2\
          libc6-dev\
          libcurl4-openssl-dev\
          libedit2\
          libgcc-9-dev\
          libpython3.8\
          libsqlite3-0\
          libstdc++-9-dev\
          libxml2-dev\
          libz3-dev\
          pkg-config\
          tzdata\
          unzip\
          zlib1g-dev      
      
thomas@dev~$ yum install\
    binutils\
    gcc\
    git\
    glibc-static\
    libbsd-devel\
    libedit\
    libedit-devel\
    libicu-devel\
    libstdc++-static\
    pkg-config\
    python2\
    sqlite

    # __block conflicts with clang's __block qualifier
    sed -i -e "s1">'s/\*__block/\*__libc_block/g' /usr/include/unistd.h   
  

thomas@dev~$ yum >install\
      binutils\
      gcc\
      git\
      glibc-static\
      gzip\
      libbsd\
      libcurl\
      libedit\
      libicu\
      libsqlite\
      libstdc++-static\
      libuuid\
      libxml2\
      tar\
      tzdata
   

Step 3: Run the command to import the PGP keys into the system which is mandatory for signature validation.

thomas@dev~$ wget -q -O - https://swift.org/keys/all-keys.asc | \
  gpg --import -
	  

Step 4: Refresh the keys to check if any updation are available in Keyserver:

thomas@dev:~$ gpg --keyserver hkp://keyserver.ubuntu.com --refresh-keys Swift

Step 5: Now use the signature file, that we have downloaded in "Step 1", to validate the the PGP key.

thomas@dev:~$ gpg --verify swift-5.9.2-RELEASE-ubuntu22.04.tar.gz.sig

Step 6: Run the command to Unzip/Extract the Swift downloaded archive file.

thomas@dev:~$ tar xzf Downloads/swift-5.9.2-RELEASE-ubuntu22.04.tar.gz

Step 7: Update the path of swift "/bin" directory and relaunch the Terminal.

thomas@dev:~$ pwd
/home/thomas
thomas@dev:~$ 
thomas@dev:~$ ls 
. . .
swift-5.9.2-RELEASE-ubuntu22.04
. . .
thomas@dev:~$ 
thomas@dev:~$  echo 'export PATH=/home/thomas/swift-5.9.2-RELEASE-ubuntu22.04/usr/bin:$PATH' >> ~/.bashrc
thomas@dev:~$ 

Step 8: Run the Swift command.

thomas@dev:~$ swift
Welcome to Swift!
 
Subcommands:
 
  swift build      Build Swift packages
  swift package    Create and work on packages
  swift run        Run a program from a package
  swift test       Run package tests
  swift repl       Experiment with Swift code interactively
 
  Use `swift --help` for descriptions of available options and flags.
 
  Use `swift help "subcommand"` for more information about a subcommand.
   

Part 2 - How to Run Swift Language in Visual Studio editor.

Step 1: Insall the below three dependency to execurte Swift code in Visual Studio. It's mandatory to install otherwise you might face code execution and complilation error.

Step 2: After installation above extension you can run Swift script in many ways

Option 2.1: Right click on the editor and at the end you will find out a dropdown option on "Swift", click on that and run the script.
Option 2.2: Right click on the editor and select the first option "Run Code", it will execute the swift script and shows the output in output screen or VS buildin terminal.
Option 2.3: There is a another option to run Swift Code directly in the Terminal, type "swift repl".
thomas@dev:~$ swift repl

Welcome to Swift version 5.9.2 (swift-5.9.2-RELEASE).
Type :help for assistance.
  1> var t = 6
t: Int = 6
  2> t
$RO:  Int = 6

How to Install and Run Swift Language in Terminal or Visual Studio Code Editor in Linux Ubuntu

About the author

D Shwari
I'm a professor at National University's Department of Computer Science. My main streams are data science and data analysis. Project management for many computer science-related sectors. Next working project on Al with deep Learning.....

Post a Comment