Installing Visual Studio Code (VS Code)#

Visual Studio Code (VS Code) is a popular open-source code editor developed by Microsoft. It offers powerful features like syntax highlighting, debugging support, integrated Git, and an extensive marketplace for extensions, making it a favorite among developers. This guide covers the installation process for VS Code on Windows, macOS, and Linux, emphasizing the use of command-line tools for streamlined setup.

Installing VS Code on Windows#

You can install VS Code on Windows using traditional installers or via command-line package managers like winget, choco, and scoop.

Method 1: Using winget#

  • Overview: winget is a command-line package manager for Windows provided by Microsoft.

  • Installation:

    • Open Command Prompt or PowerShell and run:

      winget install Microsoft.VisualStudioCode
      
    • This command downloads and installs the latest version of VS Code.

  • Verify Installation:

    • Launch VS Code from the Start Menu or by typing code in the command line:

      code .
      

Method 2: Using Chocolatey (choco)#

  • Overview: Chocolatey is a package manager for Windows that simplifies software installation.

  • Installation:

    • Install Chocolatey by following the instructions on Chocolatey’s website.

    • After installing choco, open Command Prompt or PowerShell and run:

      choco install vscode
      
    • This will install VS Code and add it to your system PATH.

  • Verify Installation:

    • Launch VS Code with:

      code .
      

Method 3: Using scoop#

  • Overview: scoop is a lightweight command-line installer for Windows.

  • Installation:

    • Install scoop by running the following in PowerShell:

      iwr -useb get.scoop.sh | iex
      
    • After scoop is installed, install VS Code using:

      scoop install vscode
      
  • Verify Installation:

    • Open VS Code using:

      code .
      

Method 4: Using the Installer#

  • Overview: Download the VS Code installer directly from the official website.

  • Installation:

    • Visit the download page and select the installer for your Windows version.

    • Run the installer and follow the setup wizard.

    • Ensure you check “Add to PATH” during installation.

  • Verify Installation:

    • Launch VS Code by searching for it in the Start Menu or by typing code in the command line.

Installing VS Code on macOS#

Method 1: Using brew#

  • Overview: brew is a popular package manager for macOS that simplifies software installations.

  • Installation:

    • Install Homebrew by running the following in Terminal:

      /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
      
    • Install VS Code with:

      brew install --cask visual-studio-code
      
  • Verify Installation:

    • Launch VS Code from the Applications folder or using the terminal:

      code .
      

Method 2: Using the Installer#

  • Overview: Download the VS Code .dmg file from the official website.

  • Installation:

    • Open the downloaded .dmg file and drag the Visual Studio Code icon into the Applications folder.

  • Verify Installation:

    • Open VS Code from the Applications folder or by typing code in the terminal.

Installing VS Code on Linux#

VS Code can be installed on various Linux distributions using package managers like apt, dnf, or directly downloading the .deb or .rpm package.

Method 1: Using apt for Debian/Ubuntu#

  • Overview: apt is the package manager for Debian-based systems like Ubuntu.

  • Installation:

    • Add Microsoft’s repository and install VS Code using the following commands:

      sudo apt update
      sudo apt install wget gpg
      wget -qO- https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor > packages.microsoft.gpg
      sudo install -o root -g root -m 644 packages.microsoft.gpg /etc/apt/trusted.gpg.d/
      sudo sh -c 'echo "deb [arch=amd64] https://packages.microsoft.com/repos/vscode stable main" > /etc/apt/sources.list.d/vscode.list'
      sudo apt update
      sudo apt install code
      
  • Verify Installation:

    • Open VS Code using:

      code .
      

Method 2: Using dnf for Fedora#

  • Overview: dnf is the package manager for Fedora.

  • Installation:

    • Import the Microsoft GPG key and install VS Code:

      sudo rpm --import https://packages.microsoft.com/keys/microsoft.asc
      sudo sh -c 'echo -e "[code]\nname=Visual Studio Code\nbaseurl=https://packages.microsoft.com/yumrepos/vscode\nenabled=1\ngpgcheck=1\ngpgkey=https://packages.microsoft.com/keys/microsoft.asc" > /etc/yum.repos.d/vscode.repo'
      sudo dnf check-update
      sudo dnf install code
      
  • Verify Installation:

    • Launch VS Code with:

      code .
      

Method 3: Using yum for CentOS/RHEL#

  • Overview: yum is the package manager for CentOS and older versions of RHEL.

  • Installation:

    • Follow the steps for adding the Microsoft repository as shown above for dnf.

    • Use yum to install VS Code:

      sudo yum install code
      
  • Verify Installation:

    • Open VS Code using:

      code .
      

Method 4: Using the .deb or .rpm Package#

  • Overview: Download the .deb or .rpm package from the official website.

  • Installation:

    • For Debian/Ubuntu:

      sudo apt install ./path-to-file.deb
      
    • For Fedora/CentOS:

      sudo rpm -i path-to-file.rpm
      
  • Verify Installation:

    • Launch VS Code using:

      code .
      

Setting Up Python in VS Code#

Once you have installed Visual Studio Code, the next step is to set it up for Python development. VS Code provides a rich ecosystem of extensions that make Python programming more efficient and enjoyable. Follow these steps to set up Python in VS Code and add essential extensions.

1. Installing the Python Extension#

  • Overview: The Python extension in VS Code is provided by Microsoft and is essential for a smooth Python development experience. It adds features like syntax highlighting, code completion, debugging, and integrated Jupyter Notebook support.

  • Installation:

    • Open VS Code.

    • Go to the Extensions View by clicking on the Extensions icon in the sidebar or pressing Ctrl+Shift+X.

    • Search for “Python” and click Install for the extension provided by Microsoft.

    • Alternatively, use the command palette (Ctrl+Shift+P or Cmd+Shift+P on macOS) and type:

      ext install ms-python.python
      

2. Selecting the Python Interpreter#

  • Overview: After installing the Python extension, you need to select the interpreter that VS Code will use for running your Python scripts. This is crucial if you have multiple Python versions or virtual environments installed.

  • Steps:

    • Open any Python file or create a new one with a .py extension.

    • Press Ctrl+Shift+P (or Cmd+Shift+P on macOS) to open the Command Palette.

    • Type Python: Select Interpreter and press Enter.

    • Choose the appropriate Python interpreter from the list (e.g., Python 3.x.x).

    • This selection will be saved in the .vscode/settings.json file in your project directory.

4. Running Python Scripts in VS Code#

  • Overview: You can run Python scripts directly from VS Code, either through the integrated terminal or using the Run Python File button.

  • Steps:

    • Open a .py file in VS Code.

    • Press F5 to run the file in the debugger, or click the Run Python File button in the top right corner.

    • To run scripts in the integrated terminal, open the terminal with `Ctrl+`` and type:

      python your_script.py
      

5. Configuring Virtual Environments in VS Code#

  • Overview: Using a virtual environment allows you to isolate dependencies for different projects, preventing conflicts between packages.

  • Steps:

    • Create a virtual environment:

      python -m venv venv
      
    • Activate the virtual environment:

      • Windows: venv\Scripts\activate

      • macOS/Linux: source venv/bin/activate

    • Once activated, VS Code should automatically recognize the environment. If not, use Python: Select Interpreter from the command palette to select the environment.

Summary#

VS Code can be installed on any operating system using different methods, including direct downloads and command-line package managers. Here’s a quick recap:

  • Windows users can leverage winget, choco, and scoop for efficient installation.

  • macOS users can use brew for a streamlined setup.

  • Linux users can install using apt, dnf, yum, or download .deb/.rpm packages directly.

  • Using command-line tools makes it easy to install, update, and manage your VS Code installation, helping you get started with coding quickly and efficiently.

  • Setting up Python in VS Code involves installing the Python extension, selecting the right interpreter, and adding useful extensions like Pylance and Jupyter. With these tools, you can leverage VS Code’s powerful features to write, debug, and manage Python code more efficiently. The combination of a lightweight editor and a robust set of extensions makes VS Code a powerful IDE for Python development.