Cannot run Appium Scripts with UiAutomator2

When I try to run Appium as Sudo and try to run a script I get:

An unknown server-side error occurred while processing the command. Original error: Could not sign with default certificate. Original error spawn ENOTDIR

When I try to run Appium without Sudo and try to run a script I get:

An unknown server-side error occurred while processing the command. Original error: Could not sign with default certificate. Original error Command '/usr/lib/jvm/java-8-oracle/bin/java -jar /usr/local/lib/node_modules/appium/node_modules/appium-adb/jars/sign.jar /usr/local/lib/node_modules/appium/node_modules/appium-uiautomator2-server/apks/appium-uiautomator2-server-v1.18.0.apk --override' exited with code 1

I have specified this in the script:

desired_caps[‘automationName’] = ‘UiAutomator2’