chocolatey / choco

Chocolatey - the package manager for Windows
https://chocolatey.org
Other
10.04k stars 890 forks source link

Can't change default choco installation directory #3474

Open bharathmuppa opened 5 days ago

bharathmuppa commented 5 days ago

Checklist

What You Are Seeing?

Not able to install choco in AppData or in any other custom location.

What is Expected?

If i set environment variable CholateyInstall prior to the installation, Choco should be installed in specified location as official Docs

How Did You Get This To Happen?

  1. I ran the following script as Administrator using powershell -NoProfile -InputFormat None -ExecutionPolicy Bypass "C:\Users\****\automation\install-choco-env.ps1"
# Install Chocolatey
Write-Host "Installing Chocolatey" -ForegroundColor Cyan
try {
    # Ensure the script uses TLS 1.2 for secure downloads
    [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12
    $chocoPath = [System.Environment]::GetEnvironmentVariable('ChocolateyInstall', [System.EnvironmentVariableTarget]::Machine)

    Write-Host "check for chocolatey installation path: $chocoPath"

    # Download and execute the Chocolatey installation script
    Invoke-Expression ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1')) 
    Write-Host "Chocolatey installed successfully." -ForegroundColor Green

} catch {
    Write-Host "Failed to install Chocolatey. Please check your internet connection, firewall settings, and execution policy." -ForegroundColor Red
    exit
}
  1. Log shows this
    >  Chocolatey Installation path: C:\Users\****\AppData\Roaming\chocolatey
  2. But Choco is insalled in c:\ProgramData\chocolatey folder and also after the script i see the install.ps1 updates my environment varibale as well.

System Details

Installed Packages

N/A

Output Log

Installing Chocolatey
check for chocolatey installation path: C:\Users\****\AppData\Roaming\chocolatey
Forcing web requests to allow TLS v1.2 (Required for requests to Chocolatey.org)
Getting latest version of the Chocolatey package for download.
Not using proxy.
Getting Chocolatey from https://community.chocolatey.org/api/v2/package/chocolatey/2.3.0.
Downloading https://community.chocolatey.org/api/v2/package/chocolatey/2.3.0 to C:\Users\*****\AppData\Local\Temp\chocolatey\chocoInstall\chocolatey.zip
Not using proxy.
Extracting C:\Users\****\AppData\Local\Temp\chocolatey\chocoInstall\chocolatey.zip to C:\Users\****\AppData\Local\Temp\chocolatey\chocoInstall
Installing Chocolatey on the local machine
Creating ChocolateyInstall as an environment variable (targeting 'Machine')
  Setting ChocolateyInstall to 'C:\ProgramData\chocolatey'
WARNING: It's very likely you will need to close and reopen your shell
  before you can use choco.
Restricting write permissions to Administrators
We are setting up the Chocolatey package repository.
The packages themselves go to 'C:\ProgramData\chocolatey\lib'
  (i.e. C:\ProgramData\chocolatey\lib\yourPackageName).
A shim file for the command line goes to 'C:\ProgramData\chocolatey\bin'
  and points to an executable in 'C:\ProgramData\chocolatey\lib\yourPackageName'.

Creating Chocolatey CLI folders if they do not already exist.

chocolatey.nupkg file not installed in lib.
 Attempting to locate it from bootstrapper.
PATH environment variable does not have C:\ProgramData\chocolatey\bin in it. Adding...

Additional Context

No response

pauby commented 4 days ago

The logs say:

Creating ChocolateyInstall as an environment variable (targeting 'Machine') Setting ChocolateyInstall to 'C:\ProgramData\chocolatey'

Which indicates it's not finding the environment variable you created.

You didn't mention how you created the ChocolateyInstall environment variable and targetting which scope.

The documentation states:

Create a machine level (user level will also work) environment variable named ChocolateyInstall and set it to the folder you want Chocolatey to install to prior to installation (this environment variable must be set globally or available to PowerShell- it is not enough to simply make it available to your current command prompt session).

bharathmuppa commented 2 days ago

The logs say:

Creating ChocolateyInstall as an environment variable (targeting 'Machine') Setting ChocolateyInstall to 'C:\ProgramData\chocolatey'

Which indicates it's not finding the environment variable you created.

You didn't mention how you created the ChocolateyInstall environment variable and targetting which scope.

The documentation states:

Create a machine level (user level will also work) environment variable named ChocolateyInstall and set it to the folder you want Chocolatey to install to prior to installation (this environment variable must be set globally or available to PowerShell- it is not enough to simply make it available to your current command prompt session).

When I started, I created a single script where I changed the environment variables and then ran install.ps1 as you can see in the script. However, after digging deeper, I realized that the environment variable changes were updated in a process other than install.ps1. Therefore, I moved that logic to another file and ran it before executing install.ps1. I ran this new script first and then, after its successful execution, I ran install.ps1.

In point 2, I posted the log of me printing the path in the other PowerShell script before install.ps1 starts executing.

corbob commented 12 hours ago

@bharathmuppa, I've done some digging on this, and I think I can explain what's happening. It looks like you're setting the ChocolateyInstall environment variable at the machine level, which is what the docs suggest. However, it would seem then that you're not loading a new PowerShell process from Windows Explorer, so the environment variable is not being made available to the Chocolatey install script.

What I have done that I can confirm works:

$env:ChocolateyInstall = 'C:\ProgramData\choco'
Invoke-Expression ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))

In your script, if you update your environment variable check to use this: [System.Environment]::GetEnvironmentVariable('ChocolateyInstall', [System.EnvironmentVariableTarget]::Process) I suspect that you will not see it set in the process level which is where Chocolatey needs it to be set.

I've opened an issue on the docs repository here: https://github.com/chocolatey/docs/issues/1031 to get the docs updated to be more clear.