The "command not found" error for tools like Composer within a Jenkins Pipeline stems from a fundamental difference in how interactive user shells and non-interactive, non-login shells (like those used by Jenkins agents) manage environment variables, particularly the PATH.
1
Command Not FoundJenkins Pipeline fails
2
Incomplete PATHAgent environment lacks tool paths
3
Extend Pipeline PATHUse `environment` or `withEnv`
4
Verify Tool PathRun `which <command>` in pipeline
Root Cause Explanation:
When a developer logs into a server via SSH or opens a terminal, their interactive shell (e.g., Bash, Zsh) typically sources configuration files like .bashrc, .bash_profile, .profile, or files within /etc/profile.d/. These files are responsible for setting up the environment, including extending the PATH variable to include directories where user-installed or system-wide tools reside (e.g., /usr/local/bin, ~/.composer/vendor/bin, ~/.nvm/versions/node/vX.Y.Z/bin).
Jenkins agent processes, however, typically execute shell commands using a non-interactive, non-login shell. These shells generally do not source the same set of configuration files by default. Consequently, the PATH environment variable inherited by the Jenkins process is often a more minimal, default system PATH (e.g., /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin) which may not contain the specific directory where Composer (or other essential tools) is installed. Therefore, when sh 'composer install' is executed, the shell searches only the directories listed in its limited PATH and fails to locate the composer executable.
Solution:
To resolve this, the Jenkins Pipeline must explicitly ensure that the PATH environment variable includes the directory where Composer is installed. There are several effective ways to achieve this:
Using the environment Directive (Recommended for Pipeline-wide consistency):
This approach sets the PATH for the entire pipeline or specific stages. It's clean and makes the PATH available to all subsequent steps.
pipeline {
agent any
environment {
// Prepend the directory containing Composer to the existing PATH.
// IMPORTANT: Adjust '/usr/local/bin' to the actual installation path of Composer
// on your Jenkins agent. Common locations include:
// - /usr/local/bin (system-wide installation)
// - ~/.composer/vendor/bin (user-specific installation)
// - /snap/bin (if installed via Snap, e.g., on Ubuntu)
PATH = "/usr/local/bin:$PATH"
}
stages {
stage('Prepare Environment & Verify') {
steps {
script {
echo "Updated PATH for pipeline: ${env.PATH}"
// Verify Composer is now found and executable
sh 'which composer'
sh 'composer --version'
}
}
}
stage('Install Dependencies') {
steps {
sh 'composer install --no-dev --prefer-dist'
}
}
stage('Run Tests') {
steps {
sh 'php artisan test' // Example of another command now using Composer installed deps
}
}
}
}
```
Using withEnv (For Step-specific PATH modification):
If the PATH modification is only needed for a few specific steps, withEnv provides a scoped solution.
pipeline {
agent any
stages {
stage('Install Dependencies') {
steps {
withEnv(["PATH=/usr/local/bin:${env.PATH}"]) {
sh 'composer install --no-dev --prefer-dist'
}
}
}
}
}
```
This is less common for globally used tools like Composer, but useful for tools whose PATH should only apply to a specific context.
- Using Absolute Paths:
As a fallback, if the tool's location is absolutely fixed and known, you can always use its full path. This is generally less flexible and harder to maintain if paths change.
sh '/usr/local/bin/composer install --no-dev --prefer-dist'
```
Edge Cases and Related Pitfalls:
- Incorrect Tool Installation Path: The most common mistake is assuming the
PATH and guessing the location. Always verify the actual installation path on the agent using which composer while logged in as the Jenkins user (or a user with similar environment) or find / -name composer 2>/dev/null for a system-wide search.
- User Permissions: Even with the correct
PATH, the Jenkins agent user (e.g., jenkins) might lack execute permissions for the Composer binary or read/write permissions for project directories or Composer's cache. Ensure the jenkins user has appropriate rwx permissions.
- Dynamic
PATH Tools (e.g., nvm, phpbrew): If tools like Node Version Manager (nvm) or phpbrew are used to manage different tool versions, simply adding a fixed path to PATH won't work. These tools require sourcing their initialization scripts. For example, for nvm:
sh '''
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" # This loads nvm
nvm install 18 # Or nvm use 18
npm install
'''
```
This ensures nvm sets up the correct Node.js version and its associated npm in the PATH for that shell.
💡 Bonus Tip: For managing widely used tools like Node.js, Maven, JDK, or even custom executables, leverage Jenkins's Global Tool Configuration (under "Manage Jenkins" -> "Global Tool Configuration"). This allows you to define tool installations, and Jenkins will automatically download, install (if necessary), and add the correct binaries to the PATH for your pipeline steps using the tool directive (e.g., nodejs('Node 18.x') { sh 'npm install' }). This centralizes tool management, ensures consistent versions across agents, and simplifies Jenkinsfiles by abstracting explicit PATH manipulation.