Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to run Swift code using Command Line Tools

I downloaded Xcode CLT from developer.apple.com/downloads and then installed the package.

Wanting to try my hands at Swift, I attempted to write a simple "hello, world" program, but, things seemed to be "wrong" from the first moment I started it. Look at the following screen output:

$ xcrun swift 
Welcome to Swift!  Type :help for assistance.
dyld: Library not loaded: @rpath/libswiftCore.dylib
  Referenced from: /Library/Developer/CommandLineTools/Library/PrivateFrameworks/LLDB.framework/Versions/A/Resources/repl_swift
  Reason: image not found
  1> s="hello, world"
Error in auto-import:
failed to get module 'Swift' from AST context
  1> ^D

I am especially concerned with the "dyld" line which is about line # 3 in that output.

I searched around on the web and found that this is caused by some certificate mismatch issues. But, I have neither requested any certificates from Apple, nor issued any certificates to anyone. I just want to try out Swift without downloading the entire XCode.

So, the questions are:

  • why am I getting the "dyld: Library not loaded:..." error?
  • how can i fix it?
  • how can I write, compile and run simple Swift code on the command line before downloading the entire XCode?

Your help is greatly appreciated. Anxiously waiting for your reply.

Edit:

Here's how I installed the tools: I downloaded the tools from the developer.apple.com/downloads web site. I got a dmg file, which I clicked. It contained a package file. When I clicked that, it brought up a nice popup that said something to the effect "installing". I went through all the dialogs it threw at me, selecting all the default values. After a few moments, it said "installed".

I then opened emacs, wrote 10 lines of c, compiled and ran it. worked! So, I got on the web, located the most elementary swift program I could find, copied it and attempted to run it ... and boom ... I got that error. So I started the swift interpreter and typed code into it. Nope! That didn't work either!

So, given all that ... I feel I did my best to install the tools, but, please let me know if I have missed any step that could have caused the CLT to install partially and not completely.

like image 223
thatmaheshrs Avatar asked Feb 26 '26 10:02

thatmaheshrs


2 Answers

failed to get module 'Swift' from AST context

Use the xcode-select command-line tool:

sudo xcode-select --switch /Applications/Xcode.app

or if you are using Xcode-beta then use this:

sudo xcode-select --switch /Applications/Xcode-beta.app

From the xcode-select manual:

After setting a developer directory, all of the xcode-select provided developer tool shims ... will automatically invoke the version of the tool inside the selected developer directory.

For more info:

man xcode-select
like image 173
ideasuns Avatar answered Mar 02 '26 15:03

ideasuns


cd /Library/Developer/CommandLineTools/Library/PrivateFrameworks/LLDB.framework/Versions/A/Resources

and then

sudo install_name_tool -rpath @executable_path/../../../../../Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/macosx /Library/Developer/CommandLineTools/usr/lib/swift/macosx repl_swift

Will get rid of the first part of the error. I can't figure out the 2nd part yet. (Error in auto-import). Anyone have any ideas based on the first part of the solution?

like image 30
lieutenantmudd Avatar answered Mar 02 '26 14:03

lieutenantmudd



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!