brookinc / swift-linux-vagrant

A Vagrant configuration that downloads and installs Swift for Linux.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

swift-linux-vagrant

A Vagrant configuration that downloads and installs Swift for Linux in one easy step (*) .

(* or three easy steps, if you include installing VirtualBox and Vagrant. 😁)

What Is It?

It's an easy (albeit no-frills) way to compile and run Swift code on any Mac, Windows, or Linux machine, by creating a Linux virtual machine on your computer, and then installing Swift for Linux on that virtual machine.

(You can also check out the Replit Swift REPL, which lets you type and evaluate simple Swift code straight from your web browser (free account required). πŸ’»πŸ˜Ž)

What Do I Need To Use It?

It should work on most modern Mac, Windows, and Linux machines.

A good network connection is necessary during initial setup, as the installation involves 1GB or so of downloading. Once the installation is complete, however, no internet connection is required for further usage.

You'll also need around 4GB of free hard drive space.

If you encounter any problems installing or running, feel free to submit an issue or (even better) a pull request. πŸ˜‰

How Do I Use It?

  • VirtualBox and Vagrant enable you to create and run virtual machines on your computer. If you haven't used them before, you'll need to download and install those two programs first. (Like Swift itself, both are free and open-source.)
  • Once that's done, sync this repository to your machine, then navigate to the corresponding local directory in the terminal.
  • Run vagrant up and wait a few minutes for the setup process to complete.
  • Use vagrant ssh to log in to your newly-provisioned virtual machine.
  • Within the virtual machine, your repo directory is accessible as /vagrant, so use cd /vagrant to change to that directory.
  • Enter swift test.swift to run the included test file. Hooray! You're running Swift code on Linux! πŸŽ‰

Playing Around

  • As noted above, you can use swift plus the name of a Swift file to run that file and display any output. (Why not start off by creating a new test2.swift file in your repo directory, then seeing if you can run it the same way you ran test.swift above?)
  • You can also use swift by itself (from any directory) to enter the Swift REPL environment, where you can type Swift code and see the results immediately.
  • Lastly, you can use Swift's package manager system to actually compile your Swift code into a command-line executable.

Exiting

  • You can quit the Swift REPL with :quit.
  • After that, you can log out of your virtual machine with exit.
  • To shut down your virtual machine, you can use vagrant suspend (which saves the contents of the VM's memory to disk first) or vagrant halt (which simply shuts the VM down). Either is typically fine.
  • To get rid of your virtual machine altogether (ie. to free up hard drive space) see the "Uninstalling" section below.

Customizing

  • As-is, these files will install the latest release of Swift on Ubuntu 22.04, but you can easily change to a different supported Swift or Ubuntu version:
    • If you want to install a different version of Swift, change the SWIFT_VERSION variable in Vagrantboot.sh.
    • If you want to use a different version of Ubuntu, change the config.vm.box entry in Vagrantfile.
    • By default, you will also get SwiftLint a helpful tool for improving the quality and consistency of your Swift code
      • To run SwiftLint on your code, simply run swiftlint in the /vagrant directory (or wherever else you've put your Swift code).
      • You can further customize how SwiftLint processes your code by editing the .swiftlint.yml file that gets created for you.
      • For more SwiftLint options, run swiftlint help, or see the documentation.
      • To prevent SwiftLint from being installed, simply change the appropriate line in Vagrantboot.sh from INSTALL_SWIFTLINT=true to INSTALL_SWIFTLINT=false
      • If you're on Windows, you may also need to run your command prompt as an administrator when you initialize your VM with vagrant up for the first time for SwiftLint to work correctly. (If needed, you can use vagrant destroy and then use an administrator command prompt to run vagrant up again.)

Uninstalling

  • To get rid of your virtual machine, you can run vagrant destroy from the repo directory.
    • This will delete the virtual machine that was created (under "~/VirtualBox VMs/"), however the files in your repo directory will remain untouched.
    • To free up additional space, you can delete the swift subdirectory that was created inside your repo directory. It contains the Swift binaries that were downloaded by the script. (They'll be re-downloaded the next time you run vagrant up.)
  • Vagrant also keeps copies of the operating system image files it downloads in ~/.vagrant.d/boxes/ -- you can wipe this directory whenever needed; the newest image file will simply be re-downloaded the next time it's required.
  • VirtualBox and Vagrant can be completely uninstalled by following their respective uninstall instructions:

Further Reading

About

A Vagrant configuration that downloads and installs Swift for Linux.

License:MIT License


Languages

Language:Shell 99.8%Language:Swift 0.2%