Hot questions for Using Vapor in ubuntu

Question:

I am trying to install vapor (https://github.com/vapor/vapor) toolbox on a server which is running Ubuntu 15.10 x64.

I am getting this error from the "swift build" command

/swift-3.0/usr/bin/swift-build: error while loading shared libraries:    libcurl.so.4: cannot open shared object file: No such file or directory

Is this a curl issue? How can I resolve this error?

Details:

I am following the steps described here: https://vapor.github.io/documentation/getting-started/install-toolbox.html

Swift 3.0 has been installed:

user@server:~$ swift --version
Swift version 3.0 (swift-3.0-RELEASE)
Target: x86_64-unknown-linux-gnu

To install the toolbox:

user@server:~$ curl -sL toolbox.vapor.sh | bash

✅  Compatible
Downloading...
Compiling...
/swift-3.0/usr/bin/swift-build: error while loading shared libraries: libcurl.so.4: cannot open shared object file: No such file or directory
Installing...
bash: line 22: .build/release/Executable: No such file or directory
Use vapor --help and vapor <command> --help to learn more.

The error above happens at this line in the script: swift build -c release > /dev/null;

swift build --help gives this same error:

user@server:~$ swift build --help
/swift-3.0/usr/bin/swift-build: error while loading shared libraries: libcurl.so.4: cannot open shared object file: No such file or directory

vapor is not present:

user@server:~$ vapor --help
vapor: command not found

Also curl info:

user@server:~$ sudo -i curl --version
curl 7.43.0 (x86_64-pc-linux-gnu) libcurl/7.43.0 GnuTLS/3.3.15 zlib/1.2.8 libidn/1.28 librtmp/2.3
Protocols: dict file ftp ftps gopher http https imap imaps ldap ldaps pop3 pop3s rtmp rtsp smb smbs smtp smtps telnet tftp 
Features: AsynchDNS IDN IPv6 Largefile GSS-API Kerberos SPNEGO NTLM NTLM_WB SSL libz TLS-SRP UnixSockets 

curl was installed with sudo apt-get install curl and did not report any errors.

user@server:/usr/lib$ find . | grep curl reports about 15 matches but none are exactly libcurl.so.4. The closes matches are:

./x86_64-linux-gnu/libcurl-gnutls.so.4
./x86_64-linux-gnu/libcurl-gnutls.so.4.3.0
./x86_64-linux-gnu/libcurl-gnutls.so.3

Answer:

This issue was resolved by running:

user@server:$ sudo apt-get install libcurl3

Question:

I have a VPS (Ubuntu 14.04) with Apache2, Swift 3.1.1 and Vapor installed. I want to receive client side requests to my Vapor-Built-Service via HTTPS protocol (on port 8443 for example). I didn't find any newbie helper document to do this, so please help me. Thanks.


Answer:

After a lot of searching I found the answer: 1. You need to install Apache or Nginx on your VPS. Here I assume that the reader uses Apache. But for more info on installing Apache server on Ubuntu 14.04 you can take a look at here. 2. The next thing you need to do is to make Apache work with SSL (or HTTPS protocol). You can find a complete guide here. 3. Now it's time to set the Apache server as a reverse proxy server. You can find a good guide to do that here and here. don't forget to put the reverse-proxy-config-code at port 443 related part.


Update: There is also another way which you can configure the Vapor directly. Take a look at here.

Question:

Im having 2 problems when trying to generate a random string in Linux with Swift 3.

  1. arc4random_uniform is not available in Linux only on BSD. SO i was able to get away with using random() function. And this worked when i was generating random numbers of a variable size (See code below)

    func generateRandomNumber() -> Int
    {
       var place = 1
    
       var finalNumber = 0;
    
    #if os(Linux)
    for _ in 0..<5
    {
        place *= 10
    
        let randomNumber = Int(random() % 10) + 1
    
        finalNumber += randomNumber * place
    }
    #else
    for _ in 0..<5
    {
        place *= 10
    
        let randomNumber = Int(arc4random_uniform(10))
    
        finalNumber += randomNumber * place
    }
    #endif
    
      return finalNumber
    }
    

And that WORKS. Edit: it works but it gives me the same number every time :(

  1. When trying to generate random alphanumeric string I'm limited to using Swift String and NOT NSSTRING. Linux throws this error

original pre Linux block of code:

   func randomString(_ length: Int) -> String
   {

      let letters : NSString = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
      let len = UInt32(letters.length)

      var randomString = ""

      for _ in 0 ..< length {
    let rand = arc4random_uniform(len)
    var nextChar = letters.character(at: Int(rand))
    randomString += NSString(characters: &nextChar, length: 1) as String
      }

       return randomString
    }

And the actual error I get when using above code

    error: cannot convert value of type 'NSString' to type 'String' in coercion
        randomString += NSString(characters: &nextChar, length: 1) as String

modified for linux block of code.

    func randomString(_ length: Int) -> String
    {

let letters : String = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
let len = letters.characters.count

var randomString = ""

#if os(Linux)

    for _ in 0..<length
    {
        let randomValue = (random() % len) + 1

        randomString += "\(letters[letters.index(letters.startIndex, offsetBy: Int(randomValue))])"
    }

    #else
    for _ in 0 ..< length
    {
        let rand = arc4random_uniform(UInt32(len))

        randomString += "\(letters[letters.index(letters.startIndex, offsetBy: Int(rand))])"
    }
    #endif


      return randomString
}          

but this time the error is weird it only says Illegal instruction with no extra information. I ran the docker container in interactive mode and i saw my server running and printing out when calling other functions etc.

but the thing is the function actually WORKS when i ran it in IBMs swift sandbox

and I'm assuming its using linux also. Im very stuck and confused any help would be greatly appreciated.

(UPDATE): I ran the same function in just a linux env with a single swift file and not the Vapor swift web framework. and it works. As mentioned in my edit above it gives me the same random string everytime. I will still have to test the entire project once my build finishes. But besides that i need to know if the random() function will actually give me something new each time instead of the same crap.


Answer:

Figured it out.

So the answer to the repeating random number/string was to just add this line before i called the random() function

srand(UInt32(time(nil)))

and I'm assuming thats what fixed the illegal instruction also. Because i don't recall changing anything else.

Needless to say here is my final result

 func generateRandomNumber() -> Int
 {
    var place = 1

    var finalNumber = 0;

    #if os(Linux)

    srand(UInt32(time(nil)))

    for _ in 0..<5
    {
        place *= 10

        let randomNumber = Int(random() % 10) + 1

        finalNumber += randomNumber * place
    }
    #else
    for _ in 0..<5
    {
        place *= 10

        let randomNumber = Int(arc4random_uniform(10))

        finalNumber += randomNumber * place
    }
    #endif

     return finalNumber
 }



 func randomString(_ length: Int) -> String
 {

    let letters : String = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
    let len = letters.characters.count

    var randomString = ""

    #if os(Linux)

    srand(UInt32(time(nil)))

   for _ in 0..<length
   {
     let randomValue = (random() % len) + 1

     randomString += "\(letters[letters.index(letters.startIndex, offsetBy: Int(randomValue))])"
   }

   #else
  for _ in 0 ..< length
  {
     let rand = arc4random_uniform(UInt32(len))

     randomString += "\(letters[letters.index(letters.startIndex, offsetBy: Int(rand))])"
  }
  #endif

   return randomString
 }  

Question:

I have just installed Swift 3.0.1 on Ubuntu 14.04 via Vapor tutorial.

curl -sL swift.vapor.sh/ubuntu | bash

Here I received information about that clang version >=3.4 is required. So I manualy have installed clang 3.5 and linked this to clang.

Next I run swift command and try to run any swift command like:

  • print("Hello")
  • let w = "hello"

But swift ends with segmentation fault.

Welcome to Swift version 3.0.1 (swift-3.0.1-RELEASE). Type :help for assistance.

1> print("Hello")

Segmentation fault

Things I have tried

  • different swift versions
  • different clang versions
  • chmod -R o+r on swift folder

Anyone can help?


Answer:

Okay. This server was made on OpenVZ virtualization. Now I have a new one on the KVM and I have made all the same steps and it works...

So solution for my problem was to change virtualization to KVM. Maybe it was a problem of host or OpenVZ itself. No clue.

Question:

Following steps from https://vapor.github.io/documentation/getting-started/install-toolbox.html are leaded to this error:

$ curl -sL toolbox.vapor.sh | bash

/swift-3.0/usr/bin/swift-build: error while loading shared libraries: libFoundation.so: cannot enable executable stack as shared object requires: Invalid argument

Any ideas?


Answer:

I don't have Ubuntu on Windows to check with, but this is a known issue.

This commenter on GitHub appears to have found a solution.

apt-get install execstack
execstack -c {swift_path}/usr/lib/swift/linux/libFoundation.so

Question:

import CTLS

failing on Ubuntu

sudo apt install ctls gives "ctls is already the newest version (1.0.1)."

Following environment: Vapor Toolbox: 3.1.2 Vapor Framework: 2.4.2 Swift version 4.0.3 (swift-4.0.3-RELEASE) openssl is already the newest version (1.0.2g-1ubuntu4.10).

Any help appreciated!


Answer:

Are you importing manually? In the Vapor 3 beta we're using COpenSSL now. There shouldn't usually be any need to directly interface with OpenSSL/LibreSSL. Cryptography is part of the Crypto module and SSL support by the OpenSSL.

Question:

I have created backend with perfectswift. Now I am trying to deploy that with heroku but I am getting some kind of error while pushing. Help me to solve this issue.


Answer:

Seems to be an issue related with stack compatibility. You need to downgrade your stack:

heroku stack:set heroku-16 -a Your_App_Name

Stack Version            Base Technology
----------------------------------------
Cedar-14 (deprecated)     Ubuntu 14.04
Heroku-16                 Ubuntu 16.04
Heroku-18 (default)       Ubuntu 18.04

If you got that warning, means you'll need to use Cedar-14:

heroku stack:set cedar-14 -a Your_App_Name


List of Heroku's stacks:

https://devcenter.heroku.com/articles/stack