Server-Side Swift (Vapor) on Windows 10 inside WSL

chrisrockscode

Christopher Liudahl-Rackley

Posted on March 25, 2021

Server-Side Swift (Vapor) on Windows 10 inside WSL

Some of the time I am not on my iMac to be able practice on my Swift programming. I have been following Vapor for about a year now and like the work that I can do with it now in linux.
So my mission was to get it up and working in WSL on my windows machine, even though its not my favorite, its my most convenient machine (for now).

SET UP WSL

  1. Have WSL version 2 running. (Using powershell run: wsl -l -v to find out which version your using. If anything less than 2, research how to update wsl.

  2. While in WSL you will need to get a GUI up and running. My favorite is VcXsrv. Install this on Windows, config it to Multiple windows and Start no client then save the config. You will need this to be running to get wsl apps to open up in their own windows.

  3. Now for your bash config
    Go back to wsl and inside your .bashrc add this to the end:

# set DISPLAY variable to the IP automatically assigned to WSL2
export DISPLAY=$(cat /etc/resolv.conf | grep nameserver | awk '{print $2; exit;}'):0.0
Enter fullscreen mode Exit fullscreen mode

After that command add this to .bashrc:

sudo /etc/init.d/dbus start &> /dev/null
Enter fullscreen mode Exit fullscreen mode

Then you need to make sure theres no password so enter this command:
sudo visudo -f /etc/sudoers.d/dbus
Nano will open up and enter:

<your_username> ALL = (root) NOPASSWD: /etc/init.d/dbus
Enter fullscreen mode Exit fullscreen mode

then ctrl-o enter ctrl=X

  1. Test a GUI app. I usually run firefox first cause you gotta have a web browser to do some work in Vapor. If all of this was done correctly then you should get a linux version of firefox to pop up on the screen!

NOW FOR SWIFT

At this time you need at least swift 5.2, but I am using 5.3.3, I think theres a new version but I havent tested that out yet.
You need to know what version of linux your running in WSL I am using ubuntu, but run lsb_release -a to get the exact version.
now to figure out where you want to file to go. This is pretty important! I have heard of tuts place the file in /mnt/c/swift or ~/ but dont listen to that!

  1. Just cd into /usr/local/. So go to the swift release page here: >https://swift.org/download/#releases copy the link address and throw it in a wget. you should have a .tar.gz file inside the file now when you run ls.
  2. Now that you see this gz file (it should be red) tar -zxvf <filename> this will extract it and show you the progress.
  3. Next part is very important!
echo 'export PATH=/usr/local/swift-3.1.1-RELEASE-ubuntu16.04/usr/bin:$PATH' >> .bashrc
Enter fullscreen mode Exit fullscreen mode

then source .bashrc
if everything is all well then when you run swift --version you should get something that looks like this

Swift version 5.3.3 (swift-5.3.3-RELEASE)
Target: x86_64-unknown-linux-gnu
Enter fullscreen mode Exit fullscreen mode

Now you got Swift on WSL and you got GUI apps to be able to open and run! Good Job!
You are certainly able to quit now... or dive into some new stuff... server-side swift! This is where some fun can begin!

SET UP VAPOR!!!!!

  1. As per vapor docs they recommend getting the cli toolbox, I got this in a swift folder in my dev directory. If you want to be official enter these commands:
git clone https://github.com/vapor/toolbox.git
cd toolbox
git checkout <desired version>
make install
Enter fullscreen mode Exit fullscreen mode

When looking for the desire version to to the git folder here: >https://github.com/vapor/toolbox/releases
and just get the most updated one.
after using make install you will be good.
confirm with vapor --help

  1. From here I created a vapor directory and a projects directory inside. then ran vapor new hello -n to create a bare bones vapor project.
  2. Now the time to see if everything will work. Run vapor run it will start to compile. Once finished it will give the address that its server the files to. Now that you got a web browser GUI app working open up that firefox and enter the address its giving you. If all works well you should get some basic 'it works!' text up at the top! If you got this working you are officially running a swift server side webframework on windows 10 inside WSL! Congrats, it was a bit of a journey but to do from nothing its worth the work!

Let me know if you guys run into any issues, hope this helped you on your coding journey!

💖 💪 🙅 🚩
chrisrockscode
Christopher Liudahl-Rackley

Posted on March 25, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related