Sub topic 3
Installing and verifying the Salesforce CLI
This chapter assumes you can run commands in a terminal, have VS Code installed, and know what Apex is. You will install the Salesforce CLI, confirm it works, and learn how the editor uses it.
What the CLI does in this setup
The Salesforce CLI is a command-line program named sf. The Salesforce extensions in VS Code do not talk to your org on their own. When you run a command such as SFDX: Deploy This Source to Org, the extension calls the CLI and shows you the output.
That makes the CLI the layer most likely to cause trouble. If a command fails in the editor, the same command usually fails in the terminal, and the terminal gives you clearer error messages.
Install the CLI
Use one of the installation methods from the Salesforce developer documentation. Pick one method and stick with it.
- Installer: download the installer for your operating system (Windows, macOS) and run it.
- npm: if you already manage Node.js, run
npm install --global @salesforce/cli. - Linux: use the tarball from the documentation, or npm.
After the install finishes, close and reopen your terminal and VS Code. Programs read the PATH variable when they start, so a window opened before the install cannot see the new command.
Verify the installation
Run these three commands. The first two check the editor and the CLI. The third checks Java, which a later chapter covers in detail.
code --version
sf --version
java -versionsf --version prints a line like @salesforce/cli/2.x.x along with your platform and Node version. The exact numbers will differ. If you see a version, the CLI is on your PATH.
You can also run sf doctor. It collects diagnostic information about your CLI, plugins, and environment. It is useful when you need to report a problem.
Keep the CLI current
Salesforce releases the CLI often. Check for updates with:
sf updateThis works for the installer-based CLI. If you installed with npm, update with npm install --global @salesforce/cli again, because sf update does not manage npm installs.
Know `sf` versus `sfdx`
Older tutorials use the sfdx command, for example sfdx force:source:deploy. That is the legacy v7 CLI syntax. Current work uses sf with commands in a topic-and-action style, such as sf project deploy start. The Salesforce extensions call sf in current releases.
| Task | Legacy `sfdx` | Current `sf` |
|---|---|---|
| Create a project | `sfdx force:project:create` | `sf project generate` |
| Authorize an org | `sfdx force:auth:web:login` | `sf org login web` |
| Deploy source | `sfdx force:source:deploy` | `sf project deploy start` |
If a command from an old blog post fails, look for the sf equivalent before debugging anything else.
Common errors
sf: command not found(or'sf' is not recognized): the terminal was opened before the install, or the install folder is not onPATH. Reopen the terminal. If that fails, add the CLI'sbinfolder toPATH.- VS Code cannot find the CLI but your terminal can: VS Code inherited an old environment. Quit VS Code fully and start it again.
- Two versions installed: if you used both the installer and npm,
sf --versionmay show a version you did not expect. Runwhich sf(macOS, Linux) orwhere sf(Windows) to see which one wins, then uninstall the other.
What comes next
The CLI is not connected to any org yet. Later chapters create a project with sf project generate and authorize an org with sf org login web. For now, you only need sf --version to print a version.
Exercise
You installed the CLI and ran sf --version in a terminal that was already open. The shell reports "command not found". What is the most likely cause, and what do you do first?
Show answer
The terminal read its PATH before the install, so it cannot see the new sf command. Close and reopen the terminal (and restart VS Code), then run sf --version again. If it still fails, check whether the CLI's bin folder is on PATH.
Exercise
A tutorial tells you to run sfdx force:source:deploy. What do you run with the current CLI, and how do you check where the sf command on your machine comes from?
Show answer
Use sf project deploy start. To see which sf executable runs, use which sf on macOS or Linux, or where sf on Windows.