Can run cargo on a terminal but not inside a terminal within vs code


When I open a terminal within vs code I see this :

roelof@DESKTOP-6H677GO:~/exercism/rust/hello-world$ cargo

Command 'cargo' not found, but can be installed with:

sudo apt install cargo

but when I open the ubuntu terminal on Windows 10 I see this :

roelof@DESKTOP-6H677GO:~/exercism/rust/hello-world$ cargo
Rust's package manager


    -V, --version           Print version info and exit
        --list              List installed commands
        --explain <CODE>    Run `rustc --explain CODE`
    -v, --verbose           Use verbose output (-vv very verbose/ output)
    -q, --quiet             No output printed to stdout
        --color <WHEN>      Coloring: auto, always, never
        --frozen            Require Cargo.lock and cache are up to date
        --locked            Require Cargo.lock is up to date
        --offline           Run without accessing the network
    -Z <FLAG>...            Unstable (nightly-only) flags to Cargo, see 'cargo -Z help' for details
    -h, --help              Prints help information

Some common cargo commands are (see all commands with --list):
    build       Compile the current package
    check       Analyze the current package and report errors, but don't build object files
    clean       Remove the target directory
    doc         Build this package's and its dependencies' documentation
    new         Create a new cargo package
    init        Create a new cargo package in an existing directory
    run         Run a binary or example of the local package
    test        Run the tests
    bench       Run the benchmarks
    update      Update dependencies listed in Cargo.lock
    search      Search registry for crates
    publish     Package and upload this package to the registry
    install     Install a Rust binary. Default location is $HOME/.cargo/bin
    uninstall   Uninstall a Rust binary

See 'cargo help <command>' for more information on a specific command.

how can I solve this ?

Looks like you need the Remote WSL extension from Microsoft. That will connect your vs code session to a WSL session where you have cargo installed

Also try running which cargo from the WSL terminal to find the directory containing cargo, then run echo $PATH on the VS Code terminal and make sure that the directory is in that list.

it seems on a wierd way that my path was messed up

I don't think that's what the extension is for. I think that's used to work inside WSL, browse the WSL's directories etc. If you want to simply use WSL to build something in say 'D:/Dev/SomeRustProject' you should be able to simply set the ubuntu terminal as the default terminal for vscode and that should be sufficient.


That works too. But the nice thing about the extension is if the intent is to use WSL for a project, you can open a project in vs code in the extension and then everything gets routed through vs code including the terminal, the debugger and any extensions, without any additional setup. And you can certainly open a project through WSL at /mnt/d/Dev/SomeRustProject.

Yeah you can do that definitely. Just pointing out that it's not particularly the solution/cause to OP's problem rather a totally different way to go about it. And yeah that is IMO the more elegant solution, If I used windows I'd probably just use WSL completely using the WSL extension.

1 Like

Yep, that is what I do.

Reluctantly I was strong armed by an ex-boss to get a Windows machine as the company used some Windows only tool, having avoided Windows since 1997.

Luckily WSL arrived and I found I could do everything in there under Debian. Then I found I could use VS Code to work in there as well.

It all works really well. So much so I sometimes forget this is not a Linux machine.

Btw, if you haven’t moved to WSL2, you should. The file performance for things stored in /home is much better than WSL v1.

My workplace is running .NET stuff with Visual Studio, MSSQL, Xamarin.... the whole shebang. If I wasn't working from home I'd die a little every day :sweat_smile: thankfully I have my own laptop on hand since I am at home.

You can pretty much use Windows as a Linux machine as long as you don't have to touch a gui. It really is a breath of fresh air. My windows environment (in my home desktop, not the work laptop or my personal manjaro laptop) has pretty much nothing that makes it seem like it belongs to a developer. Everything there like RUST and everything else is set up under WSL.


First all thanks for the help
Im using WSL2 for rust development

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.