Wednesday, April 8, 2020

Swift 5.2 on Ubuntu for Windows 10 bash shell

Prerequisite:
Windows 10 64 bits Home or Pro Edition
Enable WSL via powershell as Administrator
Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux
Enable-WindowsOptionalFeature -Online -FeatureName VirtualMachinePlatform


Install Ubuntu App from Microsoft Store

Swift 5.2 on Ubuntu (18.04 LTS) for Windows 10 bash shell

You need to download and extract the Release (Ubuntu 18.04) from swift.org and do these under bash shell of Ubuntu App.
shell script    Select all
# Show Ubuntu Version lsb_release -a # download and extract swift 5.2 Release cd ~/ wget https://swift.org/builds/swift-5.2-release/ubuntu1804/swift-5.2-RELEASE/swift-5.2-RELEASE-ubuntu18.04.tar.gz tar xzvf swift-5.2-RELEASE-ubuntu18.04.tar.gz sudo mv ~/swift-5.2-RELEASE-ubuntu18.04 /usr/share/swift-5.2 # install packages and development tools sudo apt-get update sudo apt-get install -y clang sudo apt-get install -y libcurl3 libpython2.7 libpython2.7-dev sudo apt-get install -y libcurl4-openssl-dev sudo apt-get install -y git build-essential # add path in ~/.bashrc echo "export PATH=/usr/share/swift-5.2/usr/bin:\$PATH" >> ~/.bashrc # disable color in ~/.bashrc echo 'export TERM=xterm-mono' >> ~/.bashrc #reload ~/.bashrc source ~/.bashrc Check the Swift version swift --version # test Foundation, libdispatch and swiftc compile cd $HOME cat > hello.swift <<EOF import Foundation let device = "WIN10" print("Hello from Swift on \(device)") print("\(NSTimeZone.default.abbreviation()!) \(NSDate())") // Test libdispatch import Dispatch var my_dispatch_group = DispatchGroup() let concurrentQueue = DispatchQueue(label: "myqueuename", attributes: DispatchQueue.Attributes.concurrent) for a in 1...20 { my_dispatch_group.enter() let block = DispatchWorkItem { print("do something at \(a)") } my_dispatch_group.leave() my_dispatch_group.notify(queue: concurrentQueue, work:block) } let item = DispatchWorkItem { print("PROGRAM ENDED \(NSTimeZone.default.abbreviation()!) \(NSDate())") } my_dispatch_group.notify(queue: DispatchQueue.global(qos:.userInitiated), work:item) print("press enter to exit") let _ = readLine(strippingNewline: true) EOF swiftc hello.swift ./hello # test Swift Package Manager mkdir -p $HOME/test1 cd $HOME/test1 swift package init swift test


Follow this guide to install docker on Ubuntu 16.0.4 LTS. Currently 18.0.4 is not working for docker CE on WSL.
https://medium.com/faun/docker-running-seamlessly-in-windows-subsystem-linux-6ef8412377aa


To get Swift 5.2 on Ubuntu 16.0.4
wget https://swift.org/builds/swift-5.2-release/ubuntu1604/swift-5.2-RELEASE/swift-5.2-RELEASE-ubuntu16.04.tar.gz
tar xzvf swift-5.2-RELEASE-ubuntu16.04.tar.gz
sudo mv ~/swift-5.2-RELEASE-ubuntu16.04 /usr/share/swift-5.2




For docker on WSL, you might encounter error on apt-get update with Error in GPG signature. The temporary solution is to change storage driver to vfs as the default overlay2 driver does not work under wsl. But the performance of vfs is really poor.
shell script    Select all
# create the following file in /etc/docker/daemon.json and restart the machine and docker daemon echo '{"storage-driver": "vfs"}' | sudo tee /etc/docker/daemon.json


Click on the docker label below to see some previous examples on using docker.



For example using the quantlib-python3 docker image
docker run -t -i lballabio/quantlib-python3:latest bash

To test the quantlib-python3 docker image
shell script    Select all
# Display unbuntu version apt-get update apt install lsb-release lsb_release -a # Test python 3 QuantLib apt-get install python3 python3-pip -y pip3 install numpy QuantLib-Python==1.18 pip3 freeze cd $HOME cat > $HOME/swap.py <<EOF from __future__ import print_function import numpy as np import QuantLib as ql print("QuantLib version is", ql.__version__) # Set Evaluation Date today = ql.Date(31,3,2015) ql.Settings.instance().setEvaluationDate(today) # Setup the yield termstructure rate = ql.SimpleQuote(0.03) rate_handle = ql.QuoteHandle(rate) dc = ql.Actual365Fixed() disc_curve = ql.FlatForward(today, rate_handle, dc) disc_curve.enableExtrapolation() hyts = ql.YieldTermStructureHandle(disc_curve) discount = np.vectorize(hyts.discount) start = ql.TARGET().advance(today, ql.Period('2D')) end = ql.TARGET().advance(start, ql.Period('10Y')) nominal = 1e7 typ = ql.VanillaSwap.Payer fixRate = 0.03 fixedLegTenor = ql.Period('1y') fixedLegBDC = ql.ModifiedFollowing fixedLegDC = ql.Thirty360(ql.Thirty360.BondBasis) index = ql.Euribor6M(ql.YieldTermStructureHandle(disc_curve)) spread = 0.0 fixedSchedule = ql.Schedule(start, end, fixedLegTenor, index.fixingCalendar(), fixedLegBDC, fixedLegBDC, ql.DateGeneration.Backward, False) floatSchedule = ql.Schedule(start, end, index.tenor(), index.fixingCalendar(), index.businessDayConvention(), index.businessDayConvention(), ql.DateGeneration.Backward, False) swap = ql.VanillaSwap(typ, nominal, fixedSchedule, fixRate, fixedLegDC, floatSchedule, index, spread, index.dayCounter()) engine = ql.DiscountingSwapEngine(ql.YieldTermStructureHandle(disc_curve)) swap.setPricingEngine(engine) print(swap.NPV()) print(swap.fairRate()) EOF # Test python3 cd $HOME python3 swap.py # Install boost 1.71 apt-get install build-essential export boost_version=1.71.0; export boost_dir=boost_1_71_0; cd $HOME; wget https://dl.bintray.com/boostorg/release/${boost_version}/source/${boost_dir}.tar.gz export boost_version=1.71.0; export boost_dir=boost_1_71_0; cd $HOME; tar xfz ${boost_dir}.tar.gz && cd ${boost_dir} && ./bootstrap.sh && ./b2 --without-python --prefix=/usr -j 4 link=shared runtime-link=shared install && cd .. && rm -rf ${boost_dir} && ldconfig # Install quantlib 1.17 export quantlib_version=1.17; cd $HOME; wget https://dl.bintray.com/quantlib/releases/QuantLib-${quantlib_version}.tar.gz export quantlib_version=1.17; cd $HOME; tar xfz QuantLib-${quantlib_version}.tar.gz && cd QuantLib-${quantlib_version} && ./configure --prefix=/usr --disable-static CXXFLAGS=-O3 && make -j 4 && make install && cd .. && ldconfig # Test quantlib 1.17 #Create testql.cpp cd $HOME cat > testql.cpp << 'testqlEOF' #include <ql/quantlib.hpp> int main() { std::cout << "BOOST version is " << BOOST_VERSION << std::endl; std::cout << "QL version is " << QL_VERSION << std::endl; #if __x86_64__ || __WORDSIZE == 64 std::cout << "This is 64 bits" << std::endl; #elif __i386__ || __WORDSIZE == 32 std::cout << "This is 32 bits" << std::endl; #else std::cout << "This is something else" << std::endl; #endif return 0; } testqlEOF g++ testql.cpp -lQuantLib -o testql ./testql # Test QuantLib Examples cd $HOME g++ QuantLib-*/Examples/Bonds/Bonds.cpp -lQuantLib -o testBonds ./testBonds cd $HOME g++ QuantLib-*/Examples/FRA/FRA.cpp -lQuantLib -o testFRA ./testFRA # Python 2 installation apt-get install python python-pip -y pip2 install numpy QuantLib-Python==1.17 pip2 freeze apt-get install git -y # Test python2 cd $HOME git clone git://github.com/mmport80/QuantLib-with-Python-Blog-Examples.git cd QuantLib-with-Python-Blog-Examples/ python2 blog_frn_example.py cd $HOME python2 swap.py



Export and Import of container
docker export CONTAINER_NAME | gzip > NAME.gz
zcat NAME.gz | docker import - IMAGE-NAME



No comments: