//
you're reading...

Programming

Getting Rid of Xcode Command Line Tools

Have you ever uninstalled Xcode Command Line Tools only to have it comes back again when Xcode gets updated or there’s a macOS update?  You might have a need for the command line tools at that time but don’t need it any longer — however it seems to find a way to get back into your system.

I experienced this not too long ago.  I installed Xcode’s command line tools because I needed an older version of Xcode for use with NVidia’s toolkit.  Then when I no longer have the need, I’ve uninstalled it as per Apple’s directions.  But strangely during the next macOS update cycle — which also updated Xcode in roughly the same time frame — I was prompted to update the command line tools as well.  What’s worse, there’s no way to opt out and I ended up installing the command line tools only to un-install it again. What an annoyance and waste of bandwidth.

Fortunately after much, much googling, I’ve discovered a way to permanently uninstall this command line tools.  I’ve also find a way to leverage the Xcode IDE for use by command line applications as well. Interested? Read on.

Real Mac Developer

Uninstalling Xcode Command Line Tools

To remove Xcode Command Line Tools (which is, by the way, different than the full Xcode IDE), you will need to go through two steps.

  1. Remove the files consisting the command line tools.
  2. Remove the receipts to prevent it from coming back again.

The first step is easy enough. It’s the second step that’s rather tricky because these receipt files are located in a read-only zone guarded by System Integrity Protection (SIP).

Removing Xcode Command Line Tools

As per Apple’s document TN2339, removing the command line tools is as simple as deleting a folder.

sudo rm -rf /Library/Developer/CommandLineTools

However in my personal experience, the command line tools would just get installed again the next time it gets updated. Thus you will need to follow the next step to fully remove the command-line tools.

Removing Receipt Files

Another set of files that you would need to remove are the receipt files of Xcode command line tools.  These files starts with the name com.apple.pkg.CLTools* and resides in the /System/Library/Receipts/ folder.  However since its location is inside one of SIP’s protected folders, removing them is quite involved.

What you need to do is to boot into macOS Recovery and then remove those receipt files from there. There’s really no need to disable SIP and put your system at risk just to remove these few files.  Booting from the Recovery OS also means that no other applications are running when and further reduces the risk of any malware modifying your crucial system files.

Follow these steps to remove those pesky command line tools receipt files.

  1. Restart your mac and hold ⌘R to boot into macOS Recovery.
  2. Go through the language selection and select your preferred language.
  3. In the Recovery Tool, open Disk Utility
  4. Select your mac’s internal drive (usually named “Macintosh HD” if you haven’t renamed it) and then click mount.
  5. You probably need to select an administrative user and enter that user’s password to unlock the drive.
  6. Having mounted the internal drive, close the Disk Utility app.
  7. The Recovery Tool should appear again.
  8. This time click Utility option in the menu bar and then Terminal
  9. The Terminal app should open.
  10. Using the Terminal, locate your mounted internal drive in the /Volumes folder and list its files. Again, the name is likely “Macintosh HD”
    cd "/Volumes/Macintosh HD"
    ls
    
  11. You should see a familiar macOS boot volume folder structure.
  12. Then go to the System/Library/Receipts folder
    cd System/Library/Receipts
    
  13. Look for the command line tools receipts files.
    ls -l com.apple.pkg.CLTools*
    
  14. If they are there, remove them
    rm com.apple.pkg.CLTools*
    
  15. And that’s it. The command line tools shouldn’t return unless you explicitly downloaded and install them again.

Using the Full Xcode as Command-Line Tools

Really, the command line tools are meant for those who doesn’t need the full Xcode IDE.  Maybe they need it just for compiling open-source packages.  Perhaps they only do cross-platform Unix development and uses Emacs, VIM, or some other text editors to write code. For those people, the full Xcode is a monstrosity: gigabytes containing IDE and simulators for three operating systems (iOS, tvOS, watchOS) that they are not going to write software for.

But if you really use Xcode to develop Apple-specific software, then the reverse is true: the command-line tools duplicates of what the full Xcode IDE already provide and hence would waste precious space on your startup volume. However, some open-source package’s build systems seems to require the command line tools.  Is there a way to make them use the same compilers that Xcode IDE provide?

Yes there is. Read on.

The first step of business is to configure Xcode as the default compiler for the system.  This is even more important if you have more than one copy of Xcode installed (the other one would likely be a beta of a future version of Xcode). Follow these steps to configure Xcode in the command line.

  1. Open Xcode IDE
  2. In Xcode’s application menu, select the Preferences menu option.
  3. Activate the “Locations” tab
  4. In the “Command Line Tools” section, select the current version of Xcode as default (you might be asked to enter your password or restart iTunes if you haven’t done this before).
  5. Open Terminal
  6. Enter the following command and ensure that the output points to a location from within Xcode.app’s bundle:
    xcrun —show-sdk-path
    
Now that you have setuo Xcode as the default compiler, you might want to setup a few environment variables to let open-source tools know about it.  Edit your terminal session’s startup script – usually ~/.bash_profile and add these environment settings in.
xcode_path=$(xcrun --show-sdk-path)
export CFLAGS="-I${xcode_path}/usr/include"
export CPPFLAGS="-I${xcode_path}/usr/include"
export LDFLAGS="-L${xcode_path}/usr/lib"
unset xcode_path

Those variables would sets the header files needed to build C and C++ applications. Furthermore it also lets linkers know where to find static libraries for these languages.

And that’s just about it.  You’ve just saved about 200 megabytes of space on your startup volume. Space that hopefully you can find better use.

Until next time.



Do you enjoy this post? Enter your e-mail address below to receive articles like this one in your mailbox.
* indicates required

Discussion

No comments yet.

Leave a Reply

Free Updates!

Learn how to grow your indie business while keeping your day job.

Categories

Archives

Keep updated!

Don't miss out on new articles!