Sub topic 2
Install the CLI and check the version
This chapter covers installing the Salesforce CLI, confirming that the sf command works, and fixing the problems that most often appear on a first install. It assumes you are comfortable in a terminal and know what an org, metadata, and Apex are. If you plan to install with npm, you also need Node.js.
What you are installing
The Salesforce CLI is a single command-line program. Its executable is named sf. It talks to Salesforce orgs over their APIs, so installing it does not require an org. You only need an org later, when you log in.
The CLI comes in two forms:
sf(version 2): the current CLI. This is the one the rest of this tutorial uses.sfdx(version 7 and earlier): the older CLI. It uses colon-separated commands such assfdx force:source:deploy. Salesforce has stopped developing it and recommends moving tosf.
Both programs can exist on the same machine. If you installed Salesforce tooling years ago, you may have sfdx already and not sf. The installers and npm package for the current CLI are named @salesforce/cli. The older npm package, sfdx-cli, is a different thing. Installing the wrong package is a common cause of "command not found" errors for sf.
Choose an installation method
You have three common options.
| Method | Best for | Updates | Needs Node.js |
|---|---|---|---|
| npm global install | Developers who already use Node.js | `npm install --global @salesforce/cli@latest` or `sf update` | Yes |
| OS installer (macOS `.pkg`, Windows `.exe`, Linux tarball) | Machines without Node.js | `sf update` | No, the installer bundles its own |
| Container or CI image | Automated pipelines | Rebuild the image | Depends on the image |
The OS installers bundle their own copy of Node.js. That is useful when you do not want to manage Node.js yourself. The downside is that the CLI then runs on the bundled Node.js version, not the one on your path.
The npm method uses whatever Node.js version is installed. The CLI supports the active long-term-support (LTS) releases of Node.js. If your Node.js version is very old, the install can fail or the CLI can misbehave. Check it first:
node --version
npm --versionIf node is not found, install the current LTS release from the Node.js website or through a version manager such as nvm.
Install with npm
The following commands install the CLI globally and print its version:
npm install --global @salesforce/cli
sf --version--global places the sf executable on your path so you can run it from any directory. Without it, npm installs the package into the current folder, and sf will not be found elsewhere.
Permission errors on macOS and Linux
On some systems, a global npm install fails with EACCES: permission denied. This happens when npm's global directory is owned by root. Avoid fixing it with sudo npm install --global. Running npm as root leaves root-owned files in your home directory and can cause more permission errors later.
Better options:
- Use a Node.js version manager such as
nvm, which installs Node.js and global packages under your home directory. - Or configure npm to use a global directory you own, then add that directory's
binfolder to yourPATH.
The second option looks like this:
mkdir -p "$HOME/.npm-global"
npm config set prefix "$HOME/.npm-global"
export PATH="$HOME/.npm-global/bin:$PATH"
npm install --global @salesforce/cliThe export line only affects the current shell session. Add it to your shell profile, such as ~/.bashrc or ~/.zshrc, to make it permanent.
Install with an OS installer
On macOS and Windows, download the installer from the Salesforce CLI page and run it. On Linux, you can download the tarball and unpack it into a directory on your path. After the installer finishes, open a new terminal window. Terminals read PATH when they start, so a window opened before the install may not see the new sf command.
Verify the installation
Run the version command:
sf --versionA successful result is one line that names the CLI package, your operating system and architecture, and the Node.js version. It looks similar to this, though your numbers will differ:
@salesforce/cli/2.x.x linux-x64 node-v20.x.xRead each part:
@salesforce/cli/2.x.xis the CLI version.linux-x64is the platform and CPU architecture.node-v20.x.xis the Node.js runtime the CLI is using.
The last part matters when you install with an OS installer. It shows the bundled Node.js version, which can differ from the output of node --version.
For more detail, use sf version --verbose. It adds the list of installed plugins and their versions. That output is what Salesforce Support or a bug report will ask for.
You can also confirm where the command lives:
which sfOn Windows PowerShell, use Get-Command sf instead. If this prints a path you did not expect, such as one inside an old project folder, another copy is shadowing the one you meant to use.
How the shell finds `sf`
When you type sf, your shell searches each directory in PATH, in order, and runs the first match. Most install problems come from this lookup.
That placeholder is not valid, so here is the real diagram of the lookup.
Common installation problems
| Symptom | Likely cause | Fix |
|---|---|---|
| `sf: command not found` right after install | Terminal was opened before the install, or the npm global `bin` folder is not on `PATH` | Open a new terminal. Run `npm config get prefix` and add that prefix's `bin` folder to `PATH`. |
| `EACCES: permission denied` during npm install | npm's global directory is owned by root | Use `nvm` or set a user-owned npm prefix. Do not use `sudo`. |
| `sfdx` works but `sf` does not | Only the older CLI is installed | Install `@salesforce/cli`. |
| Version shows an older number than expected | Two copies are installed and the older one comes first on `PATH` | Run `which sf` and remove or reorder the older copy. |
| Install fails with engine or syntax errors | Node.js version is too old | Upgrade to a current LTS release of Node.js. |
| `sf` is not recognized on Windows | The `PATH` change has not reached the current window | Close and reopen PowerShell or Command Prompt. |
Duplicate installs
It is easy to end up with the CLI installed twice, for example once with npm and once with an installer. Each copy keeps its own plugins and may update separately. sf update only updates installers-based copies, not npm copies. If you installed with npm, update with npm. Pick one method and remove the other to avoid confusing results. Chapter 13 returns to updates and plugins.
Uninstall an npm copy with:
npm uninstall --global @salesforce/cliFor installer-based copies, use your operating system's normal uninstall process.
Check that the CLI can see its commands
A version number proves the program starts. A second check proves it loads its command set:
sf --helpThis prints the list of topics, such as org, project, data, and apex. If the list appears, the installation is complete. The next chapter explains how those topics and commands fit together.
Summary of the verification steps
- Run
node --versionif you plan to use npm. - Install with
npm install --global @salesforce/cli, or use an OS installer. - Open a new terminal window.
- Run
sf --versionand read the CLI, platform, and Node.js values. - Run
which sf(orGet-Command sf) to confirm which copy runs. - Run
sf --helpto confirm the command list loads.
Exercise
You installed the CLI with npm and sf --version prints command not found, but npm list --global @salesforce/cli shows the package is installed. What is the most likely cause, and how do you check it?
Show answer
The directory where npm puts global executables is not on your PATH. Run npm config get prefix to find the prefix, then check that its bin folder (on macOS and Linux) appears in echo $PATH. Add it to your shell profile and open a new terminal.
Exercise
sf --version shows an older CLI version than the one you just installed. Which command helps you find the cause, and what is the usual explanation?
Show answer
Run which sf (or Get-Command sf on PowerShell). It shows which executable runs. The usual explanation is a second, older copy of the CLI earlier in PATH, for example from an OS installer. Remove or reorder one copy.