Sub topic 5
Checking your Salesforce CLI installation
Every later chapter in this tutorial runs sf commands: authorizing the Dev Hub, creating scratch orgs, deploying source. This chapter confirms that the CLI is installed, current, and able to run the commands you need. It also covers the usual installation problems.
Prerequisites
This chapter assumes you can do the following:
- Open a terminal (Terminal on macOS, a shell on Linux, PowerShell or Windows Terminal on Windows) and run commands.
- Read basic JSON, because several CLI commands can print JSON.
- Install software on your machine, or ask someone who can.
You do not need a Salesforce org yet. Nothing in this chapter contacts Salesforce, apart from the optional update check.
What `sf` is
sf is the command-line interface for Salesforce. It is the current generation of the Salesforce CLI (also called "CLI v2"). It replaced the older sfdx executable, which used commands such as sfdx force:org:create. The two differ in command names and flags.
| Older `sfdx` style | Current `sf` style |
|---|---|
| `sfdx force:auth:web:login` | `sf org login web` |
| `sfdx force:org:create` | `sf org create scratch` |
| `sfdx force:source:push` | `sf project deploy start` |
| `-u` / `--targetusername` | `-o` / `--target-org` |
| `-v` / `--targetdevhubusername` | `-v` / `--target-dev-hub` |
If you find a blog post or script that uses force: commands, it targets the older syntax. Look up the sf equivalent before you copy it.
Checking the version
Run:
sf --versionA working installation prints one line, similar to this:
@salesforce/cli/2.x.y linux-x64 node-v20.x.yThe line has three parts:
- The package name and CLI version, for example
@salesforce/cli/2.x.y. - The operating system and CPU architecture, for example
linux-x64ordarwin-arm64. - The Node.js version bundled with, or used by, the CLI.
The exact numbers on your machine will differ. What matters is that the command succeeds and that the first part begins with @salesforce/cli/.
For more detail, run:
sf version --verboseThis adds a list of installed plugins and their versions. Include this output when you ask for help, because many problems depend on a specific plugin version.
Which installation method you used matters
The CLI can be installed in several ways. Each has different rules for updating.
| Method | How you update | Notes |
|---|---|---|
| Platform installer (macOS `.pkg`, Windows `.exe`) | `sf update`, or run a newer installer | Bundles its own Node.js. |
| Tarball (Linux or manual) | `sf update` | Common in containers and CI images. |
| npm (`npm install --global @salesforce/cli`) | `npm update --global @salesforce/cli` | Uses the Node.js already on your machine. |
Find out which one you have, because sf update only works for the installer and tarball methods. An npm installation reports an error if you try sf update. Use npm to update it instead.
To see where the executable lives, use the command for your shell:
which sfOn Windows PowerShell, use Get-Command sf. A path inside a global npm directory means npm installed it. A path such as /usr/local/bin/sf or C:\Program Files\sf\bin usually means an installer or tarball.
Checking for updates
Salesforce releases the CLI weekly. A stale CLI can lack newer scratch org features or lack fixes for bugs you hit. Update with:
sf updateIf you are on a release channel other than stable, you can name the channel:
sf update stableThe stable channel is the right choice for this tutorial. The stable-rc channel gets the next release early and is for testing only.
Listing the commands you will use
You can confirm that the commands from later chapters exist without running them. Ask for help on the command group:
sf org --helpLook for login web, create scratch, open, display, list, and delete scratch in the output. Then check the project commands:
sf project --helpYou should see generate, deploy start, and retrieve start. If a command is missing, your CLI is probably old, or a plugin is missing.
Every command also has its own help. Use it whenever you are unsure of a flag:
sf org create scratch --helpThe --help output lists each flag, whether it is required, and its short form. This is the most reliable source for your installed version, because it matches the code you will actually run.
Plugins
The CLI is built from plugins. The core ones for this tutorial ship with the standard installation, so you normally do not install anything extra. List the installed plugins:
sf plugins --coreTo see everything, including plugins you added yourself:
sf pluginsPlugins you install can be out of date even when the CLI itself is current. Update them with:
sf plugins updateA common source of odd behavior is an old, manually installed plugin that overrides a core command. If a command behaves differently from the documentation, check sf plugins for unfamiliar entries.
Checking the machine-readable output
Many sf commands accept --json. This is how scripts and CI jobs read results. It is worth testing now, so you recognize the shape later:
sf version --jsonThe output is a JSON object with a status field and a result field:
statusis0when the command succeeds.resultholds the command's data.- On failure, the object also carries an error
nameandmessage, and the process exit code is non-zero.
The last point matters in automation. A shell script can test the exit code with $?, and CI systems fail the step automatically when it is non-zero.
Diagnosing the installation with `sf doctor`
The CLI has a built-in diagnostic command:
sf doctorIt collects information about your CLI, plugins, environment variables, and system, and writes the results to a diagnostics file. You can attach that file when reporting a problem. For a quick check of one command that fails, run it with debug logging:
SF_LOG_LEVEL=debug sf org listOn Windows PowerShell, set the variable first:
$env:SF_LOG_LEVEL = "debug"
sf org listThe CLI writes log files to a .sf directory in your home folder. Those logs can contain org details such as usernames and instance URLs. Review them before you share them publicly.
How the checks fit together
The sequence below is the order to follow. Stop at the first failing step and fix it before moving on.
Common problems
`sf: command not found`
The shell cannot find the executable. Either the CLI is not installed or its directory is not on your PATH. Open a new terminal window after installing, because existing windows keep the old PATH. If it still fails, locate the install directory and add it to PATH in your shell profile.
`sf` works, but `sfdx` commands are expected by a script
Older scripts call sfdx. The new CLI still provides an sfdx executable that accepts many old commands, but this support is for migration and may print deprecation warnings. Update the script to sf commands rather than depending on it.
Permission errors during `npm install --global`
On Linux and macOS, installing into a system npm directory can fail with EACCES. Do not fix this with sudo npm install. Configure npm to use a directory you own, or use the platform installer or tarball instead. Running npm with sudo can leave root-owned files in your home directory that break later updates.
Two installations on one machine
You can end up with both an installer copy and an npm copy. The one that runs is whichever comes first on PATH. If sf update seems to have no effect, list all copies:
which -a sfOn Windows, use where.exe sf. Remove the extra copy so that updates apply to the one you use.
Corporate proxy or network blocks
Updates and plugin installs download from the internet. Behind a proxy, set the standard proxy variables (HTTPS_PROXY, and NO_PROXY for hosts that should bypass it) before running sf update. If your organization uses a TLS-inspecting proxy, you may also need to provide its certificate through NODE_EXTRA_CA_CERTS. Ask your network team for the correct values.
Node.js version mismatch (npm installations only)
The npm installation uses the Node.js on your machine, and the CLI supports only certain Node.js versions. If sf --version fails with a message about an unsupported Node.js version, install a supported Node.js version (an active long-term support release is the safe choice) and run the command again.
Summary checklist
Before the next chapter, confirm that all of the following are true:
sf --versionprints a version line.- You know which installation method you used and how to update it.
sf org --helplistslogin web,create scratch,open,display,list, anddelete scratch.sf project --helplistsgenerate,deploy start, andretrieve start.sf plugins --coreruns without errors.
Exercise
Your teammate runs sf update and gets an error, although sf --version works. Which installation method most likely produced their CLI, and how should they update it?
Show answer
They most likely installed it with npm. sf update only works for the platform installer and tarball installations. They should run npm update --global @salesforce/cli, or confirm the method first with which sf (or Get-Command sf on PowerShell) and check whether the path is inside a global npm directory.
Exercise
After installing the CLI, sf --version still prints "command not found" in your current terminal window. Name two likely causes and the first thing to try.
Show answer
Either the CLI is not installed, or its directory is not on PATH. Existing terminal windows keep the old PATH, so the first thing to try is opening a new terminal window and running sf --version again. If it still fails, find the install directory and add it to PATH in your shell profile.
Exercise
Which command would you give a script so that it reads the CLI version as structured data, and which field tells the script that the command succeeded?
Show answer
sf version --json. The status field is 0 on success, and the process exit code is also 0. On failure the exit code is non-zero.