Can't I use CLI independently without Arduino IDE?

I made a simple code for a general Servo motor like below.

#include <Servo.h>

Servo myServo;

void setup() {
    myServo.attach(3);
    myServo.write(10);
}

void loop() {
}

When I tried to compile this with FQBN, arduino:avr:uno by CLI, the CLI will make an error like below because the CLI can't find a standard library for the FQBN.
fatal error: Servo.h: No such file or directory

But if the Arduino IDE is installed to my computer, the CLI compiles it normally with the installed path of Arduino IDE in preferences.txt. How can I solve it? I want to use the CLI independently without the Arduino IDE.

Hi @comseong.

The Arduino IDE installation comes with a collection of the most fundamental Arduino libraries so that beginners can get started doing cool things with Arduino right away instead of having to learn how to install libraries first.

As you discovered, Arduino CLI is able to find those libraries in the Arduino IDE installation via the information in the preferences.txt file. But if you don't have the IDE installed, it is easy enough to install any library you need via the arduino-cli lib install command.

In this case, you would run the following command:

arduino-cli lib install Servo

You can learn more about Arduino CLI's library management features by running this command:

arduino-cli lib --help

Thanks. I am pleased with that I can install it the standard library independently. I want to add one comment for someone. You can move the downloaded libraries can into /Arduino15/packages/arduino/hardware/avr/1.8.x/libraries and it works well though.

There are two problems you might encounter from doing that:

That location is for "platform bundled libraries". These libraries can only be used while you are compiling for a board of that platform. So it will work find when you are compiling for arduino:avr:uno, but if you try it for some other board like arduino:samd:nano_33_iot, you will get that "No such file or directory" error again.

The libraries you put there will be lost every time you update to a new version of the arduino:avr platform (i.e., arduino-cli core upgrade arduino:avr).

So if you want to manually install libraries, you will be better to put them under the libraries subfolder of your directories.user folder. If you don't know where that is, use the command arduino-cli config dump to see it. That is the same folder where arduino-cli lib install puts the libraries.

If you want to easily install all the libraries that are normally provided with the Arduino IDE installation, you can install the "Arduino_BuiltIn" library (arduino-cli lib install Arduino_BuiltIn). That library is just a container for the metadata that causes all the standard "built-in" libraries (e.g., Servo, Ethernet, Mouse) to be installed as dependencies.