ChatGPT解决这个技术问题 Extra ChatGPT

How to check if command line tools is installed

I have a macbook pro with OS X 10.8.2. XCode is installed. I know this as it appears in the Applications directory. There are also the xcodebuild and xcode-select files in /usr/bin I need to know if the command line tools is installed. Is there a command for it? What can I do to see if XCode CLT is installed and if yes to find the version installed?

The version of XCode is 4.6

I
Ian

10.15 Catalina Update:

See Yosemite Update.

10.14 Mojave Update:

See Yosemite Update.

10.13 High Sierra Update:

See Yosemite Update.

10.12 Sierra Update:

See Yosemite Update.

10.11 El Capitan Update:

See Yosemite Update.

10.10 Yosemite Update:

Just enter in gcc or make on the command line! OSX will know that you do not have the command line tools and prompt you to install them!

To check if they exist, xcode-select -p will print the directory. Alternatively, the return value will be 2 if they do NOT exist, and 0 if they do. To just print the return value (thanks @Andy):

xcode-select -p 1>/dev/null;echo $?

10.9 Mavericks Update:

Use pkgutil --pkg-info=com.apple.pkg.CLTools_Executables

10.8 Update:

Option 1: Rob Napier suggested to use pkgutil --pkg-info=com.apple.pkg.DeveloperToolsCLI, which is probably cleaner.

Option 2: Check inside /var/db/receipts/com.apple.pkg.DeveloperToolsCLI.plist for a reference to com.apple.pkg.DeveloperToolsCLI and it will list the version 4.5.0.

[Mar 12 17:04] [jnovack@yourmom ~]$ defaults read /var/db/receipts/com.apple.pkg.DeveloperToolsCLI.plist
{
    InstallDate = "2012-12-26 22:45:54 +0000";
    InstallPrefixPath = "/";
    InstallProcessName = Xcode;
    PackageFileName = "DeveloperToolsCLI.pkg";
    PackageGroups =     (
        "com.apple.FindSystemFiles.pkg-group",
        "com.apple.DevToolsBoth.pkg-group",
        "com.apple.DevToolsNonRelocatableShared.pkg-group"
    );
    PackageIdentifier = "com.apple.pkg.DeveloperToolsCLI";
    PackageVersion = "4.5.0.0.1.1249367152";
    PathACLs =     {
        Library = "!#acl 1\\ngroup:ABCDEFAB-CDEF-ABCD-EFAB-CDEF0000000C:everyone:12:deny:delete\\n";
        System = "!#acl 1\\ngroup:ABCDEFAB-CDEF-ABCD-EFAB-CDEF0000000C:everyone:12:deny:delete\\n";
    };
}

I found that opening xcode>Preferences>Downloads>Components had a Command Line Tools entry which was in the update state and when I updated it, it shows the status as installed
Rather than reaching into the /var/db/receipts directory, you can use the API for it: pkgutil --pkg-info=com.apple.pkg.DeveloperToolsCLI
I might be mistaken, but the receipt name is now CLTools_Executables at least that's all I find under OS Mavericks. The command would then be: pkgutil --pkg-info=com.apple.pkg.CLTools_Executables
noticed same thing as Jordan. the xcode-select doesn't return an int, just the path
I entered xcode-select-p, only to get /Applications/Xcode.app/Contents/Developer. What does it mean?
p
phyatt

Yosemite

Below are a few extra steps on a fresh Mac that some people might need. This adds a little to @jnovack's excellent answer.

Update: A few other notes when setting this up:

Make sure your admin user has a password. A blank password won't work when trying to enable a root user.

System Preferences > Users and Groups > (select user) > Change password

Then to enable root, run dsenableroot in a terminal:

$ dsenableroot
username = mac_admin_user
user password:
root password:
verify root password:

dsenableroot:: ***Successfully enabled root user.

Type in the admin user's password, then the new enabled root password twice.

Next type:

sudo gcc

or

sudo make

It will respond with something like the following:

WARNING: Improper use of the sudo command could lead to data loss
or the deletion of important system files. Please double-check your
typing when using sudo. Type "man sudo" for more information.

To proceed, enter your password, or type Ctrl-C to abort.

Password:


You have not agreed to the Xcode license agreements. You must agree to 
both license agreements below in order to use Xcode.

Press enter when it prompts to show you the license agreement.

Hit the Enter key to view the license agreements at 
'/Applications/Xcode.app/Contents/Resources/English.lproj/License.rtf'

IMPORTANT: BY USING THIS SOFTWARE, YOU ARE AGREEING TO BE BOUND BY THE 
FOLLOWING APPLE TERMS:

//...

Press q to exit the license agreement view.

By typing 'agree' you are agreeing to the terms of the software license 
agreements. Type 'print' to print them or anything else to cancel, 
[agree, print, cancel]

Type agree. And then it will end with:

clang: error: no input files 

Which basically means that you didn't give make or gcc any input files.

Here is what the check looked like:

$ xcode-select -p
/Applications/Xcode.app/Contents/Developer

Mavericks

With Mavericks, it is a little different now.

When the tools were NOT found, this is what the command pkgutil command returned:

$ pkgutil --pkg-info=com.apple.pkg.CLTools_Executables
No receipt for 'com.apple.pkg.CLTools_Executables' found at '/'.

To install the command line tools, this works nicely from the Terminal, with a nice gui and everything.

$ xcode-select --install

http://macops.ca/installing-command-line-tools-automatically-on-mavericks/

When they were found, this is what the pkgutil command returned:

$ pkgutil --pkg-info=com.apple.pkg.CLTools_Executables
package-id: com.apple.pkg.CLTools_Executables
version: 5.0.1.0.1.1382131676
volume: /
location: /
install-time: 1384149984
groups: com.apple.FindSystemFiles.pkg-group com.apple.DevToolsBoth.pkg-group com.apple.DevToolsNonRelocatableShared.pkg-group 

This command returned the same before and after the install.

$ pkgutil --pkg-info=com.apple.pkg.DeveloperToolsCLI
No receipt for 'com.apple.pkg.DeveloperToolsCLI' found at '/'.

Also I had the component for the CLT selected and installed in xcode's downloads section before, but it seems like it didn't make it to the terminal...

Hope that helps.


Can you update this answer for the latest operating systems? Thanks.
I called sudo rm -rf /Library/Developer/CommandLineTools and then xcode-select --install.
@study You got /Applications/Xcode.app/Contents/Developer on running xcode-select-p because that is how the path is in Mac Sierra.
u
user3476766

To check if command line tools are installed run:

xcode-select --version

// if installed you will see the below with the version found in your system
// xcode-select version 1234.

If command line tools are not installed run:

xcode-select --install

Simple and effective. Might be the best answer.
Saved a lot of time. Thanks mate!
P
Paul F. Wood

In macOS Catalina, and possibly some earlier versions, you can find out where the command line tools are installed using:

xcode-select -p a.k.a. xcode-select --print-path

Which will, if it is installed, respond with something like:

/Library/Developer/CommandLineTools

To find out which version you have installed there, you can use:

xcode-select -v a.k.a. xcode-select --version

Which will return something like:

xcode-select version 2370.

However, if you attempt to upgrade it to the latest version, assuming it is installed, using this:

xcode-select --install

You will receive in response:

xcode-select: error: command line tools are already installed, use "Software Update" to install updates

Which rather erroneously gives the impression you need to use Spotlight find something called 'Software Update'. In actual fact, you need to continue in the Terminal, and use this:

softwareupdate -i -a a.k.a. softwareupdate --install --all

Which tries to update everything it can and may well respond with:

Software Update Tool

Finding available software
No new software available.

To find out which versions of the different Apple SDKs are installed on your machine, use this:

xcodebuild -showsdks


s
sahilabrar

On macOS Sierra (10.12) :

Run the following command to see if CLT is installed: xcode-select -p this will return the path to the tool if CLT is already installed. Something like this - /Applications/Xcode.app/Contents/Developer Run the following command to see the version of CLT: pkgutil --pkg-info=com.apple.pkg.CLTools_Executables this will return version info, output will be something like this - package-id: com.apple.pkg.CLTools_Executables version: 8.2.0.0.1.1480973914 volume: / location: / install-time: 1486372375


J
Jagdish Adusumalli

I think the simplest way which worked for me to find Command line tools is installed or not and its version irrespective of what macOS version is

$brew config

macOS: 10.14.2-x86_64 CLT: 10.1.0.0.1.1539992718 Xcode: 10.1

This when you have Command Line tools properly installed and paths set properly.

Earlier i got output as below macOS: 10.14.2-x86_64 CLT: N/A Xcode: 10.1

CLT was shown as N/A in spite of having gcc and make working fine and below outputs

$xcode-select -p              
/Applications/Xcode.app/Contents/Developer
$pkgutil --pkg-info=com.apple.pkg.CLTools_Executables
No receipt for 'com.apple.pkg.CLTools_Executables' found at '/'.
$brew doctor
Your system is ready to brew.

Finally doing xcode-select --install resolved my issue of brew unable to find CLT for installing packages as below.

Installing sphinx-doc dependency: python
Warning: Building python from source:
  The bottle needs the Apple Command Line Tools to be installed.
  You can install them, if desired, with:
    xcode-select --install

Clear and simple one. Much appreciated.
B
Bruno

Go to Applications > Xcode > preferences > downloads

You should see the command line tools there for you to install.


A
Anon

From a programmatic perspective the Homebrew folks have a check for the existence of various files to determine if the command line tools are installed. Currently it always checks for /Library/Developer/CommandLineTools/usr/bin/git and will also check for /usr/include/iconv.h if the OS version is 10.13 or below.


G
Gonçalo

to use on a bash script:

if ! command -v xcode-select &> /dev/null
then
    echo "\t xcode was not found! You need to manually install it :/ "
else
    echo "xcode is installed, checking other IOS build tools"
fi

B
BaseZen

Because Xcode subsumes the CLI tools if installed first, I use the following hybrid which has been validated on 10.12 and 10.14. I expect it works on a lot of other versions:

installed=$(pkgutil --pkg-info=com.apple.pkg.CLTools_Executables 2>/dev/null || pkgutil --pkg-info=com.apple.pkg.Xcode)

Salt with awk to taste for branching logic.

Of course xcode-select -p handles the variations with a really short command but fails to give the detailed package, version, and installation date metadata.


Z
ZaMy

Open your terminal and check to see if you have Xcode installed already with this:

xcode-select -p

in return, if you get this:

/Library/Developer/CommandLineTools

That means you have that Xcode is installed.

Another way you can check would you if you have "HomeBrew" installed you can use the following command to see if you have Xcode and the version:

brew config

And finally, if you don't have the Xcode follow this link to download the Xcode from the Appstore. Xcode from the App Store.

Good Luck.


O
Old Sub Sailor

% xcode-select -h Usage: xcode-select [options]

Print or change the path to the active developer directory. This directory controls which tools are used for the Xcode command line tools (for example, xcodebuild) as well as the BSD development commands (such as cc and make).

Options: -h, --help print this help message and exit -p, --print-path print the path of the active developer directory -s , --switch set the path for the active developer directory --install open a dialog for installation of the command line developer tools -v, --version print the xcode-select version -r, --reset reset to the default command line tools path