Carthage not found when I starts the appium server programmatically and works fine when starts from appium desktop or from terminal

After the server starts from AppiumDriverLocalService successfully, it not able to find the carthage to start webdriver agent.

Here is the code:
public void StartAppiumServer() {

server =AppiumDriverLocalService
	   		  .buildService(new AppiumServiceBuilder().usingDriverExecutable(new File ("/usr/local/bin/node"))
	   		  .withAppiumJS(new File ("/Applications/Appium.app/Contents/Resources/app/node_modules/appium/build/lib/main.js"))
	   	  .withLogFile(new File("/Users/admin/Desktop/AppiumLog/log.txt")));
	   
	     server.start();
}

Appium Logs:
[RemoteTestNG] detected TestNG version 6.13.1
[Appium] Welcome to Appium v1.7.2
[Appium] Non-default server args:
[Appium] log: /Users/admin/Desktop/AppiumLog/log.txt
[Appium] Appium REST http interface listener started on 0.0.0.0:4723
[HTTP] --> GET /wd/hub/status {}
[debug] [MJSONWP] Calling AppiumDriver.getStatus() with args: []
[debug] [MJSONWP] Responding to client with driver.getStatus() result: {“build”:{“version”:“1.7.2”,“revision”:null}}
[HTTP] <-- GET /wd/hub/status 200 29 ms - 72
[HTTP] --> POST /wd/hub/session {“desiredCapabilities”:{“app”:"/Users/admin/Library/Developer/Xcode/DerivedData/myLexia-1-evmhciyjkfapmhcjqcdjufczofys/Build/Products/Debug-iphonesimulator/myLexia.app",“appPackage”:“com.lexialearning.internal.myLexia”,“automationName”:“XCUITest”,“browserName”:"",“deviceName”:“iPhone X”,“newCommandTimeout”:“200000”,“platformName”:“iOS”,“platformVersion”:“11.2”},“capabilities”:{“desiredCapabilities”:{“app”:"/Users/admin/Library/Developer/Xcode/DerivedData/myLexia-1-evmhciyjkfapmhcjqcdjufczofys/Build/Products/Debug-iphonesimulator/myLexia.app",“appPackage”:“com.lexialearning.internal.myLexia”,“automationName”:“XCUITest”,“browserName”:"",“deviceName”:“iPhone X”,“newCommandTimeout”:“200000”,“platformName”:“iOS”,“platformVersion”:“11.2”},“firstMatch”:[{“browserName”:"",“platformName”:“ios”}]}}
[debug] [MJSONWP] Calling AppiumDriver.createSession() with args: [{“app”:"/Users/admin/Library/Developer/Xcode/DerivedData/myLexia-1-evmhciyjkfapmhcjqcdjufczofys/Build/Products/Debug-iphonesimulator/myLexia.app",“appPackage”:“com.lexialearning.internal.myLexia”,“automationName”:“XCUITest”,“browserName”:"",“deviceName”:“iPhone X”,“newCommandTimeout”:“200000”,“platformName”:“iOS”,“platformVersion”:“11.2”},null,{“desiredCapabilities”:{“app”:"/Users/admin/Library/Developer/Xcode/DerivedData/myLexia-1-evmhciyjkfapmhcjqcdjufczofys/Build/Products/Debug-iphonesimulator/myLexia.app",“appPackage”:“com.lexialearning.internal.myLexia”,“automationName”:“XCUITest”,“browserName”:"",“deviceName”:“iPhone X”,“newCommandTimeout”:“200000”,“platformName”:“iOS”,“platformVersion”:“11.2”},“firstMatch”:[{“browserName”:"",“platformName”:“ios”}]}]
[debug] [BaseDriver] Event ‘newSessionRequested’ logged at 1518527851547 (18:47:31 GMT+0530 (IST))
[Appium] Merged W3C capabilities {“desiredCapabilities”:{“app”:"/Users/admin/Lib… into desiredCapabilities object {“app”:"/Users/admin/Library/Developer/Xcode/De…
[Appium] Creating new XCUITestDriver (v2.67.0) session
[Appium] Capabilities:
[Appium] app: /Users/admin/Library/Developer/Xcode/DerivedData/myLexia-1-evmhciyjkfapmhcjqcdjufczofys/Build/Products/Debug-iphonesimulator/myLexia.app
[Appium] appPackage: com.lexialearning.internal.myLexia
[Appium] automationName: XCUITest
[Appium] browserName:
[Appium] deviceName: iPhone X
[Appium] newCommandTimeout: 200000
[Appium] platformName: ios
[Appium] platformVersion: 11.2
[debug] [BaseDriver]
[debug] [BaseDriver] W3C capabilities [] and MJSONWP desired capabilities were provided
[BaseDriver] Expected W3C “capabilities” to be a JSON Object but was provided with: []
[BaseDriver] Falling back to MJSONWP desired capabilities
[BaseDriver] Capability ‘newCommandTimeout’ changed from string (‘200000’) to integer (200000). This may cause unexpected behavior
[BaseDriver] The following capabilities were provided, but are not recognized by appium: appPackage.
[BaseDriver] Session created with session id: 62125e52-0e8e-4270-bfc8-332192d53b43
[debug] [XCUITest] Current user: ‘admin’
[debug] [XCUITest] Unable to get version of libimobiledevice: Command ‘brew’ not found. Is it installed?
[debug] [XCUITest] Xcode version set to ‘9.2’ (tools v9.2.0.0.1.1510905681)
[debug] [XCUITest] iOS SDK Version set to ‘11.2’
[debug] [BaseDriver] Event ‘xcodeDetailsRetrieved’ logged at 1518527853824 (18:47:33 GMT+0530 (IST))
[iOSSim] Constructing iOS simulator for Xcode version 9.2 with udid ‘C65515DE-C55E-4AAC-AE28-B2DB6C64A425’
[XCUITest] Determining device to run tests on: udid: ‘C65515DE-C55E-4AAC-AE28-B2DB6C64A425’, real device: false
[BaseDriver] Using local app ‘/Users/admin/Library/Developer/Xcode/DerivedData/myLexia-1-evmhciyjkfapmhcjqcdjufczofys/Build/Products/Debug-iphonesimulator/myLexia.app’
[debug] [BaseDriver] Event ‘appConfigured’ logged at 1518527855746 (18:47:35 GMT+0530 (IST))
[debug] [XCUITest] Checking whether app ‘/Users/admin/Library/Developer/Xcode/DerivedData/myLexia-1-evmhciyjkfapmhcjqcdjufczofys/Build/Products/Debug-iphonesimulator/myLexia.app’ is actually present on file system
[debug] [XCUITest] App is present
[debug] [iOS] Getting bundle ID from app ‘/Users/admin/Library/Developer/Xcode/DerivedData/myLexia-1-evmhciyjkfapmhcjqcdjufczofys/Build/Products/Debug-iphonesimulator/myLexia.app’: ‘com.lexialearning.internal.myLexia’
[debug] [BaseDriver] Event ‘resetStarted’ logged at 1518527856013 (18:47:36 GMT+0530 (IST))
[XCUITest] Not scrubbing third party app in anticipation of uninstall
[debug] [BaseDriver] Event ‘resetComplete’ logged at 1518527856374 (18:47:36 GMT+0530 (IST))
[debug] [XCUITest] Starting log capture for iOS Simulator with udid ‘C65515DE-C55E-4AAC-AE28-B2DB6C64A425’, using ‘xcrun simctl spawn C65515DE-C55E-4AAC-AE28-B2DB6C64A425 log stream --style compact’
[debug] [BaseDriver] Event ‘logCaptureStarted’ logged at 1518527857648 (18:47:37 GMT+0530 (IST))
[XCUITest] Setting up simulator
[debug] [iOS] No reason to set locale
[debug] [iOS] No iOS / app preferences to set
[debug] [iOSSim] Matched 1 Simulator cache item for cleanup: /Users/admin/Library/Developer/CoreSimulator/Devices/C65515DE-C55E-4AAC-AE28-B2DB6C64A425/data/Library/Caches/com.apple.mobile.installd.staging
[debug] [iOSSim] Setting common Simulator preferences to {“ConnectHardwareKeyboard”:false}
[debug] [iOSSim] Updated C65515DE-C55E-4AAC-AE28-B2DB6C64A425 Simulator preferences at ‘/Users/admin/Library/Preferences/com.apple.iphonesimulator.plist’ with {“ConnectHardwareKeyboard”:false}
[debug] [iOSSim] The count of running Simulator UI client instances is 1
[iOSSim] Both Simulator with UDID C65515DE-C55E-4AAC-AE28-B2DB6C64A425 and the UI client are currently running
[debug] [BaseDriver] Event ‘simStarted’ logged at 1518527861263 (18:47:41 GMT+0530 (IST))
[debug] [XCUITest] Reset requested. Removing app with id ‘com.lexialearning.internal.myLexia’ from the device
[debug] [XCUITest] Installing ‘/Users/admin/Library/Developer/Xcode/DerivedData/myLexia-1-evmhciyjkfapmhcjqcdjufczofys/Build/Products/Debug-iphonesimulator/myLexia.app’ on Simulator with UUID ‘C65515DE-C55E-4AAC-AE28-B2DB6C64A425’…
[debug] [XCUITest] The app has been installed successfully.
[debug] [BaseDriver] Event ‘appInstalled’ logged at 1518527879428 (18:47:59 GMT+0530 (IST))
[XCUITest] Using WDA path: ‘/Applications/Appium.app/Contents/Resources/app/node_modules/appium-xcuitest-driver/WebDriverAgent’
[XCUITest] Using WDA agent: ‘/Applications/Appium.app/Contents/Resources/app/node_modules/appium-xcuitest-driver/WebDriverAgent/WebDriverAgent.xcodeproj’
[debug] [XCUITest] No obsolete cached processes from previous WDA sessions listening on port 8100 have been found
[debug] [JSONWP Proxy] Proxying [GET /status] to [GET http://localhost:8100/status] with no body
[debug] [XCUITest] WDA is not listening at ‘http://localhost:8100/
[debug] [BaseDriver] Event ‘wdaStartAttempted’ logged at 1518527880808 (18:48:00 GMT+0530 (IST))
[XCUITest] Launching WebDriverAgent on the device
[XCUITest] Error: Carthage binary is not found. Install using brew install carthage if it is not installed and make sure the root folder, where carthage binary is installed, is present in PATH environment variable. The current PATH value: ‘/usr/bin:/bin:/usr/sbin:/sbin’
at Object.wrappedLogger.errorAndThrow (/Applications/Appium.app/Contents/Resources/app/node_modules/appium-support/lib/logging.js:69:13)
at checkForDependencies$ (/Applications/Appium.app/Contents/Resources/app/node_modules/appium-xcuitest-driver/lib/wda/utils.js:70:9)
at tryCatch (/Applications/Appium.app/Contents/Resources/app/node_modules/appium-xcuitest-driver/node_modules/babel-runtime/regenerator/runtime.js:67:40)
at GeneratorFunctionPrototype.invoke [as _invoke] (/Applications/Appium.app/Contents/Resources/app/node_modules/appium-xcuitest-driver/node_modules/babel-runtime/regenerator/runtime.js:315:22)
at GeneratorFunctionPrototype.prototype.(anonymous function) [as throw] (/Applications/Appium.app/Contents/Resources/app/node_modules/appium-xcuitest-driver/node_modules/babel-runtime/regenerator/runtime.js:100:21)
at GeneratorFunctionPrototype.invoke (/Applications/Appium.app/Contents/Resources/app/node_modules/appium-xcuitest-driver/node_modules/babel-runtime/regenerator/runtime.js:136:37)
at
Error: Carthage binary is not found. Install using brew install carthage if it is not installed and make sure the root folder, where carthage binary is installed, is present in PATH environment variable. The current PATH value: ‘/usr/bin:/bin:/usr/sbin:/sbin’
at Object.wrappedLogger.errorAndThrow (/Applications/Appium.app/Contents/Resources/app/node_modules/appium-support/lib/logging.js:69:13)
at checkForDependencies$ (/Applications/Appium.app/Contents/Resources/app/node_modules/appium-xcuitest-driver/lib/wda/utils.js:70:9)
at tryCatch (/Applications/Appium.app/Contents/Resources/app/node_modules/appium-xcuitest-driver/node_modules/babel-runtime/regenerator/runtime.js:67:40)
at GeneratorFunctionPrototype.invoke [as _invoke] (/Applications/Appium.app/Contents/Resources/app/node_modules/appium-xcuitest-driver/node_modules/babel-runtime/regenerator/runtime.js:315:22)
at GeneratorFunctionPrototype.prototype.(anonymous function) [as throw] (/Applications/Appium.app/Contents/Resources/app/node_modules/appium-xcuitest-driver/node_modules/babel-runtime/regenerator/runtime.js:100:21)
at GeneratorFunctionPrototype.invoke (/Applications/Appium.app/Contents/Resources/app/node_modules/appium-xcuitest-driver/node_modules/babel-runtime/regenerator/runtime.js:136:37)
at
[debug] [BaseDriver] Event ‘wdaStartFailed’ logged at 1518527881318 (18:48:01 GMT+0530 (IST))
[debug] [XCUITest] Unable to launch WebDriverAgent because of xcodebuild failure: “Carthage binary is not found. Install using brew install carthage if it is not installed and make sure the root folder, where carthage binary is installed, is present in PATH environment variable. The current PATH value: ‘/usr/bin:/bin:/usr/sbin:/sbin’”.
[debug] [XCUITest] Quitting and uninstalling WebDriverAgent, then retrying
[XCUITest] Shutting down sub-processes
[debug] [XCUITest] Removing WDA application from device
[debug] [BaseDriver] Event ‘wdaStartAttempted’ logged at 1518527893467 (18:48:13 GMT+0530 (IST))
[XCUITest] Launching WebDriverAgent on the device
[XCUITest] Error: Carthage binary is not found. Install using brew install carthage if it is not installed and make sure the root folder, where carthage binary is installed, is present in PATH environment variable. The current PATH value: ‘/usr/bin:/bin:/usr/sbin:/sbin’
at Object.wrappedLogger.errorAndThrow (/Applications/Appium.app/Contents/Resources/app/node_modules/appium-support/lib/logging.js:69:13)
at checkForDependencies$ (/Applications/Appium.app/Contents/Resources/app/node_modules/appium-xcuitest-driver/lib/wda/utils.js:70:9)
at tryCatch (/Applications/Appium.app/Contents/Resources/app/node_modules/appium-xcuitest-driver/node_modules/babel-runtime/regenerator/runtime.js:67:40)
at GeneratorFunctionPrototype.invoke [as _invoke] (/Applications/Appium.app/Contents/Resources/app/node_modules/appium-xcuitest-driver/node_modules/babel-runtime/regenerator/runtime.js:315:22)
at GeneratorFunctionPrototype.prototype.(anonymous function) [as throw] (/Applications/Appium.app/Contents/Resources/app/node_modules/appium-xcuitest-driver/node_modules/babel-runtime/regenerator/runtime.js:100:21)
at GeneratorFunctionPrototype.invoke (/Applications/Appium.app/Contents/Resources/app/node_modules/appium-xcuitest-driver/node_modules/babel-runtime/regenerator/runtime.js:136:37)
at
Error: Carthage binary is not found. Install using brew install carthage if it is not installed and make sure the root folder, where carthage binary is installed, is present in PATH environment variable. The current PATH value: ‘/usr/bin:/bin:/usr/sbin:/sbin’
at Object.wrappedLogger.errorAndThrow (/Applications/Appium.app/Contents/Resources/app/node_modules/appium-support/lib/logging.js:69:13)
at checkForDependencies$ (/Applications/Appium.app/Contents/Resources/app/node_modules/appium-xcuitest-driver/lib/wda/utils.js:70:9)
at tryCatch (/Applications/Appium.app/Contents/Resources/app/node_modules/appium-xcuitest-driver/node_modules/babel-runtime/regenerator/runtime.js:67:40)
at GeneratorFunctionPrototype.invoke [as _invoke] (/Applications/Appium.app/Contents/Resources/app/node_modules/appium-xcuitest-driver/node_modules/babel-runtime/regenerator/runtime.js:315:22)
at GeneratorFunctionPrototype.prototype.(anonymous function) [as throw] (/Applications/Appium.app/Contents/Resources/app/node_modules/appium-xcuitest-driver/node_modules/babel-runtime/regenerator/runtime.js:100:21)
at GeneratorFunctionPrototype.invoke (/Applications/Appium.app/Contents/Resources/app/node_modules/appium-xcuitest-driver/node_modules/babel-runtime/regenerator/runtime.js:136:37)
at
[debug] [BaseDriver] Event ‘wdaStartFailed’ logged at 1518527893475 (18:48:13 GMT+0530 (IST))
[debug] [XCUITest] Unable to launch WebDriverAgent because of xcodebuild failure: “Carthage binary is not found. Install using brew install carthage if it is not installed and make sure the root folder, where carthage binary is installed, is present in PATH environment variable. The current PATH value: ‘/usr/bin:/bin:/usr/sbin:/sbin’”.
[debug] [XCUITest] Quitting and uninstalling WebDriverAgent, then retrying
[XCUITest] Shutting down sub-processes
[debug] [XCUITest] Removing WDA application from device
[XCUITest] Error: Unable to launch WebDriverAgent because of xcodebuild failure: “Carthage binary is not found. Install using brew install carthage if it is not installed and make sure the root folder, where carthage binary is installed, is present in PATH environment variable. The current PATH value: ‘/usr/bin:/bin:/usr/sbin:/sbin’”.
at XCUITestDriver.quitAndUninstall$ (/Applications/Appium.app/Contents/Resources/app/node_modules/appium-xcuitest-driver/lib/driver.js:387:13)
at tryCatch (/Applications/Appium.app/Contents/Resources/app/node_modules/appium-xcuitest-driver/node_modules/babel-runtime/regenerator/runtime.js:67:40)
at GeneratorFunctionPrototype.invoke [as _invoke] (/Applications/Appium.app/Contents/Resources/app/node_modules/appium-xcuitest-driver/node_modules/babel-runtime/regenerator/runtime.js:315:22)
at GeneratorFunctionPrototype.prototype.(anonymous function) [as next] (/Applications/Appium.app/Contents/Resources/app/node_modules/appium-xcuitest-driver/node_modules/babel-runtime/regenerator/runtime.js:100:21)
at GeneratorFunctionPrototype.invoke (/Applications/Appium.app/Contents/Resources/app/node_modules/appium-xcuitest-driver/node_modules/babel-runtime/regenerator/runtime.js:136:37)
at
Error: Unable to launch WebDriverAgent because of xcodebuild failure: “Carthage binary is not found. Install using brew install carthage if it is not installed and make sure the root folder, where carthage binary is installed, is present in PATH environment variable. The current PATH value: ‘/usr/bin:/bin:/usr/sbin:/sbin’”.
at XCUITestDriver.quitAndUninstall$ (/Applications/Appium.app/Contents/Resources/app/node_modules/appium-xcuitest-driver/lib/driver.js:387:13)
at tryCatch (/Applications/Appium.app/Contents/Resources/app/node_modules/appium-xcuitest-driver/node_modules/babel-runtime/regenerator/runtime.js:67:40)
at GeneratorFunctionPrototype.invoke [as _invoke] (/Applications/Appium.app/Contents/Resources/app/node_modules/appium-xcuitest-driver/node_modules/babel-runtime/regenerator/runtime.js:315:22)
at GeneratorFunctionPrototype.prototype.(anonymous function) [as next] (/Applications/Appium.app/Contents/Resources/app/node_modules/appium-xcuitest-driver/node_modules/babel-runtime/regenerator/runtime.js:100:21)
at GeneratorFunctionPrototype.invoke (/Applications/Appium.app/Contents/Resources/app/node_modules/appium-xcuitest-driver/node_modules/babel-runtime/regenerator/runtime.js:136:37)
at
[debug] [XCUITest] Not clearing log files. Use clearSystemFiles capability to turn on.
[debug] [iOSLog] Stopping iOS log capture
[MJSONWP] Encountered internal error running command: Error: Unable to launch WebDriverAgent because of xcodebuild failure: “Carthage binary is not found. Install using brew install carthage if it is not installed and make sure the root folder, where carthage binary is installed, is present in PATH environment variable. The current PATH value: ‘/usr/bin:/bin:/usr/sbin:/sbin’”.
at XCUITestDriver.quitAndUninstall$ (/Applications/Appium.app/Contents/Resources/app/node_modules/appium-xcuitest-driver/lib/driver.js:387:13)
at tryCatch (/Applications/Appium.app/Contents/Resources/app/node_modules/appium-xcuitest-driver/node_modules/babel-runtime/regenerator/runtime.js:67:40)
at GeneratorFunctionPrototype.invoke [as _invoke] (/Applications/Appium.app/Contents/Resources/app/node_modules/appium-xcuitest-driver/node_modules/babel-runtime/regenerator/runtime.js:315:22)
at GeneratorFunctionPrototype.prototype.(anonymous function) [as next] (/Applications/Appium.app/Contents/Resources/app/node_modules/appium-xcuitest-driver/node_modules/babel-runtime/regenerator/runtime.js:100:21)
at GeneratorFunctionPrototype.invoke (/Applications/Appium.app/Contents/Resources/app/node_modules/appium-xcuitest-driver/node_modules/babel-runtime/regenerator/runtime.js:136:37)
at
[HTTP] <-- POST /wd/hub/session 500 44828 ms - 459

Although you don’t say, this sounds like an IDE problem. Because you are using TestNg, I’m going to guess you are using Java and therefore:

You should check that carthage is installed or not in the /usr/ directory, because when you are running appium from GUI then it is picking carthage from the Appium.app location, but when you are running programatically then it will pick from the usr directory.

Hope it will help. Thanks

I had the same issue while running appium CLI parallel execution of iOS automation in Eclipse, Here below the solution.

  1. Right click on your project > Run as > Run Configurations
  2. Select Environment tab
  3. Click on select option and select PATH environment
  4. Edit Path variable using Edit option
  5. Update path value with carthage location /usr/local/bin and select append env to native env
  6. Apply changes and run the test.