For a long time I was avoiding server side swift. My main reason was that I really don't like the fact that I have to use virtualization in order to get things work on the local environment. After the release of Swift 3 finally I decided to play with the language on the server side. In this tutorial I'll teach you how to form your local workspace for some backend development.
Step 1: Install VirtualBox
brew cask install virtualbox
Step 2: Grap ubuntu minimal disk image.
You can download the minimal CD - it's called CD 😂 - from this link.
Step 3: Make an ubuntu virtual machine.
Step 3: Install ubuntu
WARNING: During the install process the partition manager wants to create lots of swap area, so you should double check if your partition sizes are correct.
Pro tip: Set at least 16gigs for the main drive.
Follow the install instructions and finish the setup process.
Step 4: Setup port forwarding
Stop the machine, unomunt or even completely eliminate the 'optical' the ubuntu installer disk. Now go to the network settings of the virtual machine.
Click port forwarding, and create a new rule. You can set any kind of host port you want, only the guest port matters. We would like to forward the ssh (22) port. So basically this will allow you to ssh to your virtual machine from your local machine.
Step 5: ssh
On your virtual machime make sure that you install openssh-server.
sudo apt-get install openssh-server
On your host machine (your mac) make a new key-pair.
ssh-keygen -t rsa -b 4096 -C "email@example.com"
Save it inside the ~/.ssh folder. I named mine like 'vbox'.
SSH to your virtual machine using your user account and your password.
ssh -p 3022 firstname.lastname@example.org
Hopefully now you are inside your virtual machine through ssh. The thing is that I don't really prefer passwords. So go to your home folder, make an .ssh folder and place an authorized_keys file inside of it.
cd mkdir .ssh nano authorized_keys
Start a new terminal window/tab. List your vbox.pub file contents on your mac, and copy it's content to the authorized_keys file on the virtual machine. If you used nano you can save by ctrl+x and then pressing the y key and return.
Now create or add the following lines to your ~/.ssh/config file on your host / mac.
Host vbox HostName 127.0.0.1 Port 3022 User ubuntu IdentityFile ~/.ssh/vbox
Obviously you should use your ubuntu linux username here.
Congratulations you can simply ssh to virtual machine like this:
No more password typing again and again.
Step 6: install Swift 3
If you like youtube tutorials, you should watch this one.
Long story short
- Go to swift.org/download
- Right click on the ubuntu installer, and copy the link.
- Use wget or curl to download the swift snapshot to your home folder.
- Use tar -xf swift*.tar.gz to unpack the file.
- Rename the extracted folder simply to swift
- Add the following path component to /etc/environment (use your username)
- Re-ssh to your virtual box machine.
- Try the 'swift --version' command.
- Install some extra packages that you'll need
sudo apt-get install virtualbox-guest-additions-iso sudo apt-get install clang libicu-dev libcurl3
Step 7: rsync
From now on you can use nano to make some Swift code, or you can use Xcode, Sublime Text or Atom on your host machine, and with a simple makefile and the help of the rsync command you can transfer all the files to the client.
all: rsync -avzh --progress --exclude=".*" --exclude="perfect/Packages/*" --exclude="noze/Packages/*" --delete-before ./ vbox:/home/ubuntu/linux
This build script will transfer all the local source files to the virtual machine's ~/linux folder, it'll also exclude some local directories.
NOTE: I am using Sublime Test, because I just have to press cmd+b to sync everything. Also I always keep an open terminal window with an ssh login to the client, so I can compile the sources instantly.
Step 8: Manage the virtualbox machine from Terminal
One little extra thing for command line maniacs like me.
alias vbox.start='VBoxManage startvm "ubuntu" --type headless' alias vbox.stop='VBoxManage controlvm "ubuntu" savestate'
Insert this two aliases to your .bash_profile and you'll never have to start your virtualbox application - in the usual way - again.
By default mongodb won't allow to connect to the database from external IP addresses. If you are trying to tunnel mongodb to the host machine you should edit the /etc/mongod.conf file. Don't forget to tunnel the 27017 port inside the virtual box network settings.
# Listen to local and LAN interface. bindIp = 127.0.0.1,10.0.2.15