Sub topic 3
Installing and verifying the Salesforce CLI
The Salesforce CLI is the program that does the real work behind most Salesforce commands in VS Code. This chapter covers how to install it, check that it works, and keep it current. It also covers the version and PATH problems that most often break the setup later.
This chapter assumes you have VS Code installed, can run commands in a terminal, and know what a Salesforce org is. You don't need a Java JDK or the extensions yet. They come in the next two chapters.
What the CLI does in this setup
The command is sf. It is a command-line program that talks to Salesforce APIs for you. Its main jobs here are:
- Creating projects (
sf project generate). - Authorizing orgs (
sf org login web). - Deploying and retrieving source (
sf project deploy start,sf project retrieve start). - Running tests and anonymous Apex (
sf apex run test,sf apex run).
The Salesforce extensions for VS Code mostly wrap these commands. When you run "SFDX: Deploy This Source to Org" from the Command Palette, the extension builds an sf command and runs it in the background. If the CLI is missing or broken, many editor commands fail. Installing and checking the CLI first makes later problems easier to trace.
The extensions find sf through your PATH. The PATH is the list of folders your operating system searches when you type a command name. Most CLI installation problems are PATH problems.
Choosing an install method
There are three common ways to install the CLI. They produce the same sf command, but they differ in how updates work.
| Method | Best for | Updates | Notes |
|---|---|---|---|
| OS installer (macOS `.pkg`, Windows `.exe`) | Most developers | `sf update` | Bundles its own Node.js runtime. |
| Tarball (Linux, or manual installs) | Servers, containers, Linux desktops | `sf update` | You extract it and add it to `PATH` yourself. |
| npm package `@salesforce/cli` | Teams that already manage Node.js | `npm install --global @salesforce/cli@latest` | Uses the Node.js you installed. |
If you have no strong preference, use the OS installer. It needs no separate Node.js installation, and sf update works without extra steps.
Download installers from the official Salesforce CLI page on developer.salesforce.com. On macOS, pick the build that matches your processor: arm64 for Apple silicon, x64 for Intel. Running the wrong build is a common cause of slow or failing commands.
Installing with npm
If you manage Node.js yourself, use a current LTS release of Node.js, then run:
npm install --global @salesforce/cliAvoid sudo with npm. If a global install needs sudo, fix your npm prefix or use a Node version manager instead. Root-owned global folders cause permission errors on later updates.
With an npm install, update through npm, not sf update. Mixing the two update paths leaves you unsure which copy of sf is running.
Removing the old CLI first
Before 2023, Salesforce shipped a CLI with the command sfdx as the package sfdx-cli. The current CLI is sf. It also understands the older sfdx command names, so existing scripts mostly keep working. Having both packages installed at once causes confusion about which one runs.
If you installed the old one with npm, remove it before installing the new one:
npm uninstall --global sfdx-cliIf you installed it with an OS installer, uninstall it using your operating system's usual method (Apps & Features on Windows, deleting the app files on macOS) and then install the current CLI.
Verifying the installation
Open a new terminal window after installing. A terminal that was already open may still have the old PATH. Then run the three version checks from the topic plan:
code --version
sf --version
java -versionAt this point java -version may fail, because you haven't installed a JDK yet. That is expected. Focus on the sf output. It looks something like this:
@salesforce/cli/2.x.x linux-x64 node-v22.x.xThe output has three parts:
- The CLI package and version.
- The platform and architecture (
darwin-arm64,win32-x64,linux-x64). - The Node.js version the CLI is using.
Your numbers will differ. The platform value is worth checking. If you are on Apple silicon and see darwin-x64, you installed the Intel build.
Looking deeper
The basic version check proves the command runs. These commands show more:
sf version --verbose
sf plugins
sf --helpsf version --verboselists the installed plugins and their versions.sf pluginslists the plugins you added or that ship with the CLI.sf --helplists the top-level command groups, such asorg,project, andapex.
You can also ask for help on any command. For example, sf project generate --help lists the flags for creating a project, with examples. Help output always matches your installed version, so it is more reliable than a web page written for a different release.
Finding which copy of `sf` runs
If the version looks wrong, find out which file your shell runs.
On macOS and Linux:
which -a sfOn Windows PowerShell:
Get-Command sf -AllThe first result is the one that runs. If an old npm copy or a stale install appears first, remove it or reorder your PATH.
Running the doctor
The CLI includes a diagnostic command:
sf doctorIt collects information about your environment, such as the CLI version, Node.js, plugins, and configuration, and writes a diagnostic report to a file. You will want it when you ask for help or file a bug, because it gathers the details a support engineer would otherwise ask you for one by one. Read the report before sharing it, in case it contains information you consider private.
Keeping the CLI current
Salesforce releases CLI updates frequently, and Salesforce releases new API versions three times a year. The CLI needs to be recent enough to understand the API version in your sfdx-project.json. If you set sourceApiVersion to a version your CLI doesn't know, deployments can fail or warn.
For installer and tarball installs, update in place:
sf updateFor npm installs:
npm install --global @salesforce/cli@latestYou can check the version before and after. In a team, agree on a minimum CLI version, because different versions can handle source metadata differently and produce noisy diffs.
The CLI can also update itself automatically in the background for installer-based setups. In a locked-down environment such as a build server, you may want pinned versions instead. Pin the version in your container image or use a fixed npm version, so that a build doesn't change under you.
npm install --global @salesforce/[email protected]Replace the version with the one your team has tested.
Configuration the CLI stores
The CLI keeps two kinds of configuration:
- Global settings apply to every project for your user.
- Local settings apply only to the current project, and the CLI stores them in a
.sffolder inside it.
You'll use this when you set a default org in a later chapter. To see what is set now:
sf config listOn a fresh install, this may report that no configuration is set. That is normal.
Authorized org credentials are stored in a .sfdx folder in your home directory, not inside the project. Treat that folder like a password store. Don't copy it into a repository, a shared drive, or a container image.
The .sf and .sfdx folders inside a project should not be committed. The project template's .gitignore handles this for you.
Common problems
| Symptom | Likely cause | Fix |
|---|---|---|
| `sf: command not found` or "not recognized" | The install folder is not on `PATH`, or the terminal is old | Open a new terminal. If it still fails, add the CLI's `bin` folder to `PATH`. |
| Works in a terminal, fails in VS Code | VS Code started before the install and kept the old `PATH` | Quit VS Code completely and reopen it. |
| `sf --version` shows an unexpected version | Two copies installed | Use `which -a sf` or `Get-Command sf -All`, then remove the extra copy. |
| `EACCES` permission error during npm install | Global npm folder owned by root | Fix the npm prefix. Don't use `sudo`. |
| Slow commands on a Mac | Intel build running on Apple silicon | Reinstall the `arm64` build. |
| Update fails on a corporate network | Proxy or certificate interception | Set the proxy environment variables your IT team documents, such as `HTTPS_PROXY`. |
The second row deserves attention. VS Code reads your environment when it starts. Closing only the window may leave the app running in the background on some systems, so quit it fully.
Checklist before moving on
Before the next chapter, confirm these points:
sf --versionprints a version in a newly opened terminal.which -a sf(orGet-Command sf -All) shows only the copy you intended.sf --helplists command groups such asorg,project, andapex.- You know how you installed the CLI, so you know which update command to use.
Exercise
You installed the CLI, and sf --version works in your terminal. In VS Code, the Salesforce extension reports that it can't find sf. List two actions you would take, in order, and explain why the first one often solves it.
Show answer
First, quit VS Code completely and reopen it. VS Code reads the PATH when it starts, so a copy opened before the install doesn't see the new sf. If the problem remains, run which -a sf (or Get-Command sf -All on Windows) in a terminal to confirm where sf lives and whether another copy is shadowing it, then fix the PATH or remove the extra copy.
Exercise
You installed @salesforce/cli with npm. A teammate tells you to run sf update. Which command should you use to update, and why?
Show answer
Use npm install --global @salesforce/cli@latest. An npm install is managed by npm, so updating it through npm keeps one clear update path. Running sf update is for installer and tarball installs, and mixing the two can leave more than one copy of sf on your system.